Backgrounds of heap size measurements
Hello,
after building a JVMTI agent for measuring the size of the java heap (in a first step) I found out about many other ways of measuring the heap. Initially all of them seem to be contradictive to my agent, but step by step I brought some of them in harmony.
My agents is interested in measuring the size of live-objects, thats why it is based on iterateOverReachableObjects().
Currently I have my agents output values and understood the differences which occur on comparison with some of the other techniques.
Those measurements are understood and are no more concadictive:
- JVMTI demo HeapViewer in JDK 1.5
- JVM option -XX:+PrintClassHistogram
- Using -agentlib:hprof=heap=sites to get a heap dump and using jhat to analyse it (which among other things give a total size)
What remains unexplained and contradictive (to my agent and the other techniques) is the output of the following techniques, which ALL give the same values. The values of the following unexplained techniques are always less than the values from the explained techniques above.
- using jstat (formerly jvmstat) and summing up Survivor, Eden and Old Usage
- from inside a java program calling: heapusage = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
- from inside a java programm too calling: heapusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() [which seems equally to the technique above - maybe this bean is based on those Runtime values?]
What I am interested in is why it is that way? What is measured by JVMTI and the other techniques and remains unmeasured by the unexplained techniques? Is it some JVM heap interna, may be something constant pool related or is there another reason for the different values?
Robert

