• 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
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Noob question on Garbage Collection eligibility

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The book provided 2 situations when an object is eligible for garbage collection when they are no longer reachable:

1. The object no longer has any references pointing to it
2. All references to the object have gone out of scope

I understand the first one. But can someone provide me an example of the 2nd one?

Thanks!
 
Marshal
Posts: 71047
291
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which book? Is it one of the Java11 cert exams revision books by Boyarsky and Selikoff?

You only really need to know eligibility for garbage collection (=GC) if you are revising for a cert exam or you are designing heap memory.That Bar instance in eligible for GC whenever that reference is set to null, or the Foo instance it is associated with is set to null or goes out of scope or is otherwise unreachable, and all other references to it elsewhere are set to null or go out of scope.At line 7½, b has gone out of scope and is ∴ no longer reachable. Does anybody know whether escape analysis is in scope for a cert exam? The b reference is not GC‑ed because escape analysis (Java6+)  allows the JVM to “know” that b is not used outside that method, so it is created on the stack rather than the heap.The JVM here “knows” that b is used in more than one method; the object is ∴ created on the heap. The reference is not retained by println(), and when it goes out of scope at line 8½, it is eligible for GC.
 
Cosmid Constantine
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ritchie! Yes, it is the Java11 cert exam books by Boyarsky and Selikoff. I have the three in one version, 1Z0-815, 816, and 817. Maybe the contents are compressed and some of it isn't explained very well. I'm glad the book pointed out this website and there are good people like you to help me out!
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cosmid Constantine wrote:Thanks

That's a pleasure

. . . good people like you to help me out!

Careful; you'll get me al embarrassed.
 
Master Rancher
Posts: 3719
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You only really need to know eligibility for garbage collection (=GC) if you are revising for a cert exam or you are designing heap memory.


Or if you're, say, trying to understand why your program keeps crashing with OutOfMemoryError because you've created a memory leak somewhere.
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What use is knowing GC in those circumstances? You need to know how you retained unnecessary references to too many things.
 
Mike Simmons
Master Rancher
Posts: 3719
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What use is knowing GC in those circumstances?



You seem to have replaced "eligibility for GC" with "GC".  I'm not talking about other GC topics, just the basic question of whether a given object is eligible or not, and why.

Campbell Ritchie wrote:You need to know how you retained unnecessary references to too many things.



Which seems to support my point.  Admittedly, understanding GC eligibility is more of a prerequisite - most of the work tends to be in actually finding the chain of references that is preventing GC of a big blob of objects.  But the essence of why that's important, that depends on understanding what makes an object eligible for GC, or not.

I've also seen people waste time making local variables null just before they go out of scope, or removing elements from a collection when they could just let the collection itself becomes eligible for GC.  Sometimes they might need to consider using a WeakHashMap instead of another Map.  These are all things where a good understanding of GC eligibility can help you fix memory problems.
 
Campbell Ritchie
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right there. MS.
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic