Java Programming - Garbage Collections - Discussion

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


Which statement is true?

[A]. Calling Runtime.gc() will cause eligible objects to be garbage collected.
[B]. The garbage collector uses a mark and sweep algorithm.
[C]. If an object can be accessed from a live thread, it can't be garbage collected.
[D]. If object 1 refers to object 2, then object 2 can't be garbage collected.

Answer: Option C


This is a great way to think about when objects can be garbage collected.

Option A and B assume guarantees that the garbage collector never makes.

Option D is wrong because of the now famous islands of isolation scenario.

Anitha said: (Apr 15, 2011)  
What is live thread?

Pramod Kumar said: (Jul 11, 2011)  
Live thread means the program that is being executed currently

Hareendra said: (Sep 3, 2011)  
How is option (d) incorrect !

They did not specify whether object1 has no live reference.

Santosh said: (Mar 23, 2012)  
What is live thread is that working concurrently ?

Gayathri said: (Jan 24, 2013)  
Why not (1) option because we have two methods for calling garbage collector explicitly 1.Runtime.gc() 2.System.gc()

Why not (2) option .. garbage collector uses Mark and Sweep algorithms.

Rajesh said: (Sep 20, 2013)  
Why not option(B).

The basic garbage collector uses Mark and Sweep algorithms.

Laughingpig said: (Oct 1, 2013)  
This answer is wrong. If an object X is only referenced by a WeakReference, then that object X is eligible for garbage collection when memory runs low even if the live thread has access to the WeakReference.

Gody said: (Nov 18, 2013)  
Seems like all are correct. Why C why not all are?

Kinjal said: (Jan 6, 2014)  
The mark-and-sweep algorithm was the first garbage collection algorithm to be developed that is able to reclaim cyclic data structures. So answer b can be possible too.

Ganesh Mane said: (Mar 16, 2015)  
The Garbage collector uses mark and sweep method also true.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.