Link to home
Start Free TrialLog in
Avatar of Cheney
Cheney

asked on

Fast dereference detection in Java

Hi!

Is there any fast way of detecting when an object loses all its references in Java?

I'm currently using the WeakReference class to allow the garbage collector to decide for me when an object is no longer referenced. The issue is that its just too slow (I've written a unit test to demonstrate this - in the test it takes a System.gc() and a Thread.sleep(200) before the get() method on the weak reference returns null - far too slow for my application).

Thanks!
Avatar of for_yan
for_yan
Flag of United States of America image

why do you need this class WeakReference ?
Isn't it this decision a part of Java garbage collection operation?
As far as I understand WeakReferences are used in order to maintain collections of objects that can grow too large
and also to avoid memory leask due to groups of mutually referencing objects which are not directly referenced by other objects and are unreachable can thus become permanently resident;
see here
http://en.wikipedia.org/wiki/Weak_reference
and here:

http://oreilly.com/catalog/javapt/chapter/ch04.html

They do not say that weak references speed up grabage collection.

which version of java are you using?

If you are concerned with garabge collection, and if you are not using Java 7, perhpas you want
to try Java 7 which has siginificantly changed garbage collection
which is getting good reviews:
http://stackoverflow.com/questions/2254041/java-g1-garbage-collection-in-production


and of course follow some advice on good practices related to garbage collcetion:
this rather old but still useful papers about it:

http://www.ibm.com/developerworks/java/library/j-jtp01274/index.html

http://www.ibm.com/developerworks/java/library/j-jtp03216/index.html
Avatar of Cheney
Cheney

ASKER

Thanks for your relies :)

I'm not so much worried about garbage collection itself. What I'm trying to do is almost implement my own garbage collection for objects which use native resources.

I'm using WeakReference so that I can find out when an object is no longer used so I can recycle its native resources. The issue is that its the garbage collector who decides when weakReference.get() stops returning my object and starts returning null.

I have a reaper thread which does the following continiously:

 
while(!Thread.interrupted()) {
    for(NativeInfo nInfo : info) {
       if(nInfo.getWeakReference.get() == null) {
             nInfo.recycleResources();
       }
    }

    Thread.yield();
}

Open in new window


Problem is even after my objects are dereferenced the loop above doesn't pick them up until after the next major garbage collection - which as my heap is mostly empty doesn't happen for a very long time. As a result I run out of native resources long before any of them get recycled and my app dies.
Avatar of Cheney

ASKER

Ideally I'd want recycleResources to be called the next time the reaper thread runs through the loop after the object is dereferenced by all the other threads which happen to have a strong reference to it.
ASKER CERTIFIED SOLUTION
Avatar of gordon_vt02
gordon_vt02

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cheney

ASKER

That makes sense.

I wanted to avoid intermediate classes and have the client having to clean up after themselves but it looks like it's the most reliable way forward. Maybe Java 7s try with resource will make it look less messy :)
Avatar of Cheney

ASKER

I ended up using reference counting as suggested and hand a handler class which implemented AutoClosable manage the count and hold the reference to my natives. That way the client can use Java 7s try with resource mechanism.

Its a shame I couldn't totally remove the responsibly of cleaning up from the client but this solution is good enough.

Thanks.