Quick revision
The short answer
Native memory tracking only sees memory the JVM allocates itself, so the gap is almost always native code or the allocator: libraries called through JNI or FFM, such as compression codecs, RocksDB or native TLS engines; glibc malloc holding many per-thread arenas and fragmented free memory; Inflater, Deflater and ZipFile objects not closed; and memory-mapped files whose touched pages count as resident. We find it by reading /proc/<pid>/smaps or pmap for large anonymous mappings, comparing them with NMT, and attributing allocations with a native profiler such as jemalloc's.
What to remember
In this answer, we saw that NMT only covers the JVM's own allocations, so a large RSS gap points to native libraries called through JNI or FFM, glibc arenas holding fragmented free memory, unclosed Inflater, Deflater or ZipFile objects, or touched pages of memory-mapped files. We locate the memory with pmap and /proc/<pid>/smaps, attribute it with a native allocation profiler such as jemalloc's, and tell fragmentation from a leak by trimming the heap, changing the allocator and watching whether growth levels off.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Native memory tracking only sees memory the JVM allocates itself, so the gap is almost always native code or the allocator: libraries called through JNI or FFM, such as compression codecs, RocksDB or native TLS engines; glibc malloc holding many per-thread arenas and fragmented free memory; Inflater, Deflater and ZipFile objects not closed; and memory-mapped files whose touched pages count as resident. We find it by reading /proc/<pid>/smaps or pmap for large anonymous mappings, comparing them with NMT, and attributing allocations with a native profiler such as jemalloc's.
Why this question is asked
This is the next step after the basic container memory question. The team has already done the right thing, turned on native memory tracking and found that the JVM's own accounting adds up to, say, 4 GB, yet the process holds 6 GB. Interviewers ask this because the remaining gigabytes are invisible to every Java tool, and finding them needs operating system tools and some knowledge of how native allocators behave. In this answer, we will recap what NMT can and cannot see, go through the usual suspects, find the memory with smaps and pmap, attribute it with a native profiler, and then prove whether it is a leak or fragmentation.
1What NMT Sees and What It Cannot See
NMT instruments the JVM's own allocation paths: the heap, metaspace, the code cache, thread stacks, GC structures, compiler memory, and allocations made through Unsafe or direct ByteBuffers, which show up as Other. Anything that native code allocates by calling malloc itself is invisible to it. That includes JNI libraries such as RocksDB, Snappy, zstd or Netty's native TLS engine, libraries called through the Foreign Function and Memory API that allocate on their side, and the C library used by zlib inside the JDK.
NMT also reports committed memory as the JVM sees it, not how the allocator below it behaves. If glibc keeps freed blocks instead of returning them to the operating system, they are free to NMT but still resident to the kernel.
- Seen by NMTHeap, metaspace, code, threads, GC, direct buffers: about 4 GB here
- malloc by native librariesJNI and FFM code, zlib, RocksDB, TLS engines
- glibc arenas and fragmentationFreed memory kept by the allocator
- Memory-mapped filesTouched pages of
MappedByteBufferand mmap - Kernel view: RSSEverything above, about 6 GB
5 layers, from the top down. Step through them one at a time.
Only the top layer appears in the JVM's own accounting.
2The Usual Suspects
Native libraries are the first suspect, because they allocate freely and their memory never appears in a Java tool. RocksDB block caches and memtables, compression contexts and native TLS sessions can easily hold gigabytes.
glibc malloc is the second. To avoid lock contention, it gives threads separate arenas, up to eight per core on 64-bit Linux, each reserving 64 MB at a time. Freed memory in an arena is kept for reuse and often cannot be returned because of a few live blocks scattered through it. A process with many threads that do native allocation can carry gigabytes of this free but resident memory. Setting MALLOC_ARENA_MAX to 2 or 4, or switching to jemalloc with LD_PRELOAD, often reduces RSS sharply.
The third is unclosed native-backed Java objects. Inflater and Deflater, used by GZIPInputStream, GZIPOutputStream and ZipFile, hold zlib buffers in native memory. If code never calls close() or end(), that memory is freed only when the objects become unreachable and their cleaners run after a GC, which can take a long time.
The last suspect is memory-mapped files. A MappedByteBuffer or a library that uses mmap, such as Lucene, adds the pages it has touched to RSS. Those pages are file-backed and the kernel can drop them under pressure, but they still count in the numbers we look at, so we must tell them apart from anonymous memory.
3Finding the Memory With smaps and pmap
The kernel knows every mapping in the process. pmap and /proc/<pid>/smaps list them with their resident size, so we can sort by size and see what kind of memory dominates.
Notice the pattern: many anonymous mappings of exactly 64 MB are a fingerprint of glibc arenas, while named mappings point to files. Comparing the anonymous total with NMT's committed total tells us how much memory lives outside the JVM's accounting.
4Attributing Allocations With a Native Profiler
Mappings tell us how much, not who. To find which code allocates, we run the process with jemalloc and its profiling turned on. jemalloc samples allocations with their native stack traces and writes profiles we can compare over time.
Library paths and option names vary between distributions and jemalloc versions, so check yours. Simply switching to jemalloc is also a useful experiment on its own, because it handles fragmentation differently from glibc.
5Follow-up questions
How would you prove that the growth is fragmentation rather than a leak?
A leak means native code still holds the memory, while fragmentation means the memory is free but the allocator keeps it. Three tests separate them. First, ask glibc to return free memory: on recent JDKs, jcmd <pid> System.trim_native_heap does this and prints RSS before and after, so a large drop shows the memory was free. Second, run with MALLOC_ARENA_MAX=2 or with jemalloc: fragmentation then shrinks or levels off, while a leak keeps growing at the same rate. Third, watch the shape over time under steady load: fragmentation plateaus once the allocator has enough free blocks to reuse, while a leak grows in proportion to work done, and a jemalloc profile diff points to one allocation site that keeps growing.
Does NMT track memory allocated through the FFM API?
Memory the JVM allocates for an Arena goes through the JVM's own allocation path, so it is accounted for. What NMT cannot see is memory that the native library we call allocates inside its own code with malloc. Check your version's NMT categories for the details.
Is a high RSS from mapped files a problem?
Usually less so, because those pages are file-backed and the kernel can drop them when memory is tight. But in a container they still count towards the memory cgroup, and dirty pages of a writable mapping must be written back first, so they belong in the budget.
6Conclusion
In this answer, we saw that NMT only covers the JVM's own allocations, so a large RSS gap points to native libraries called through JNI or FFM, glibc arenas holding fragmented free memory, unclosed Inflater, Deflater or ZipFile objects, or touched pages of memory-mapped files. We locate the memory with pmap and /proc/<pid>/smaps, attribute it with a native allocation profiler such as jemalloc's, and tell fragmentation from a leak by trimming the heap, changing the allocator and watching whether growth levels off.
Open with
If NMT is well below RSS, the missing memory is outside the JVM's own accounting, so I would think about who else calls malloc. Native libraries used through JNI or FFM, like RocksDB, compression codecs or a native TLS engine, allocate memory NMT never sees. glibc malloc is another big one: it creates many per-thread arenas, up to eight per core, each 64 MB, and keeps freed memory that is fragmented, so it is free to the program but resident to the kernel; MALLOC_ARENA_MAX or switching to jemalloc often cuts RSS a lot.
Then, if they want more
Then there are unclosed Inflater, Deflater, GZIP streams and ZipFile objects, which hold zlib buffers until their cleaners run, and memory-mapped files, whose touched pages count as RSS. To find it, I would run pmap or read /proc/pid/smaps, sort mappings by resident size, and compare the anonymous total with NMT.
Lots of 64 MB anonymous mappings point to malloc arenas, and named mappings point to files. To attribute allocations to code, I would run with jemalloc profiling and diff profiles over time, which shows the native call sites that grow.
And to tell fragmentation from a leak, I would trim the native heap with jcmd System.trim_native_heap, try a smaller arena count, and check whether growth plateaus or keeps rising with load.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Java process reaches 6 GB resident memory with -Xmx3g. Native memory tracking totals...": https://scrivio-interview-library.netlify.app/java/memory-layout/4badc27bc0/