• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

One object eligible for garbage collection?

 
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?


 
author
Posts: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
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
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!