aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Hi Everybody,
Consider the following program,originally created by Antti Barck

result:Finalize:CombineSimple

Here, getSimple() in combine class returns an object of Simple class,
therefore according to me two references for Simple class object exists,
(1)One in private object reference variable obj of CombineSimple
(2)Second in bah a Simple class object reference variable in UseSimple's main method.
If I set har=null, which is an object reference variable for CombineSimple,then a call to System.gc() invokes the finalize method for this object.Now my question is what happens to private variable obj of CombineSimple at the time of GC, since this variable and the variable bah in main both refer to same Simple object,will obj be set to null by the GCwhen it is forcefully called, thereby reducing the number of references to Simple object in heap, to one or will it do something else ?
Please help....
THANKS

[This message has been edited by Bindesh Vijayan (edited August 29, 2001).]
[This message has been edited by Bindesh Vijayan (edited August 30, 2001).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Helo Bindesh!
I guess Mr. Hockleys explanation on your other thread covered this one as well, so I am going to take five here... hey, Dallas, could you swing up something for this matter as well?
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Antti Barck (edited August 30, 2001).]
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello everybody
Putting System.out.println(obj); within CombineSimple's finalize shows that obj is not set to null, at least until the execution of finalize.
I think that setting to null the fields of a object pointed by reference that was set to null or becames out of scope, would be dangerous: other references could point to the same object. So, this could be done only when the gc determines that the object is not reachable. This doesn�t seem useful at all because the objects pointed by fields of an unreachable object are not going to be accessed via this object.


SCJP2. Please Indent your code using UBB Code
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Thanks Jose you gave me a wonderful idea.I worked out and found that obj is never set to null even after the super.finalize() statement is called in finalize(),obj becomes unreachable as soon as har is set to null.
[please correct if wrong]
THANKS EVERYBODY
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
It seems that obj is unreachable after the closing bracket of the getSimple() method.
har becomes unreachable after it is set to null and then it calls the finalize() method of CombineSimple.
bah never becomes unreachable in this particular code, therefore the finalize() method of the Simple class is never called.
If you set both har and bah to null, you will see both statements print out.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
When you set har = null, the object referenced by har becomes eligible for GC.The System.gc() forces the finalize method to be called to allow GC.After the completion of finalize(), the har (along with its attributes obj) becomes eligible for GC.
However, note that bah would still not be GCed, because the reference is still active.You will need to unreference the object held by bah to be allowed to GCed.This is possible if you set bah = null or reference bah to another object like bah = new Simple();.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Thanks Marilyn And Sandeep.
As Sandeep say's After the completion of finalize(), the har (along with its attributes obj) becomes eligible for GC..But even after the finalize method is called,as Jose said,if you put
System.out.println(obj);
as the last line in finalize(),you will see Simple printed with some special characters.Does'nt this mean that obj is not set to null?
But if you explicitly assign obj=null in finalize(),then it prints null.
Please clear it.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
If har = null, obj is not set to null. The reference to obj is set to null. All the links are broken and obj is eligible for gc even though obj itself is not null.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 104
Thanks Marilyn,once again.
I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection