• 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

On garbage Collector

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear,
How to determine the numbers of objects are eligible for garbage collection in a particular java program.

Please explain!!!
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects having no references in given program become eligible for garbage collection. Just check how many objects in the program loose their references(0 references) and thats the count.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding on to Mohit's response, an object can actually still have some references pointing to it, and still be eligible for garbage collection. All that matters is that there must be no reachable references, i.e. references that can still be used by the current program. Two objects may have mutual references to each other, but as long as there are no reachable references to either object, then they're both eligible for garbage collection. For example:


(Things get a bit more complicated once we also start talking about soft/weak/phantom references, but those are thankfully outside the scope of the SCJP.)
 
Ranch Hand
Posts: 1274
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!

You can also write some code where you override the finalize method of a given class so you see some output from the garbage collection:


But when you are really looking at some code,

the best thing is to draw a little pic with your variables pointing to the objects created. Then you should see when an object is no longer referenced and therefore eligible for GC.
In this example after line 3 it should look like:


After line 4 one is nulled, but that does not mean that the object is nulled, regard this as if the variable points to a null object:


Both Bananas are still there and none is eligible for GC.

After line 5 however there is no arrow pointing at the second banana and therefore it is eligible for GC.


If the GC (a background thread) runs you got the output from the finalize method if you have overridden it:


You may try to paint the situation from the code with the island of isolation provided by Kelvin.

By the way, drawing:
In the prometric test centers they will give you something to draw on, but you have to leave this at the testing center.

Yours,
Bu.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I whole-heartedly second the diagram drawing advice. As a matter of fact, that's exactly how I go about solving these garbage collection problems too. Excellent tip, Bu! (Cool drawings, too.)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow Burkhard, you really did your best on this one with the drawings and all!
[ November 17, 2007: Message edited by: Jesper Young ]
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear
Barkhard,
Very good explanation using diagram. Thanks a lot. I understood but having doubt on the last paragraph about isolation.


 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey
i can't see the drawings ......
can you send me this ?
i don't know if it is the explorer and i can't see the drawings ...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it is a two year old topic, so you can't be surprised that whatever temporary location that was used to host those diagrams may be gone.

Henry
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I anticipated that this day would come and kept backups, which I've now restored. Enjoy!
 
Police line, do not cross. Well, this tiny ad can go through:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic