15

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:

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.

10
  • 1
    Perhaps you should avoid creating new canonicals and instead add a topic to SO/docs covering this.
    – bwoebi
    Aug 3, 2016 at 21:45
  • 7
    @bwoebi Until we can use a doc as a duplicate target, that will have minimal effect, imo. Aug 3, 2016 at 21:48
  • Uhhh, let's just assume the devs will add this in a short timeframe…
    – bwoebi
    Aug 3, 2016 at 21:52
  • 4
    My personal beef with those questions, is that they don't really make any sense. Even reasoning on scope can lead to trouble and objects can be garbage collected even if referenced by local var. The canonical would have to cover a lot of aspects.
    – Tunaki
    Aug 3, 2016 at 21:55
  • 1
    @Tunaki That's the misleading part. Yeah, the canonical would have to explain that scope is a compile time concept. Shall we make a list of what should be covered? Aug 3, 2016 at 21:57
  • Perhaps create a canonical in Java that covers "what does it mean an object is eligible for garbage collection?" It is possible given enough system resource that the gc never runs for small sets of memory use.
    – Travis J
    Aug 3, 2016 at 21:57
  • @SotiriosDelimanolis Sure, we can make a community-wiki answer listing things to consider. I can't find a proper canonical out there either.
    – Tunaki
    Aug 3, 2016 at 22:10
  • 5
    typo in title, should read "How many “How many objects are eligible for garbage collection?” questions are eligible for garbage collection?"
    – gnat
    Aug 3, 2016 at 22:49
  • @Tunaki Add below? I can't find something in the specs that clearly states that a finalizer can only be run on objects that are not reachable. I need a break. Aug 3, 2016 at 23:23
  • 7
    @gnat I feel like someone posts one of these on meta every day. The question we should really be asking ourselves is, How many "How many 'How many objects are eligible for garbage collection?' questions are eligible for garbage collection?" questions are eligible for garbage collection?
    – jtbandes
    Aug 4, 2016 at 5:39

1 Answer 1

12

A canonical should include a description of the following (please add what you think is relevant):

  • JVMS details about the Heap

    The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java Virtual Machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements.

  • Finalization in the JLS

    • Before the storage for an object is reclaimed by the garbage collector, the Java Virtual Machine will invoke the finalizer of that object.
    • The Java programming language does not specify how soon a finalizer will be invoked, except to say that it will happen before the storage for the object is reused.
    • A reachable object is any object that can be accessed in any potential continuing computation from any live thread.

More entries in JLS

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .