Everyone knows Java has garbage collection but apparently no one knows how it works so we keep seeing variations of the question "How many objects are eligible for garbage collection?". Here are a few:
- How many objects in this Java code is eligible for Garbage Collection?
- Java job interview. How many objects are eligible for garbage collection?
- How many objects will be eligible for garbage collection after executing "m1=null; m2=null;"?
- How many objects are eligible for garbage collector when we create an array of arrays?
- SCJP Mock Question: How many objects are eligible for garbage collection?
- How many objects will be eligible for garbage collection?
- Objects eligible for garbage collection
- How many objects are eligible for garbage collector in this example
- Interview question: Objects eligible for garbage collection
All of these have different examples that ask about garbage collection of objects of type String
and other custom types, assignment of reference values and null
to local, instance, and class variables, etc.
There is no variation of these concepts (except some edge cases with specific JVM implementations) that doesn't reduce to the same issue. They can all be answered by the question What makes an object eligible for garbage collection? for which I have yet to find a (satisfactory) canonical post. If you have one, please provide a link to it.
What's worse, a lot of the answers contain misleading information. A lot of these have collected considerable upvotes making it hard to signal to others that they are wrong (yes, yes, downvote and comment and Stack Overflow will work out over time).
Is there a use for all of these or should we create a canonical as mentioned above?
I'm aiming to clean these up very soon and I'd like some direction.