• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Garbage Collector question

 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if this diagram helps...
 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Java Slackware
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to deal with GC problems is to draw memory locations and doing it thoroughly, as a trick might yield wrong results.
 
Niels Krijger
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic