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

Garbage Collection question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which are true? (Choose all that apply.)
A. At line 8, one object is eligible for garbage collection.
B. At line 8, two objects are eligible for garbage collection.
C. At line 8, three objects are eligible for garbage collection.
D. At line 18, 0 objects are eligible for garbage collection.
E. At line 18, two objects are eligible for garbage collection.
F. At line 18, three objects are eligible for garbage collection.

The right answers are C, D. Anyone can explain me the steps in detail? Thanks a lot
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please QuoteYourSources
 
Wu Wen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Please QuoteYourSources



The question comes from OCP Java SE 6 Programmer Practice Exams Page 181
 
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inside method go(),two objects are created. f3 refers tothe object originally referred by f2. That particular object has a member variable f,which again refers to the object initially referenced by f1. Therefore, none of the two are eligible for GC at line 18. If we also had f3=null;, the two objects would still had referenced each other but none of them could be accessed externally and hence eligible for GC.
In main(),one object is created on which go() was invoked.That object was created on the go,and there is no reference associated with it.Hence, it is definitely eligible for GC at line 8.The two objects created inside go() were accessible only with a local variable f3,which does not live anymore after go() returns. These two can also be garbage collected at line 8.
 
Wu Wen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebanti Sanyal wrote:Inside method go(),two objects are created. f3 refers tothe object originally referred by f2. That particular object has a member variable f,which again refers to the object initially referenced by f1. Therefore, none of the two are eligible for GC at line 18. If we also had f3=null;, the two objects would still had referenced each other but none of them could be accessed externally and hence eligible for GC.
In main(),one object is created on which go() was invoked.That object was created on the go,and there is no reference associated with it.Hence, it is definitely eligible for GC at line 8.The two objects created inside go() were accessible only with a local variable f3,which does not live anymore after go() returns. These two can also be garbage collected at line 8.



Thank your for the explanation it is very clear.

If I remove base=null, what will be the result? I'm a little confused because base is static. Thanks
 
Sebanti Sanyal
Ranch Hand
Posts: 58
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If I remove base=null, what will be the result? I'm a little confused because base is static. Thanks



Answer will be A and D. The objects created inside go() can still be referenced through base at line 8.
 
Greenhorn
Posts: 1
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wu Wen wrote:

Sebanti Sanyal wrote:Inside method go(),two objects are created. f3 refers tothe object originally referred by f2. That particular object has a member variable f,which again refers to the object initially referenced by f1. Therefore, none of the two are eligible for GC at line 18. If we also had f3=null;, the two objects would still had referenced each other but none of them could be accessed externally and hence eligible for GC.
In main(),one object is created on which go() was invoked.That object was created on the go,and there is no reference associated with it.Hence, it is definitely eligible for GC at line 8.The two objects created inside go() were accessible only with a local variable f3,which does not live anymore after go() returns. These two can also be garbage collected at line 8.



I think it is a good explanation but only if f3 doesn't play any further role. For example:
What would be the scenario if we instead of doStuff at line 18 have something like this:

reply
    Bookmark Topic Watch Topic
  • New Topic