Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One object eligible for garbage collection?

 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Marshal
Pie
Posts: 21379
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only one object is eligible for gc here .(original object which was pointed by c initially)

avi sinha
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic