1

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?

Holger
  • 285,553
  • 42
  • 434
  • 765
Martin
  • 129
  • 1
  • 6
  • From the "naming" I would guess: the first one is about "processing" newly added references, the second about putting (enqueuing) references into specific "lists". – GhostCat Jul 02 '19 at 07:19
  • My mistake, I wanted to ask what exactly the "processing" is, and what kind of activities happens there. – Martin Jul 02 '19 at 07:30
  • If I would have more than that gut feeling, I would have written an answer already ;-) – GhostCat Jul 02 '19 at 07:45
  • 1
    As said in [this answer](https://stackoverflow.com/a/56687250/2711488): “[Garbage collectors] have to traverse all live references, i.e. strongly reachable objects, and everything not encountered, is garbage per elimination. So when encountering a weak reference during a traversal, it won’t traverse the referent, but remember the reference object. Once it completed the traversal, it will run through all encountered reference objects and see whether the referent has been marked as reachable through a different path. If not, the reference object is cleared and linked for enqueuing.” – Holger Jul 02 '19 at 14:10
  • @Holger thank you for your answer, so as I understand GC use information about reference to treat referenced objects differently than "normal", strong referenced objects. Am I correct? Also, I'm wondering what is the purpose of "enqueuing", and when GC starts to process this queue? – Martin Jul 02 '19 at 17:22
  • 1
    “Enqueuing” means that the reference objects are made available through a [`ReferenceQueue`](https://docs.oracle.com/javase/8/docs/api/java/lang/ref/ReferenceQueue.html) so applications actively using that feature can learn that an object has become unreachable. The garbage collector doesn’t process the queue. – Holger Jul 03 '19 at 08:34

1 Answers1

1

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).

Alexey Ragozin
  • 8,081
  • 1
  • 23
  • 23