Understanding Incoming and Outgoing References in Memory Analysis

Incoming & Outgoing reference is an important utility to learn when doing Heap Dump analysis. It will help you to understand what objects are accumulating in the memory and who keeps them alive in memory. These are vital facts to diagnose OutOfMemoryError. In this post let’s learn what is an outgoing reference? What is the incoming reference? How to find incoming reference of an object? How to find outgoing reference of an object? 

Sample Java Program

It’s always easier to learn new concepts through an example. So, let’s learn incoming, outgoing references through this simple Java program.

public class ReferenceDemo {

	private static GrandFather grandFather = new GrandFather();

	public static void main(String args[]) throws Exception {
		
		grandFather.sayHello();		
	}
}

public class GrandFather {

	private Father myFather = new Father();
	
	public void sayHello() {

		System.out.println("Hello from GrandFather!");
		myFather.sayHello();
	}	
}

public class Father {

	private Son mySon = new Son();
	
	public void sayHello() {
		
		System.out.println("Hello from Father!");
		mySon.sayHello();		
	}
}

public class Son {

	private GrandSon myGrandSon = new GrandSon();
	
	public void sayHello() {
		
		System.out.println("Hello from Son!");
		myGrandSon.sayHello();
	}		
}

public class GrandSon {

	public void sayHello() {
		
		System.out.println("Hello from GrandSon!");
	}	
}

Please take a moment to read the above Java program before moving forward. This hypothetical program tries to create object reference chain as given below:

a. ReferenceDemo object refers to GrandFather object

b. GrandFather object refers to Father object

c. Father object refers to Son object

d. Son object refers to GrandSon Object.

Object reference chain of the above program can be visualized through this diagram:

Fig: Object Reference Chain

What are Outgoing References?

As the name indicates, Outgoing references show all the object references that are going outside from the object. In other words, it shows the child objects of the current object. For example, the table below shows the outgoing references of each object in the above program.

ObjectOutgoing Reference
Reference DemoGrand Father
Grand FatherFather
FatherSon
SonGrand Son
Grand Son

What are Incoming References?

As the name indicates, Incoming references show all the object references that are pointing to the object. In other words, they are the parent objects of the current object. For example, the table below shows the incoming references of each object.

ObjectIncoming Reference
Reference Demo
Grand FatherReference Demo
FatherGrand Father
SonFather
Grand SonSon

How to find Outgoing References of an Object?

When you capture heap dump from the above program and upload it to memory analyzer tool such as HeapHero, it will parse the dump and generation analysis report. In this analysis report you can see the reference chain between the objects.

Fig: Selecting Incoming, Outgoing Reference in HeapHero Memory Analyzer Tool

Above is the screenshot of the ‘Largest Object’ section in the HeapHero report for the above program. This section reports all the objects in memory sorted by their size. On the right side of each object there is a ‘… more’ icon. Click on this ‘…more’ icon, then select ‘List object(s) with’ menu item from the drop-down menu. Then select ‘outgoing references’ menu item. This will show all the outgoing references of the selected object. Below screenshot shows the outgoing reference of the ‘ReferenceDemo’.

Fig: Outgoing Reference Chain of RefenceDemo Object

From the screenshot you can see the outgoing reference chain as: ‘ReferenceDemo 🡪 GrandFather 🡪 Father 🡪 Son 🡪 GrandSon’. You can also note that tool even displays the variable names which these objects are referenced i.e. ‘myGrandFather, myFather, mySon, myGrandson’.

How to find Incoming Reference of an Object?

Similarly, if you want to see the incoming references of an object, then click on the ‘…more’ icon on the object, select ‘List object(s) with’ menu item. Then select ‘incoming references’ menu item. It will show all the objects pointing to the selected object. Below is the incoming reference chain of the ‘GrandSon’ object: 

Fig: Incoming Reference Chain of GrandSon Object

From the screenshot you can see the incoming reference chain as: ‘GrandSon 🡪 Son 🡪 Father🡪 GrandFather 🡪 ReferenceDemo’. You can also note that the tool even displays the variable names which these objects are referenced i.e. ‘ myGrandson, mySon, myFather , myGrandFather’.

Incoming/Outgoing Reference Comparison

Below table summarizes the key difference between Incoming & Outgoing Reference:

Outgoing ReferencesIncoming References
Purpose in AnalysisIdentify what other objects are retained because of this object — essential for understanding retained size and memory bloat.Identify why an object is not garbage collected — essential for tracing memory leaks and finding leak paths from GC Roots.
DirectionShow who the object itself is pointing to. They represent the children it keeps alive.Show who is pointing to the object. They represent the parents that keep this object alive.
How to Use It?…more > List Object(s) with > Outgoing References…more > List Object(s) with > Incoming References

Conclusion

When you analyze a heap dump, it’s not enough to look at object sizes in isolation. The true story of the memory leak is told through the references:

  • Incoming references reveal why an object is still alive and point you to the leak paths that prevent its collection.
  • Outgoing references reveal what an object is keeping alive and show you the retained set that contributes to memory bloat.

I hope the practical insights about incoming/outgoing references shared in this post will facilitate you to do heap dump analysis more effectively.

Share your Thoughts!

Up ↑

Index

Discover more from HeapHero – Java & Android Heap Dump Analyzer

Subscribe now to keep reading and get access to the full archive.

Continue reading