5

I am trying to measure my application memory foot print pragmatically. I am using java.lang.management class to calculate this

val heap = ManagementFactory.getMemoryMXBean.getHeapMemoryUsage
val nonHeap = ManagementFactory.getMemoryMXBean.getNonHeapMemoryUsage
val total = heap + nonHeap + (?)

I assumed the sum of both will give me the total amount of memory used by application, but this is not the case, the actual size is greater which was provided by top command.

So I am trying to understand what am I missing? What else do I need to add to this equation in order to get the total memory usage of my application.

Tomer
  • 2,398
  • 1
  • 23
  • 31
  • 1
    *'the actual size is greater'* - How did you find the "actual" size then? – apangin Feb 03 '16 at 22:17
  • Use the way you found the actual size was greater. – Peter Lawrey Feb 03 '16 at 23:43
  • @apangin The actual size was obtain via top command. I updated my question. – Tomer Feb 04 '16 at 16:00
  • Which value did you use? Since other processes have no idea about Java objects, they won’t tell you the *used* size, but rather *committed* or even *max* size. See [`MemoryUsage`](https://docs.oracle.com/javase/8/docs/api/?java/lang/management/MemoryUsage.html) – Holger Feb 05 '16 at 13:21

1 Answers1

14

To find the memory usage as provided by top, check the OS-level statistics for the process. On Linux you can do this by reading /proc/self/stat or /proc/self/status. More about proc pseudo-file system.

Note that Application footprint is a different concept. From JVM point of view Java application footprint is roughly the amount of space occupied by Java objects (Heap) and Java classes (Non-heap). From OS point of view there are much more things to count, including JVM itself and all the components of Java Runtime that make your application work.

The memory used by the whole Java process include

  • Java Heap;
  • Metaspace (for class metadata);
  • Code Cache (the place for JIT-compiled methods and all the generated code);
  • Direct ByteBuffers;
  • Memory-mapped files, including files mapped by JVM, e.g. all JAR files on the classpath;
  • Thread stacks;
  • JVM code itself and all the dynamic libraries loaded by Java Runtime;
  • Many other internal JVM structures.
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Thanks! is there a way for the jvm the get hold of more information than the heap and non heap memory reported by the ManagementFactory? – Tomer Feb 07 '16 at 09:04