Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

GC question from Sun Web Learning...

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:

Give your answer and reason please.
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is D. But there're 2 other references on the object even the rg is set to null.
Are objects referenced only by local variables eligible for GC when the method returned?
[ October 30, 2002: Message edited by: Yan Bai ]
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it!
Once the method completes, all local references are gone. The key to GC issues is to NOT focus on reference counts, but instead focus on one question, "Is the object reachable from a live thread?" Can anyone actually *get* to the object? If no, because the variables that referenced it have all gone completely out of scope at the end of a method, then the object is eligible.
If yes, because a reference to the locally-created object was then stored in an instance variable somewhere, then the object is not elible (unless the object holding the reference variable is ALSO eligible...)
If you are not sure, because a valid reference was returned (as would be the case if rg had not been first set to null) to a calling method, or passed as an argument, then... you cannot be sure.
You will need to recognize all three of those conditions:
1) Definitely eligible (still has reachable references)
2) Definitely NOT eligible (does not have reachable references)
3) Possibly, but not really sure (because you can't tell from the code whether a reference was saved or not after the method completes)
For the exam, it is not any more subtle than that. You do not need to worry about garbage collection and the String literal pool. (You *do* need to know about String literals, but not with respect to GC).
The fun with garbage collection just never stops!
cheers,
Kathy
 
Kathy Sierra
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it!
Once the method completes, all local references are gone. The key to GC issues is to NOT focus on reference counts, but instead focus on one question, "Is the object reachable from a live thread?" Can anyone actually *get* to the object? If no, because the variables that referenced it have all gone completely out of scope at the end of a method, then the object is eligible.
If yes, because a reference to the locally-created object was then stored in an instance variable somewhere, then the object is not elible (unless the object holding the reference variable is ALSO eligible...)
If you are not sure, because a valid reference was returned (as would be the case if rg had not been first set to null) to a calling method, or passed as an argument, then... you cannot be sure.
You will need to recognize all three of those conditions:
1) Definitely eligible (still has reachable references)
2) Definitely NOT eligible (does not have reachable references)
3) Possibly, but not really sure (because you can't tell from the code whether a reference was saved or not after the method completes)
For the exam, it is not any more subtle than that. You do not need to worry about garbage collection and the String literal pool. (You *do* need to know about String literals, but not with respect to GC).
The fun with garbage collection just never stops!
cheers,
Kathy
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kathy, Thanks for clear explanation


1) Definitely eligible (still has reachable references)
2) Definitely NOT eligible (does not have reachable references)


Does eligible means eligible for GC? if so, I think you might want to change them as:
1) Definitely eligible (does not have reachable references)
2) Definitely NOT eligible (still has reachable references)
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic