File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 18103

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: 9280

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
Don't get me started about those stupid light bulbs.
subject: One object eligible for garbage collection?
Similar Threads
Problem in Garbage collector
Exam lab question on Garbage Collection
How many object is eligible for garbage collection?
Question about Garbage collection from ExamLab diagnostic Test
Garbage Collection Question from Devaka's Exam Labs for SCJP6 - diagnostic exam question number 70.