Since we don’t have any nice NMT (Native Memory Tracking) MBean in HotSpot (yet), and therefore not in the JMC console, I thought I’d show how it can be done using command line arguments and JCMD. Please note that you’ll get a 5-10% performance hit if you enable this.

Step 1 – Enabling NMT

This is done by using the following command line:

-XX:NativeMemoryTracking=[off|summary|detail]

Where the different options are:

off NMT is turned off. This is the default. summary Only collect memory usage aggregated by subsystem. detail Collect memory usage by individual call sites.

Again, note that enabling this will cause you a 5-10% overhead.

Step 2 – Using JCMD to Access the Data

To use JCMD to dump the data collected thus far, and to optionally compare it to the last baseline, use the following command:

jcmd <pid> VM.native_memory [summary | detail | baseline | summary.diff | detail.diff | shutdown] [scale= KB | MB | GB]

summary Print a summary aggregated by category. detail Print memory usage aggregated by category

Print virtual memory map

Print memory usage aggregated by call site baseline Create a new memory usage snapshot to diff against. summary.diff Print a new summary report against the last baseline. detail.diff Print a new detail report against the last baseline. shutdown Shutdown NMT.

Here is a table with the different memory categories (this may change):



Category Description Java Heap The heap. This is where your objects live. Class Class meta data. Code Generated code. GC Data use by the GC, such as card table. Compiler Memory used by the compiler when generating code. Symbol Symbols. Memory Tracking Memory used by the NMT itself. Pooled Free Chunks Memory used by chunks in the arena chunk pool. Shared space for classes Memory mapped to class data sharing archive. Thread Memory used by threads, including thread data structure, resource area and handle area, etc. Thread stack Thread stack. It is marked as committed memory, but it might not be completely committed by the OS. Internal Memory which does not fit the above categories, such as the memory used by the command line parser, JVMTI, properties, etc. Unknown When memory category can not be determined. Arena: when arena is used as stack or value object

Virtual Memory: when type information has not yet arrived

Additional Options

Use the following NMT options to print a report on VM exit:

-XX:+PrintNMTStatistics -XX:+UnlockDiagnosticVMOptions

NMT will shut itself down when it detects low resource conditions, such as when running low on native memory. Sometimes that may be the situation when you would really want the data! This is the flag to disable auto shutdown:

-XX:-AutoShutdownNMT

Note that detail level information is not supported on ARM.



Final Thoughts

The information in this blog post mostly comes from a wiki document authored by Zhengyu Gu, who also did the HotSpot implementation. Also, shout out to Fredrik Öhrström who came up with the NMT idea, and who did the original JRockit implementation of it!