One of the most important parts of a JXRay report is the “Where memory goes, by class” section. This table gives you the number of instances for each class, and how much memory they use. However, instead of one “memory” column, there are three: Shallow size, Implementation-inclusive size and Retained memory. What’s the difference between them?
Shallow size is simple: that’s just how much memory all instances of the given class use, period. If you have 10 instances of class
Foo, and each takes 32 bytes, this column will be 320b. All other memory analysis tools will give you the same number.
However, what if you are looking at the line for class
String? Each instance of
String has fixed size, most typically 24 bytes. That (times the number of objects) is what you will see in the “Shallow size” column. But in the “Impl-incl. size”, there is a different, bigger number. Why is that?
That’s because in this case, as in many others, JXRay gives you both a “formal” and a “practical” answer. Each
String object references a
char array, where the string contents are contained. Memory used by these arrays, plus the memory used by
String objects themselves, is the Implementation-inclusive memory reported by JXRay. And most of the time, that number is what you really need to estimate the memory footprint of strings.
Impl-inclusive size also differs from shallow size for collections, such as
HashMap. That’s because internally each collection object points to one or more implementation objects. For example, an
ArrayList references an
Object array that contains the actual list contents. Thus the impl-inclusive size of an
ArrayList reported by JXRay is the sum of sizes of these two objects. For a
HashMap this is even more complex, because for each key-value pair there is a separate internal
HashMap$Node object. Thus very often the implementation-inclusive size of a collection (that does not include the size of its contents!) is much bigger than its shallow size. And again, collection footprint measured in this way is usually the most relevant in practice.
You will find that similar “practically important vs formal” approach is used in many other parts of JXRay reports. We hope you will find it useful, but if something is unclear, please don’t hesitate to send us your feedback.