It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes One object eligible for garbage collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "One object eligible for garbage collection?" Watch "One object eligible for garbage collection?" New topic

One object eligible for garbage collection?

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
This question is from the ExamLab Diagnostic Test for SCJP 5.

I drew a sketch of this problem, and I found that only 1 object was eligible for garbage collection. The test answers say that there are 2 objects eligible for garbage collection. Am I right or am I wrong? What do you think?

Henry Wong

Joined: Sep 28, 2004
Posts: 18529

I believe one object becomes eligible at line 10. And one object becomes eligible at line 11.

And BTW, this question has come up before -- just do a search of the forums.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 452

only one object is eligible for gc here .(original object which was pointed by c initially)

avi sinha

SCJP 5.0 SCWCD 5.0
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

As Henry said, two is the right answer. In the line A d=new A().aob=new A(); the new object in italics is eligible for GC. The actual object that is assigned to the reference d is the one in bold. The At this statement the memory map looks like this

At the next statement i.e. at line 11, one more object is eligible for GC

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: One object eligible for garbage collection?
Similar Threads
Problem in Garbage collector
Garbage Collection Question from Devaka's Exam Labs for SCJP6 - diagnostic exam question number 70.
How many object is eligible for garbage collection?
Exam lab question on Garbage Collection
Question about Garbage collection from ExamLab diagnostic Test