This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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 Head First Android this week in the Android 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: 19925

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

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

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?
jQuery in Action, 3rd edition