• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Another Garbage Collector question

 
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9687
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Ranch Hand
Posts: 46
Eclipse IDE Slackware Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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').
 
Ranch Hand
Posts: 231
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic