Re: more on SoftReference, WeakReference and PhantomReference
"Richard O. Hammer" <[email protected]>
| Newsgroups | gmane.org.user-groups.trijug.juglist |
|---|---|
| Message-ID | <[email protected]> |
Concerning the meaning of "scope" as used by the garbage collector --
I have assumed that a referent object fell out of scope when either:
1. the object in which the referent object is an instance variable is no
longer reachable, or
2. the method in which the referent object is a local variable has finished.
In your example you use another valid but different meaning of falling
out of scope:
3. the block of code in which a local variable is declared has finished,
although the same method is still being executed.
Below I show another test, following your model, which seems to confirm
that the garbage collector uses at least definition 2 above.
The append code produces the following output.
-----------
Both Weak References survive before call to gc()
111, 222
Neither Weak Reference survives call to gc()
null, null
-----------
import java.lang.ref.WeakReference;
public class RefTest
{
public static void main (String[] args)
{
WeakReference <Integer> wr1 = getWeakReference(111);
WeakReference <Integer> wr2 = getWeakReference(222);
System.out.println (
"Both Weak References survive before call to gc()\n" +
wr1.get () + ", " + wr2.get ());
System.gc ();
System.out.println (
"Neither Weak Reference survives call to gc()\n" +
wr1.get () + ", " + wr2.get ());
}
static WeakReference<Integer> getWeakReference(int i)
{
Integer obj = new Integer(i);
return new WeakReference <Integer> (obj);
}
}
Harold Meder wrote:
> The appended code produces the following output.
>
> ----------
>
> Both Weak References survive while Strong References survive on stack.
> (1)111, 222
> Can't garbage collect it while it has a strong reference.
> (100) 111, 222
> Can't garbage collect, even when it Strong Reference goes out of scope.
> (100) 111, 222
> Can garbage collect, when Strong Reference set to null.
> (1) 111, null
> At last! If the stack is overwritten, the last Strong Reference is gone.
> (1) null, null
>
> ----------
>
> import java.lang.ref.WeakReference;
>
> public class RefTest
> {
> public static void main (String[] args)
> {
> WeakReference <Integer> wr1;
> WeakReference <Integer> wr2;
> {
> Integer obj1 = new Integer (111);
> Integer obj2 = new Integer (222);
> wr1 = new WeakReference <Integer> (obj1);
> wr2 = new WeakReference <Integer> (obj2);
> }
> // obj is out of scope,
> // but the garbage collector is lazy.
> System.gc (); Thread.yield ();
> System.out.println (
> "Both Weak References survive " +
> "while Strong References survive on stack.\n(1)" +
> wr1.get () + ", " + wr2.get ());
>
> int i = 0;
> Integer newStrongRef2;
> {
> // Before we loose the objects,
> // let's save them with new Strong Referenences.
> Integer newStrongRef1 = wr1.get ();
> newStrongRef2 = wr2.get ();
> while (wr1.get () != null)
> {
> System.gc (); Thread.yield ();
> if ( ++i > 99) break;
> }
> System.out.println (
> "Can't garbage collect it while it has a strong reference.\n("
> + i + ") " +
> wr1.get () + ", " + wr2.get ());
> }
>
> i = 0;
> {
> // OK, with newStrongRef1 out of scope,
> // but
> while ( wr1.get () != null)
> {
> System.gc (); Thread.yield ();
> if ( ++i > 99) break;
> }
> System.out.println (
> "Can't garbage collect, " +
> "even when it Strong Reference goes out of scope.\n("
> + i + ") " +
> wr1.get () + ", " + wr2.get ());
>
> i = 0;
> newStrongRef2 = null;
> {
> // OK, with newStrongRef1 out of scope,
> // but
> while (wr2.get () != null)
> {
> System.gc (); Thread.yield ();
> if ( ++i > 99) break;
> }
> System.out.println (
> "Can garbage collect, " +
> "when Strong Reference set to null.\n("
> + i + ") " +
> wr1.get () + ", " + wr2.get ());
> }
> }
>
> Integer obj3 = new Integer(333);
> i = 0;
> {
> while (wr1.get () != null)
> {
> System.gc (); Thread.yield ();
> if ( ++i > 999) break;
> }
> System.out.println (
> "At last! If the stack is overwritten, " +
> "the last Strong Reference is gone.\n("
> + i + ") " +
> wr1.get () + ", " + wr2.get ());
> }
> }
>
> }
>