Keep alive graph

The Keep Alive graph shows how an instance is kept alive on the heap.

The following diagram shows a simplified direct reference graph. Instance 1 references instance 2 and 3, which in turn reference 4 and 5 respectively. However, instance 5 is referenced by instance 2 and 3.

Example 1 ‐ Direct Reference Graph

Example 1 - direct reference graph
Example 1 - direct reference graph

The Keep Alive graph converts the reference graph into a graph in which each instance has exactly one parent. If an instance has multiple referrers, the keep-alive referrer is shifted upwards.

The following figure shows the corresponding Keep Alive graph of the preceding reference graph. The referrer that is keeping instance 5 alive is instance 1. If instance 1 is removed, 2 and 3 are also removed, so that 5 has no more references and can be garbage collected.

Example 1 ‐ Keep Alive Graph

Example 1 - keep alive graph
Example 1 - keep alive graph

Adding another reference to instance 5 gives a slightly different picture. Instance 6 has no referrer,so it is a heap-root instance. It references instance 5. Because there are now two heap-root instances, a virtual root is needed(the <VMRoot> instance). Because instance 5 is referenced by instance 6, it cannot be garbage collected when removing instance 1. Therefore, the keep-alive referrer is the virtual root node (instance 0).

Example 2 ‐ Direct Reference Graph and Keep Alive Graph with Virtual Root Node