This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Spark in Action and have Jean-Georges Perrin on-line!
See this thread for details.
Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Chapter 2 practice Test (McGraw-Hill)

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about chapter 2 #46 on page 29 of the OCA/OCP Java SE 8 Programmer Practice Tests book.

Here is the code:


The question asks: how many objects are eligible for garbage collection right before the end of the main method?

Can someone explain how many objects are eligible for gc and when they become eligible?

Thanks!


 
Ranch Hand
Posts: 117
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object becomes eligible for garbage collection when there is no reference pointing to it anymore.
In your example, you have created 3 objects inside the main method, but the code is passing the references between the reference variables.
Let's see your code in details:

At line 5, you create a new Person object and assign it to the reference variable elena (it is a local variable).
At line 6, you create another Person object and assign it to the reference variable diana (another local variable).
At line 7, you are passing the reference of the second object created (pointed by the variable diana) to the instance variable youngestChild of the object pointed by the variable elena. Now, you have two variables pointing to the same object.
At line 8 you are assigning null to the variable diana. This means that now diana points to null, and the object created at line 6 is only referenced by the instance variable youngestChild of elena (elena.youngestChild). Thus, the object still has a reference, and it won't be eligible for garbage collection yet.
At line 9, you are creating a new Person object, referenced by the reference variable zoe.
At line 10, you are assigning another reference to elena.youngestChild, so it will point to the same object pointed by the variable zoe. Now, remember that elena.youngestChild was pointing to the object created at line 6, and it was the only reference that was pointing to this object, since at line 8 you eliminated the other reference to the same object. This means that the object created at line 6 has no reference to it anymore, and after line 10, it will be eligible for garbage collection.
At line 11, you are assigning null to the variable zoe, but the object that was referenced by this variable is still being referenced by another variable (look at line 10 again), which means that it will not be eligible for garbage collection before the main() method ends.

Concluding, only the object created at line 6 will be eligible for garbage collection right before the main() method ends.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

AngelinaJeana Brown wrote:Can someone explain how many objects are eligible for gc and when they become eligible?


And for 1 bonus point: how many objects will be eligible for GC if youngestChild is a class variable instead of an instance variable?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

AngelinaJeana Brown wrote:Can someone explain how many objects are eligible for gc and when they become eligible?


When objects become eligible for Garbage Collection is one of the most popular (and sometimes hard to understand) topics in this forum, so if you use the search function you'll find plenty of (excellent) topics about this topic. Here are a few:
  • How many objects eligible for garbage collection? (K&B7, chapter 3, self test question 11)
  • Chapter 3 Of OCA/OCP Java SE 7 Programmer I & II Study Guide, need help on question 11
  • Doubt about CH3 Q11: how many objects eligible for garbage collection? (K&B7)
  • doubt on question #6 self test chapter 5 book and cd [K&B7]
  •  
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    AngelinaJeana Brown wrote:Can someone explain how many objects are eligible for gc and when they become eligible?


    And this topic has also an excellent explanation (with some code snippets) about when objects become eligible for GC. Definitely worth reading!
     
    AngelinaJeana Brown
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    And for 1 bonus point: how many objects will be eligible for GC if youngestChild is a class variable instead of an instance variable?



    Class variables live the entire life of the program, so won't be GC. Correct?
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    AngelinaJeana Brown wrote:Class variables live the entire life of the program, so won't be GC. Correct?


    No, that's not correct! A class reference variable can be nullified just like an instance or local reference variable.
     
      Bookmark Topic Watch Topic
    • New Topic