Re: overwrite the stack?
Harold Meder <[email protected]>
| Newsgroups | gmane.org.user-groups.trijug.juglist |
|---|---|
| Message-ID | <[email protected]> |
I know nothing. It is my presumption that if the garbage collector will not collect it, there must still be a strong reference to it. As your investigation shows, when a reference to an additional object is put on the stack, the garbage collector does its job, when before adding the new reference to the stack, the garbage collector refused to collect the object pointed to by the reference that went out of scope. I believe that this was because a strong reference still existed on the stack, even if it was out of scope. I am open to alternative explainations. On 1/21/06, Richard O. Hammer <[email protected]> wrote: > > Harold, > > I've run your code and I'm curious to know how you know that the line > Integer obj3 = new Integer(333); > causes the stack to be overwritten. > > Even though the Integer obj3 is never used in the code, that line > changes the code's behavior (commenting it out changes the output of the > last print statement) at least in my tests running Java 1.5 from Eclipse > 3.1. > > Rich As you suggested, I have added a finalize method to my objects. See below. Harold. 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 222 has been Garbage Collected Can garbage collect, when Strong Reference set to null. (1) 111, null 111 has been Garbage Collected At last! If the stack is overwritten, the last Strong Reference is gone. (1) null, null --------------------------- import java.lang.ref.WeakReference; import org.omg.PortableServer._ServantActivatorStub; public class RefTest { public static void main (String[] args) { WeakReference <myClass> wr1; WeakReference <myClass> wr2; { myClass obj1 = new myClass ("111"); myClass obj2 = new myClass ("222"); wr1 = new WeakReference <myClass> (obj1); wr2 = new WeakReference <myClass> (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; myClass newStrongRef2; { // Before we loose the objects, // let's save them with new Strong Referenences. myClass 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 ()); } } } class myClass { private final String _string; myClass (String string) { _string = string; } public void finalize () { System.out.println(toString()+" has been Garbage Collected"); } public String toString() { return _string; } } _______________________________________________ Juglist mailing list [email protected] http://trijug.org/mailman/listinfo/juglist_trijug.org