Java Native Memory Tracking and Off Heap Usage Explained
Use Native Memory Tracking to see where non heap memory goes: Metaspace, threads, code cache, GC structures and direct buffers, with diffs.

A Java process uses more memory than its heap. Metaspace holds class data, each thread has a stack, the JIT compiler keeps compiled code, the garbage collector has bookkeeping, and NIO buffers sit outside the heap. When a container is killed for exceeding its memory limit while the heap graph looks fine, the missing memory is in these areas. Native Memory Tracking, NMT, is the JVM's own accounting of them. It is built in, needs no extra software, and answers the question that heap dumps cannot: where did the rest of the memory go. This post uses JDK 25.
Turn it on
NMT is off by default, because tracking costs some CPU and memory. Start the process with a flag:
java -XX:NativeMemoryTracking=summary -jar app.jarsummary records totals per category and is the level to use first. detail adds the call sites of allocations and costs more; use it only when the summary has pointed at a category and you need to know who allocates. There is no switching it on in a running process, so enable summary in the environment where the problem appears, accepting the small overhead. Check the documentation for the current cost figure before turning it on in a busy production service.
Read a summary
jcmd <pid> VM.native_memory summaryThe output lists total reserved and committed memory, then each category. The ones to know:
| Category | What it holds |
|---|---|
| Java Heap | The heap you set with -Xmx; the part most people watch |
| Class | Metaspace: class metadata, grows with loaded classes |
| Thread | Thread stacks, about one megabyte each by default |
| Code | JIT-compiled code, bounded by the code cache size |
| GC | Collector data structures such as remembered sets; larger with big heaps and some collectors |
| Compiler | Memory used while compiling, usually temporary |
| Internal and Other | Direct byte buffers and miscellaneous JVM allocations |
| Symbol | Interned symbols and constants |
Committed is what the process has actually claimed from the operating system, and it is the number that approaches your container limit. Reserved is address space set aside, and it can be much larger without costing anything. The total committed figure plus a margin is what the memory limit has to cover, and it is larger than the heap by hundreds of megabytes in a typical service.
Find what grows
A single summary tells you the shape. A leak shows up as growth between two points:
jcmd <pid> VM.native_memory baseline
# wait while the problem develops
jcmd <pid> VM.native_memory summary.diffThe diff lists each category with its change since the baseline. If Thread is growing, something is creating threads and not ending them, which is often an executor created per request and never shut down; see shutting down an ExecutorService. If Class grows steadily, classes are being loaded without being unloaded, a classloader leak that Metaspace growth explains. If Other grows, look at direct buffers.
Direct buffers
ByteBuffer.allocateDirect allocates memory outside the heap, and so do many libraries, notably network frameworks and compression. The JVM caps it with -XX:MaxDirectMemorySize, which defaults to the maximum heap size, so a service can use as much direct memory as heap before the cap applies. Netty keeps its own pools of direct memory and reports them in its metrics. A buffer is freed when its owning object is garbage collected, so a program that allocates many small direct buffers while the heap is quiet may hold a lot of native memory before a collection runs. When Other is the growing category, check the buffer counts through the JMX BufferPool beans or your framework's metrics, and set a cap so the failure is a clear OutOfMemoryError: Direct buffer memory and not a kill by the operating system.
What NMT cannot see
NMT covers what the JVM allocates. It does not include memory that native libraries allocate on their own, such as a JNI library, a database driver with native code, or the C library's allocator holding on to freed memory. If the operating system's figure for the process, the resident set size from /proc/<pid>/status or pmap, is much larger than the NMT total, the difference is outside the JVM's accounting. A well-known cause on Linux is glibc's per-thread allocation arenas, which fragment and hold on to memory; setting MALLOC_ARENA_MAX=2 in the environment often reduces the resident size noticeably. Compare the two numbers before blaming any one library.
Use it with container limits
A container limit has to cover the heap plus everything in the table above. Take the committed total from NMT under realistic load, add headroom, and set the limit from that rather than from the heap alone. The method for choosing -Xmx against a limit is in sizing the heap in a container, and the failure that results from getting it wrong is described in OOMKilled services. Record the NMT summary once at steady state as part of the service's runbook: when memory use later changes, the comparison is immediate.
A worked example: a service killed at 1 GiB
Suppose a service runs with -Xmx512m in a container limited to 1 GiB and keeps being killed, while the heap graph never goes above 400 MB. The arithmetic of a typical process explains it before any tool does: 512 MB of heap, perhaps 100 MB of Metaspace and class data, 60 MB for 60 threads at one megabyte each, 50 MB of code cache and 80 MB of collector structures and other overhead come to about 800 MB, which leaves a narrow margin. Add direct buffers from an HTTP client and the limit is crossed during a traffic spike.
NMT turns those guesses into numbers. Take a summary under load, add up the committed figures, and compare with the limit. If the total is 950 MB, the limit was too tight for this configuration. Raise the limit, or lower the heap so the sum has room, and re-measure. If instead the total stays at 700 MB and the process is killed anyway, the difference is native memory outside the JVM's accounting, and the resident set size tells you by how much. The workflow is the same each time: measure the parts, sum them, compare with the limit, and find the part that moved.
When NMT is not what you need
For the heap itself, use a heap dump. For a suspected leak inside a native library, use the allocator's own profiler or the operating system's tools. For a sudden kill with no growth trend, check for memory limit changes or a co-located process first. The tool is documented in the [jcmd reference](https://docs.oracle.com/en/java/javase/25/docs/specs/man/jcmd.html), and the troubleshooting guide has a chapter on diagnosing native memory leaks.


