• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marilyn,once again.
I got it.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic