• 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 question

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to work on Garbage Collector questions? I never get my answers right. Is there any way to find out which of the objects are eligible for Garbage Collection?
Can anyone of you please tell me how to find out how many objects are eligible for GC?

Following is the code(This question is from Examlab) :

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use the following method:

Take a piece of paper and draw the objects on the right and the references on the left.
Use lines to connect them. Then go through the code and erase and draw new lines to
update the references.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly. You do that and going wrong is unlikely.
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't make out the diagram for the above code
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When line 0 executes,
---------ob1--------ob2---------ob3
gb1->grab(){g->grab(){g->grab(){g->null}}}
gb1.g---------------^
gb1.g.g---------------------------^
gb1.g.g.g------------------------------------^
-------------------------------------------ob4---------ob5
So when line1 executes- gb1.g.g->grab(){g->grab(){g->null}}, when gb1.g.g points to a new object, ob3 has nothing accessing it.

When line2 executes-
---------ob6--------ob7----------ob8
gb2->grab(){g->grab(){g->grab(){g->gb1}}}
gb2.g----------------^
gb2.g.g-----------------------^
gb2.g.g.g-> the same object pointed by gb1;

When line3 executes-
gb2.g-> the same object pointed by gb1.g, ob7 has nothing accessing it.

Totally 8 objects are created.

I think 2 objects ob3 and ob7 are eligible for Garbage collection. Please let me know if I'm wrong.
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohit Ramachandran wrote:
When line 0 executes,
---------ob1--------ob2---------ob3
gb1->grab(){g->grab(){g->grab(){g->null}}}
gb1.g---------------^
gb1.g.g---------------------------^
gb1.g.g.g------------------------------------^
-------------------------------------------ob4---------ob5
So when line1 executes- gb1.g.g->grab(){g->grab(){g->null}}, when gb1.g.g points to a new object, ob3 has nothing accessing it.

When line2 executes-
---------ob6--------ob7----------ob8
gb2->grab(){g->grab(){g->grab(){g->gb1}}}
gb2.g----------------^
gb2.g.g-----------------------^
gb2.g.g.g-> the same object pointed by gb1;

When line3 executes-
gb2.g-> the same object pointed by gb1.g, ob7 has nothing accessing it.

Totally 8 objects are created.

I think 2 objects ob3 and ob7 are eligible for Garbage collection. Please let me know if I'm wrong.



In the ExamLab Answer is- 3 objects are eligible for GC.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hold on a bit. I'm taking out a paper.
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether gb1.g.g.g will also be eligible to GC? As it is pointing to null..? I am not sure
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes yes yes.

3 Objects eligible for garbage collection. As I've mentioned ob3 and ob7. Since ob8 can be reached only through an ob7.g, since ob7 can't be accessed even ob8 can't be accessed.

Hence 3 objects eligible are ob3,ob7 and ob8. You should understand it now. If you don't read it and try.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saumya Srivastava wrote:Whether gb1.g.g.g will also be eligible to GC? As it is pointing to null..? I am not sure



No no no. references aren't eligible for garbage collection. Only Objects are.
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohit Ramachandran wrote:Yes yes yes.

3 Objects eligible for garbage collection. As I've mentioned ob3 and ob7. Since ob8 can be reached only through an ob7.g, since ob7 can't be accessed even ob8 can't be accessed.

Hence 3 objects eligible are ob3,ob7 and ob8. You should understand it now. If you don't read it and try.



Thank you Rohit. You explained it very well
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure. What's a greenhorn btw? I'm a Ranch hand, you seem to be a greenhorn.
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I m sorry..I don't know what is the purpose of that... how it came and what it means...Do you know?
 
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

Saumya Srivastava wrote:I m sorry..I don't know what is the purpose of that... how it came and what it means...Do you know?



A unrelated question, but easy to answer...

http://faq.javaranch.com/java/SaloonTitles

Henry
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it helps, I draw diagrams like this for GC questions (objects marked as GC in the diagram are eligible for garbage collection)
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit. Very nice explanation.
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this question is explained in earlier thread gc thread
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude, I don't get it. How will anything be eligible for garbage collection at line 3?
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rohit see my above thread it has bitmap image of the above garbage collection problem
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dude, I don't get it. How will anything be eligible for garbage collection at line 3?


If this statement was meant for me, then I would say a new object was not eligible for GC. Its the same object that became eligible for GC at line 2. I didn't remove it from the diagram so that in the last diagram you can clearly see that total 3 objects are eligible for GC...
 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:If it helps, I draw diagrams like this for GC questions (objects marked as GC in the diagram are eligible for garbage collection)



Got it !! Very easily understood... thank u...
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude I'm not sure exactly which 3 objects are eligible for garbage collection. Could you please look at my diagram above and tell me if it's ob3, ob7 and ob8 eligible?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Thanks for your diagrams.But i would like to see how the diagrams change when you have a static instance variable in the class Grab apart from g.Please explain that too.Thanks in advance
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic