D
Dave Rudolf
Hi all,
This might be a better question for the VM newsgroup, but they don't seem to
get a lot of readers there, so I thought I would try this group:
Suppose that I have a collection of WeakReference objects, and I want to
know when the referent object is finalized by the garbage collector so that
I can remove the reference from the collection. I noticed that the Reference
class has a public non-final method called clear(), and also when I look at
the java source code for Reference (jdk1.4.1_01-b01) , this clear() method
is the only place where the referent is getting set to null. I figured that
I could override this clear() method to notify listeners of what is
happening. So I decided to write my own reference class like so
public class BetterThanWeakReference extends
java.lang.ref.WeakReference
{
public BetterThanWeakReference( final Object referent )
{
super( referent );
}
public void clear()
{
super.clear();
/** @todo Fire events, or whatever */
System.err.println( "Cleared" );
}
}
If I then create hordes and hordes of these references, and then try to get
the referent, I pretty much always get null, yet my overridden method is
never called. Is there some JVM voodoo happening here that is setting the
reference's referent to null without calling clear()???
Dave
This might be a better question for the VM newsgroup, but they don't seem to
get a lot of readers there, so I thought I would try this group:
Suppose that I have a collection of WeakReference objects, and I want to
know when the referent object is finalized by the garbage collector so that
I can remove the reference from the collection. I noticed that the Reference
class has a public non-final method called clear(), and also when I look at
the java source code for Reference (jdk1.4.1_01-b01) , this clear() method
is the only place where the referent is getting set to null. I figured that
I could override this clear() method to notify listeners of what is
happening. So I decided to write my own reference class like so
public class BetterThanWeakReference extends
java.lang.ref.WeakReference
{
public BetterThanWeakReference( final Object referent )
{
super( referent );
}
public void clear()
{
super.clear();
/** @todo Fire events, or whatever */
System.err.println( "Cleared" );
}
}
If I then create hordes and hordes of these references, and then try to get
the referent, I pretty much always get null, yet my overridden method is
never called. Is there some JVM voodoo happening here that is setting the
reference's referent to null without calling clear()???
Dave