How do final fields NOT leak memory?

TTT

I have been, without a question, using the final keyword for years to denote fields that should not change for the lifetime of an instance/class. Suddenly this occured to me...

So given this example:

public class TestFinalGC{

    private TestFinalGC(){}

    private final Object obj = new Object();

     public static void main(String []args){
         TestFinalGC instance = new TestFinalGC();
         // instance Ref -> actual instance ->? obj ref-> actual obj
         System.out.println(instance.obj);
         instance = null;
         //say GC makes sweep here... what happens?
         //lets assume theres more code, obj would obviously be eligible for GC on app exit.
     }
}

How does the obj member NOT leak here? Are final fields automatically WeakReferences such that if the strong references to parent(s) are nulled, they are eligible for garbage collection?

The JLS does not seem to note anything special about final

Update:

So this question of mine was founded on the premise that "reachability" and strong/weak references are closely related. There is this confusing oracle doc on reachability that leads me to believe that nested references should always be "strongly reachable". Hence, I do null my nested object references in all my objects, but it appears that this obviously should not be the case from all of the comments I am receiving.

So regarding "reachability", then, is it simply just that nested object references are no longer considered "reachable" if parent references are no longer reachable?

It could be is true that the premise of this problem is incorrect, but there is still intriguing information to consolidate here.

velis

As Makoto suggested, there is simply nothing special about final in variable declarations as far as GC is concerned. In your example code

private final Object obj = new Object();

will be garbage collected at the same time as

private Object obj = new Object();

Both are strong references, but are invalidated and garbage collected together with their parent class TestFinalGC instance. That is because when the instance is GC'd, the reference fields are destroyed as well and the references do not exist any more. obj's reference count thus decreases by one.

However, should you write something like

Object x = myTestFinalGC.obj; // only works if your obj is not private, of course

Then the object will not be garbage collected because it will still have one reference lingering around (assuming this particular line of code is in another class instance that remains alive when myTestFinalGC is garbage collected.

tl;dr: memory allocations are garbage collected when their hard reference count drops to zero (and the collector runs, of course). final doesn't change this fact.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to clone abstract objects with final fields in Java?

From Dev

Do Java compilers commonly precompute hashcodes of final fields?

From Dev

Java Memory Model: Is it safe to create a cyclical reference graph of final instance fields, all assigned within the same thread?

From Dev

How to initialize a circular dependency (final fields referencing each other)?

From Dev

How constructors can initialize final fields with complex algorithm?

From Dev

How hibernate is initializing final fields using the no-arg constructor?

From Dev

How hibernate is initializing final fields using the no-arg constructor?

From Dev

iOS - "Clean memory" or "Free memory" : how to do it

From Dev

How does the linker generate final virtual memory addresses?

From Dev

Cost of using final fields

From Dev

Final fields initialization order

From Dev

Semantics of final Fields in JMM

From Dev

Initialization order of final fields

From Dev

Initialization of final fields

From Dev

How to do fields.property()?

From Dev

Initialization final and not final static fields in static block

From Dev

How do I unset all fields except a known set of fields?

From Dev

How do I unset all fields except a known set of fields?

From Dev

How can I reverse the order that items are added to the fields in final-form/react-final-form-arrays so most recent is first

From Dev

How can I reverse the order that items are added to the fields in final-form/react-final-form-arrays so most recent is first

From Dev

Why are not volatile final fields permitted?

From Dev

Why is publishing final fields safe?

From Dev

Jackson deserialization circumventing final fields

From Dev

Final fields Vs. Volatile

From Dev

static final fields vs TrustFinalNonStaticFields

From Dev

Why are not volatile final fields permitted?

From Dev

Memory for fields of class

From Dev

How do I $concat fields in an array of objects?

From Dev

How do I have two fields for a choice?

From Dev

How do bit-fields work in C?

Related Related

  1. 1

    How to clone abstract objects with final fields in Java?

  2. 2

    Do Java compilers commonly precompute hashcodes of final fields?

  3. 3

    Java Memory Model: Is it safe to create a cyclical reference graph of final instance fields, all assigned within the same thread?

  4. 4

    How to initialize a circular dependency (final fields referencing each other)?

  5. 5

    How constructors can initialize final fields with complex algorithm?

  6. 6

    How hibernate is initializing final fields using the no-arg constructor?

  7. 7

    How hibernate is initializing final fields using the no-arg constructor?

  8. 8

    iOS - "Clean memory" or "Free memory" : how to do it

  9. 9

    How does the linker generate final virtual memory addresses?

  10. 10

    Cost of using final fields

  11. 11

    Final fields initialization order

  12. 12

    Semantics of final Fields in JMM

  13. 13

    Initialization order of final fields

  14. 14

    Initialization of final fields

  15. 15

    How to do fields.property()?

  16. 16

    Initialization final and not final static fields in static block

  17. 17

    How do I unset all fields except a known set of fields?

  18. 18

    How do I unset all fields except a known set of fields?

  19. 19

    How can I reverse the order that items are added to the fields in final-form/react-final-form-arrays so most recent is first

  20. 20

    How can I reverse the order that items are added to the fields in final-form/react-final-form-arrays so most recent is first

  21. 21

    Why are not volatile final fields permitted?

  22. 22

    Why is publishing final fields safe?

  23. 23

    Jackson deserialization circumventing final fields

  24. 24

    Final fields Vs. Volatile

  25. 25

    static final fields vs TrustFinalNonStaticFields

  26. 26

    Why are not volatile final fields permitted?

  27. 27

    Memory for fields of class

  28. 28

    How do I $concat fields in an array of objects?

  29. 29

    How do I have two fields for a choice?

  30. 30

    How do bit-fields work in C?

HotTag

Archive