• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

One object eligible for garbage collection?

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic