0

I recently started to study garbage collectors (GCs) used in languages like Java.
I am particularly interested in tracing (or mark-and-sweep) GCs like G1 (garbage-first).

I am confused with some details in an object-graph traversal performed to mark reachable objects in the mark phase.

From my understanding, starting from roots, the G1 GC scan the heap memory of a visited object and find reference variables (stored in its region) to find other objects to visit (rather than maintaining the actual object graph at runtime and traversing it).

Regarding heap memory scan, how does the G1 GC determine which memory location in an object stores a reference variable?

For example, given an object A, it could contain N reference variables as class members. For the object A, how does the GC know how many reference variables are there and at which offset they are stored?

I am aware of techniques using remembered sets, card tables, etc.
From my understanding, those are all for optimization.
I am specifically curious about memory scanning.

Please do not just say "the GC stores some metadata to figure out those information".
I hope to hear about the actual (or detailed) implementations.

Community
  • 1
  • 1
yonghae
  • 51
  • 5
  • 3
    I know you want specifics, but why do you assume that the GC can't just access the class definitions just like the rest of the JVM does? That contains plenty of information to figure out which fields are references. – Joachim Sauer Jan 15 '23 at 01:38
  • 1
    See https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html and https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.5 and https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3.2 – tgdavies Jan 15 '23 at 01:39
  • 1
    A language designed from the start to support garbage collection incorporates mechanisms to support garbage collection, – undefined symbol Jan 15 '23 at 02:07
  • Thank you for all the comments, which were very helpful to me. – yonghae Jan 16 '23 at 17:13

1 Answers1

2

The definition of the class of the object A specifies which fields are references. (And the object A contains, in an always predictable location -- the object header -- a reference to its class that holds that information.) That's really the whole answer.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413