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

how many objects are eligible for GC

 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Someone did post same post but I am not getting why c3 is not eligible for GC. I think c3 refers to c2.As soon as cb=null, c2 is null & c3=null.
Why Short wrapper object is eligible?

Can anyone please explain?
 
Yeming Hu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is eligible for GC is object not reference variables. c3 has never point to an object. There are two objects of CardBoard type, each of which have a reference to a Short object. Therefore, There are 4 objects. c2 is not assigned to null although cb is because cb only have c2's value when c1.go(c2) is called. cb and c2 are defined in different methods. At last, there are only two objects are eligible for GC, one object of type CardBorad referenced by c1 originally (now c1 is null) and the other object of type Short referenced by the former object. These two objects are isolated island now
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yeming Hu:
At last, there are only two objects are eligible for GC, one object of type CardBorad referenced by c1 originally (now c1 is null) and the other object of type Short referenced by the former object. These two objects are isolated island now
No. In this sample there is only one object eligible for garbage collection. For details have a look here.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dolly shah:
As soon as cb=null, c2 is null
When you change cb, c2 does not change, since cb is a copy of c2. Have a look at Pass-by-Value Please. It may help you to get the concept.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You knew there is a thread already exisiting, it would have been better if you had continued there itself.
You tell us your understanding of what you know and what you expect the code or run, we can proceed ahead to get you along.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Akhilesh Trivedi:
You knew there is a thread already exisiting, it would have been better if you had continued there itself.
You tell us your understanding of what you know and what you expect the code or run, we can proceed ahead to get you along.


Yes, thats right. Many repeated-but-seems-like-duplicate threads are unnecessarily created and they can be avoided in that way!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic