• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question from Practice exams

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



The answer is:
At GC point 1 -> 3 objects are eligible for GC.
At GC point 2 -> 0 objects are eligible for GC.

I understand, second anser. But I feel the first answer should be => 2 objects are eligible for GC.

Larsen.



 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. The answer is 3. Why do you think it should be 2?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Larsen,

At GC point 2, there are two object eligible for GC (f1 and f2), so, option 2 is not the answer.

Now, GC point 1 comes after GC point 2, and at GC point 1, another object is eligible for GC(the one created by new Fiji()), so, at GC point 1, three objects are eligible for GC.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, at point 2 there are no object eligible, because the two objects created in the go() method are still reachable. As soon as the method returns though, they become unreachable.

You're correct about point 1 though.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,

At last statement of method go()

So, both objects created in method go() are now unreachable, and hence eligible for GC, right?
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, f3 still has a reference to the object that was referenced by f2, and that object's f field references the object that was initially assigned to f1.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Stephan, you are right. I missed it

f3 refers to f1.f, so f1 is not eligible for gc. Further, f1.f refers to f2, so f2 also is not eligible for gc. So... is it that both options are correct (i.e. no object is eligible for gc at GC point 2)?

Need to brush up gc fundamentals again
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Just ignored the simple fact. Since nothing is returned from the method, all the objects created in the method should be available for GC. Additionally, nothing is persisted in the static field. Thank you guys.


Larsen.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic