Measuring and analysing the Memory consumption in Java and other OOP languages is not an easy task, because all the objects form a big highly connected graph. Only counting the flat, also called “shallow” size, of the objects, like for example the NetBeans profiler does, is not always helpful. If you have to figure out how much memory is spend by the several hundred applications/services running on your application server, you really need a more powerful tool to analyze a complete heap dump.
The question is then, whether there is a better way to measure memory consumption ?
Yes there is !
It’s called “retained size” and it was pioneered (at least as far as I know) by the yourkit profiler, but is now also the main concept that the SAP Memory Analyzer has been released is build around
Definition of Retained Set/Size:
The retained set for a leading set of objects (e.g. all objects of a particular class or all objects of all classes loaded by a particular class loader or simply a bunch of arbitrary objects) is the amount of memory which would be freed if all objects of that leading set would became unreachable, i.e. the retained set includes these objects as well as all other objects only reachable through these objects. The retained size is the total heap size of all objects contained in the retained set.
So in short, the retained size for a set objects is the amount of memory, that would be freed, if those objects could be removed from memory and a full garbage collection cycle would have been run.
The retained set can also be computed for single objects. The SAP Memory Analyzer, has special support for this through the dominator tree.
Definition of Dominator/Dominator Tree:
A node d dominates a node n if every path from the start node to n must go through d. The transformation of the object reference graph into a dominator tree allows us to easily identify the biggest chunks of retained memory.
The dominator tree views you, which are the biggest (in terms of retained size) objects in your heap. It also shows you, why an object is big, by providing a hierarchical view of the objects hold/retained by the object.
So go ahead an give the SAP Memory Analyzer has been released a try ! It’s really a big step forward in memory consumption analysis.