Search code examples
javadata-structuresgarbage-collectiongarbagemark-and-sweep

Common data structures used in java Garbage Collection techniques


I have come across the following question multiple times:

What data structures are used in garbage collection?

I haven't found many resources about the data structures used in GC algorithms.

Edit: I understand that the question seems too broad since there are different kinds of garbage collection techniques. We could go with the commonly used garbage collection algorithms, like the ones found in most popular JVMs.


Solution

  • Your question is rather like asking "how does an operating system work?" There are many different algorithms for GC and they use different internal data structures depending on how the algorithm works.

    Many algorithms use a root set as a starting point. This is a list of all the objects directly accessible from your application threads. It is created by scanning the thread stacks, registers, static variables, etc. The GC will typically process the root set to follow links to other objects (that are therefore accessible) and build a graph of all accessible objects.

    There are other data structures like card tables but these are not used in all algorithms.

    You might want to pick a particular GC algorithm and study that.