The char arrays account for nearly 70%
of the memory usage(!) whilst the
String field accounts for about 7%
This is subtlety of memory profiling known as "retained size" and "shallow size":
- Shallow size refers to how much memory is taken up by an object, not including any child objects it contains. Basically, this means primitive fields.
- Retained size is the shallow size plus the size of the other objects referred to by the object, but only those other objects which are referred to only by this object (tricky to explain, simple concept).
String is the perfect example. It contains a handful of primitive fields, plus the char[]
. The char[]
accounts for the vast majority of the memory usage. The shallow size of String is very small, but it's retained size is much larger, since that includes the char[]
.
The NetBeans profiler is probably giving you the shallow size, which isn't a very useful figure, but is easy to calculate. The retained size would incorporate the char[]
memory usage into the String
memory usage, but calculating the retained size is computationally expensive, and so profilers won't work that out until explicitly asked to.