Java Programming - Garbage Collections - Discussion

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


Which statement is true?

[A]. Programs will not run out of memory.
[B]. Objects that will never again be used are eligible for garbage collection.
[C]. Objects that are referred to by other objects will never be garbage collected.
[D]. Objects that can be reached from a live thread will never be garbage collected.

Answer: Option D


Option D is correct.

Option C is wrong. See the note above on Islands of Isolation (An object is eligible for garbage collection when no live thread can access it - even though there might be references to it).

Option B is wrong. "Never again be used" does not mean that there are no more references to the object.

Option A is wrong. Even though Java applications can run out of memory there another answer supplied that is more right.

Alexander Monakhov said: (Sep 9, 2010)  
"Objects that can be reached from a live thread will never be garbage collected."

Very misleading. If the thread nullifies reference to some object, the object becomes eligible for GC. Thus, the statement "will never" is formally wrong.

In contrast, B is right with some addition.
"Objects that are guaranteed not to be used again are eligible for garbage collection."

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.