Java Programming - Garbage Collections - Discussion

Discussion :: Garbage Collections - Pointing out the correct statements (Q.No.2)

2. 

Which statement is true?

[A]. All objects that are eligible for garbage collection will be garbage collected by the garbage collector.
[B]. Objects with at least one reference will never be garbage collected.
[C]. Objects from a class with the finalize() method overridden will never be garbage collected.
[D]. Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.

Answer: Option D

Explanation:

All objects are placed in the garbage collectible heap.

Option A is incorrect because the garbage collector makes no guarantees.

Option B is incorrect because islands of isolated objects can exist.

Option C is incorrect because finalize() has no such mystical powers.


Alexander Monakhov said: (Sep 9, 2010)  
void f()
{
Object o = new Runnable() { public void run() {} };
}

'o' is allocated on stack, or JIT used is just a crap.

Corrie said: (Jul 27, 2011)  
All objects are actually not placed in the garbage collectible heap. Only ones that can be garbage collected.

Objects that are instantiated in an anonymous class might still be used, because they are still in scope. They are not just on a garbage heap because they are in an anonymous class.

B. is correct, because the "islands of isolated objects" actually exist because they are referenced in a circular way, and cannot therefore be garbage collected.

Surya said: (Jul 7, 2014)  
Garbage collector is all so collecting of unused memory/referenced memory.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.