This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Another Garbage Collector question

Matheus Souza
Ranch Hand
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
Posts: 9528
33
• 1
See if this diagram helps...

Matheus Souza
Ranch Hand
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
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
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
Posts: 41
• 1
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
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: 224
1
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
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
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..