tags:

views:

600

answers:

4

Whenever I study Garbage Collector I hear the term object Graph. What does it mean exactly?

+2  A: 
Object graph is basically a dependency graph between objects

It is used to determine which objects are reachable and which not, so that allunreachable objects could be made eligible for garbage collection.

Suraj Chandran
why would someone give this answer a vote :)
Suraj Chandran
I don't know. I was inclined to downvote because you described the reachability graph as a dependency graph :-)
Stephen C
+1  A: 

An 'Object Graph' is the conceptualization of all instances of the objects from your object model (the classes in your program) and their interconnections.

Take for example:

If you have two classes

Class Foo
{
String aString = "foo";
Bar aBar;
}

Class Bar
{
String aString = "boo";
}

If you were to create an instance, myFoo, of class Foo and then create an instance of Bar, and connect them, myFoo.aBar = myBar;, your object graph would consist of... a single instance of Foo with a reference to a single instance of Bar.

The garbage collector essentially uses the object graph to determine which instances in memory are still linked to something and possibly needed by the program, and which instances are no longer accessible and therefore can be deleted.


Someone on wikipedia puts it more eloquently than me:

Object-oriented applications contain complex webs of interrelated objects. Objects are linked to each other by one object either owning or containing another object or holding a reference to another object. This web of objects is called an object graph and it is the more abstract structure that can be used in discussing an application's state.

instanceofTom
+4  A: 

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 any harvest 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.

Chandra Patni
Re "not all": I think, all objects have references to other objects, at the very least a reference to their class.
Thilo
@Thilo: Good point. I have removed "not all".
Chandra Patni
maybe primitive type arrays don't...
Thilo
+1  A: 

What we are talking about is the mathematical notion of a directed graph that consists of nodes and edges that connect the nodes. An object graph is some graph whose nodes are objects, and whose edges are relationship of interest between the objects.

In the case of the Java garbage collector, the object graph of concern is the graph of reachable objects. In this graph, the nodes are Java objects, and the edges are the explicit or implied references that allow a running program to "reach" other objects from a given one. (For example of an implied reference, there is an implied reference from an object to it's Class object, and hence to the heap objects holding the classes statics and its code ... but I digress.)

As @Chandra Patni explained, garbage collection works by traversing the reachability graph consisting of all objects that can be reached from one of a set of starting points; the "root set" in GC terminology. Any object that is not found in this graph traversal can no longer influence the computation, and is therefore eligible to be garbage collected.

Stephen C