• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection question

 
Wu Wen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21426
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Wu Wen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Please QuoteYourSources


The question comes from OCP Java SE 6 Programmer Practice Exams Page 181
 
Sebanti Sanyal
Ranch Hand
Posts: 58
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Selena Klasnja
Greenhorn
Posts: 1
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic