Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another Garbage Collector question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another Garbage Collector question" Watch "Another Garbage Collector question" New topic
Author

Another Garbage Collector question

Matheus Souza
Ranch Hand

Joined: Mar 06, 2012
Posts: 38

Hey guys I was doing an mock from ExamLab and this question appeared:



The answer is 3, but I one found 1 object eligible for the colection. Can anyone knows the others 2?

Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

See if this diagram helps...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Matheus Souza
Ranch Hand

Joined: Mar 06, 2012
Posts: 38

Ankit Garg wrote:See if this diagram helps...


Man you're awsome, I got it. Thanks for the diagram it helps a lot.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Ankit Garg wrote:See if this diagram helps...

I don't even want to think about how long it must have taken to write that out in ASCII. But I hope everyone preparing for the exam will take note how much an effective diagram like this simplifies a complicated GC problem. There's really no better way to tackle it.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
HI Ankit,
Thank you so much for the diagram. It helps a lot to understand the tricky questions.
Can you explain little more about the following line of code? And what gb1.g.g is referring to?,


Ankit Garg wrote:See if this diagram helps...
Yalvin Duha
Ranch Hand

Joined: Apr 07, 2012
Posts: 40

Ankit Garg wrote:See if this diagram helps...


I don't understand how you got from this:


To this!!!:

First, gb2.g.g should point to the rectangular object (not the object that holds it (it being gb2.g.g)) -- same with gb1.g.g:

Then after "line 10" is executed, the diagram should look like this:

Now, there is no access to GB(2) and GB(3) from any of the main references, gb1 and gb2 (however, there is a reverse reachability available from GB(3) to GB(2), then to gb1 which doesn't count), so they become eligible for GC.
Niels Krijger
Greenhorn

Joined: Apr 22, 2012
Posts: 4
A method that worked for me (very quickly) was number each object and for each line write down the set of active references. If you do this you get the following:

123
1245
6781245
61245

Finally count the objects that dissapeared from the last line;
378

Total; 3

You can check by the count of the keyword 'new' whether you've written them all down (yep, I went up to 8 and count eight times 'new').
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

The best way to deal with GC problems is to draw memory locations and doing it thoroughly, as a trick might yield wrong results.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Niels Krijger
Greenhorn

Joined: Apr 22, 2012
Posts: 4
Rajdeep Biswas wrote:The best way to deal with GC problems is to draw memory locations and doing it thoroughly, as a trick might yield wrong results.


With hindsight Rajdeep's answer is much better, I'd have fallen into quite a few traps with my solution. One I have actually fallen into is a GC-resurrection question.
Sudipta Deb
Greenhorn

Joined: Dec 31, 2011
Posts: 6
The best way to deal with this is to draw the memory diagram. Ankit Garg's diagram is perfect and it is very easy to understand also..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another Garbage Collector question