File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection question 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 question" Watch "Garbage Collection question" New topic
Author

Garbage Collection question

Saumya Srivastava
Ranch Hand

Joined: Mar 19, 2007
Posts: 45
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) :



OCPJP 6.0
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Exactly. You do that and going wrong is unlikely.
Saumya Srivastava
Ranch Hand

Joined: Mar 19, 2007
Posts: 45
Can't make out the diagram for the above code
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102

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

Joined: Mar 19, 2007
Posts: 45
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

Joined: Oct 05, 2010
Posts: 102
Hold on a bit. I'm taking out a paper.
Saumya Srivastava
Ranch Hand

Joined: Mar 19, 2007
Posts: 45
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

Joined: Oct 05, 2010
Posts: 102
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

Joined: Oct 05, 2010
Posts: 102
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

Joined: Mar 19, 2007
Posts: 45
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

Joined: Oct 05, 2010
Posts: 102
My pleasure. What's a greenhorn btw? I'm a Ranch hand, you seem to be a greenhorn.
Saumya Srivastava
Ranch Hand

Joined: Mar 19, 2007
Posts: 45
I m sorry..I don't know what is the purpose of that... how it came and what it means...Do you know?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abhi Ahuja
Greenhorn

Joined: Oct 11, 2010
Posts: 7
Thanks Ankit. Very nice explanation.
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

this question is explained in earlier thread gc thread


Thanks & Regards
Sumit Kothalikar
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Dude, I don't get it. How will anything be eligible for garbage collection at line 3?
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

rohit see my above thread it has bitmap image of the above garbage collection problem
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: Mar 19, 2007
Posts: 45
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

Joined: Oct 05, 2010
Posts: 102
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?
Moahmmed Raes
Greenhorn

Joined: Apr 13, 2012
Posts: 3
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection question