• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects eligible for garbage collection? (K&B7, chapter 3, self test question 11)

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given this code:


The exercise asks you to determine how many objects will be eligible for garbage collection and the answer is 1, but I can not understand why.
I would have answered 3, namely a1, b1 and b2, as the reference are set to null.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:The exercise asks you to determine how many objects will be eligible for garbage collection and the answer is 1, but I can not understand why.
I would have answered 3, namely a1, b1 and b2, as the reference are set to null.

Before you can answer this question correctly, you need to know when an object becomes eligible for garbage collection (GC). Can you explain/tell us the general rule when an object is eligible for GC?
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object becomes eligible for garbage collection when there is no thread that uses it, then we say that a reference that no longer points to a specific purpose and that is set to null indicates the possibility that the object pointed previously is eligible for the garbage collection.
The speech changes when there are more reference they are associated with the objects and that refer to each other, in that case, even if only one of these reference is NOT set to null "protects" the other objects from being deleted by the garbage collector.
This is what I understood, I hope not to have been wrong.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:An object becomes eligible for garbage collection when there is no thread that uses it, then we say that a reference that no longer points to a specific purpose and that is set to null indicates the possibility that the object pointed previously is eligible for the garbage collection.
The speech changes when there are more reference they are associated with the objects and that refer to each other, in that case, even if only one of these reference is NOT set to null "protects" the other objects from being deleted by the garbage collector.
This is what I understood, I hope not to have been wrong.

You are correct! But the general rule can be formulated a lot simpler Let me give a try!

When an object can't be referenced anymore (from a live thread), the object is considered to be eligible for GC. The simplest example to illustrate:So because on line2 the reference variable o is set to null, the object created on line1 is not accessible anymore and thus eligible for GC. So rule of thumb: when an object is no longer referenced by at least one reference variable, the object is eligible for GC.

Now, in order to correctly answer this kind of question it's not enough to just count the = null statements. That's way too easy. The best approach is to make a little drawing of each line. You'll can find an example in this thread. And I'm pretty sure that thread will also clear all your doubts about this question, because it's about exactly the same question

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic