Search code examples
javareferencegarbage-collectiong1gc

What is Reference Processing in garbage collection process


I'm reading about G1 GC, and there are processes called "Reference Processing" and "Reference Enq" in both Young collection and Concurrent Marking cycle steps. What exactly are those processes? What will happen there?


Solution

  • Reference processing is a phase dedicated for handling special references objects (e.g. weak references, finalizers, JNI references).

    Special references have two important aspects

    • they may have special semantic regarding object reachability (e.g. weak references)
    • reference object may need to be added to reference queue as a consequence of GC work

    During normal GC phase, reference object are queue for post processing.

    "Ref Proc" phase starts after main GC phase, so it is known which objects have survived and which are not, so reference semantic could be applied.

    "Ref Enq" is done afterward to place reference into reference queues (reference queue is a Java object on heap, typically used to implement patterns enabled by special references).