• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Question on GC

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me understand the best (easy) way to tackle this kind of question. I have a trouble of getting the right answer.

How many objects are eligible for GC, after executing the line-10 of this program?


Thanks a lot for all your help.


 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way I have found is to go through the code line by line and every time a new object is created, draw a circle on a piece of paper, write the reference variable down, and draw a line between the two. Then, when that reference gets changed, mark out the first line and draw a line to the other object (or nothing, if null). Once you get through all of the code, count how many objects don't have any more live references and tada!*

Note: You could also get an island of isolation (I think that's the term I've seen...) where some object point to each other through instance variables, but don't have any live references in the program. Even though each object technically has a reference, since there is no way to access the objects anymore, they are eligible.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats the technique I try to use but I often get gotcha'd!
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the answer of your question, I get it as 1.
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is 3. Thanks Joe. I will try sketching a diagram.

Thank you guys for replying.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tayitu Betule wrote:The answer is 3. Thanks Joe. I will try sketching a diagram.

Thank you guys for replying.



That's what I got when I drew it out. This is a pretty tricky one too, with all those nested new calls, and the constructor taking an object.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup there are 3 objects eligible for gc.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, three objects are eligible for GC,:-the object pointed to originally by gb1.g.g no
longer has a reference and also the objects pointed to originally by gb2.g and gb2.g.g also lose
any references pointing to them. I tend to use the same method as W. Joe Smith, although this was
a particularly nasty GC question. It seems to test your ability to solve puzzles rather than your
ability to program or your knowledge of Java.

Martin
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey neha i have posted a new topic called object creation...my question lies there.....

thanks
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic