javagarbage-collection

What is an object graph in the Java garbage collector?


Whenever I study about the garbage collector I hear the term "object graph". What does it mean exactly?


Solution

  • Objects have references to other objects which may in turn have references to more objects including the starting object. This creates a graph of objects, useful in reachability analysis. For instance, if the starting object is reachable (say it's in a thread's local stack) then all objects in the graph are reachable and an exact garbage collector cannot harvest any of these objects. Similarly, starting with a set of live objects (roots) if we create a list of all reachable objects, all other objects are garbage - fair game for collection.