• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic