• 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
Sheriff
Posts: 22635
111
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: 9597
36
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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!