Since Native Memory Tracking can only be enabled when a HotSpot JVM starts, would you turn on its summary mode by default across a fleet of containerised JVM services, and what would you weigh?
answer
- evidence you cannot add later
- a header on every allocation
- summary everywhere, detail on demand
- per-category numbers in JFR
basics
~20 sSummary-mode NMT usually belongs on by default, after measuring its cost: it cannot be enabled mid-incident, and restarting to add it resets a slow leak. Keep detail mode for reproductions and watch RSS too, since NMT sees only JVM-owned memory.
solid answer
~50 sI lean towards summary mode everywhere, decided per service on measurement rather than by reflex. The argument for: NMT is startup-only, so when a pod's memory has crept for two days, a restart to enable it throws away exactly the growth you needed to see; with it on, `jcmd <pid> VM.native_memory summary.diff` works at any time, and JFR's `jdk.NativeMemoryUsage` and `jdk.NativeMemoryUsageTotal` events, on at a 1 s period in the default settings, turn its categories into a time series in continuous recordings. The cost: every JVM `malloc` carries a 16-byte header and a 2-byte canary plus counter updates, so allocation-heavy services should be benchmarked with and without it. Detail mode, which captures a call stack per allocation, stays for a reproduction instance. And I would not let it become the only memory signal, since it cannot see native libraries' own `malloc`.
code
bash · 6 lines# fleet default in the service's JVM options
JAVA_TOOL_OPTIONS="-XX:NativeMemoryTracking=summary"
# the same evidence later, without a restart
jcmd <pid> VM.native_memory baseline
jcmd <pid> VM.native_memory summary.diff scale=MBgo deeper
Recall that NMT is chosen on the command line at startup and cannot be added to a running JVM.
Explain what summary mode costs per allocation and why detail mode costs more because it records call stacks.
Show why having NMT on before an incident changes the diagnosis of a slow native leak, and what NMT still misses.
Own the fleet default: summary on by measurement, detail on demand, NMT data in continuous recordings, and resident-memory signals beside it to cover its blind spot.
## The decision **Native Memory Tracking (NMT)** is HotSpot's accounting of the native memory the JVM allocates for itself, split into categories such as Thread, Code, Class and Other. It is **off by default** and is read only at startup from `-XX:NativeMemoryTracking=summary` or `=detail`. That single property turns a tooling detail into a standing decision for whoever owns the fleet's JVM defaults: the evidence exists during an incident only if someone chose it before the incident. There is no single right answer; the honest position is a default plus the conditions that override it. ## The case for summary mode everywhere - **A slow leak cannot be diagnosed after the fact.** A container whose resident memory climbs for two days before it is killed gives you one chance: the running process. Restarting it to add NMT resets the growth curve and the leak has to be waited out again. - **Diffs on demand.** With NMT on, `jcmd <pid> VM.native_memory baseline` and later `summary.diff` work on any instance at any time, without a redeploy. - **A time series at little extra cost.** JFR's `jdk.NativeMemoryUsage` (per memory type) and `jdk.NativeMemoryUsageTotal` events carry reserved and committed bytes and are enabled at a one-second period in the default recording settings. They report NMT's numbers, so they only have something to say when NMT is on. A team that already runs a continuous recording gets per-category history alongside GC and allocation data. - **Guard rails become possible.** NMT-dependent options such as the diagnostic `MallocLimit` can end the VM or simulate a native out-of-memory error when a subsystem passes a limit, though few teams need them. ## The costs to weigh | Aspect | Summary mode | Detail mode | |---|---|---| | Per-allocation cost | 16-byte header, 2-byte canary, counter updates | the same plus a captured native call stack | | Extra memory | headers and NMT's own category | plus the call-site tables | | What it attributes | categories | categories and call sites | | Sensible scope | fleet default, after measurement | one reproduction instance | The numbers that matter are your own. A service that makes many small JVM-internal allocations on its hot path should be benchmarked with and without NMT before the default is imposed. NMT's own footprint is visible in its report as the Native Memory Tracking category and its tracking overhead line, which makes the memory side easy to check on a canary. A fair measurement runs the same load against two otherwise identical instances, one with the flag and one without, and compares: - throughput and tail latency on the hot path; - resident memory after warm-up; - the size of NMT's own category on the instance that has it. ## What NMT does not buy A fleet default must not create false confidence: 1. **NMT sees only the JVM's own allocations.** JNI and other native libraries calling `malloc` directly, and memory glibc keeps after a free, are invisible. A pod can be killed with every NMT category flat. 2. **NMT does not measure residency.** Its committed figures say what the JVM holds, not what is in RAM, so the container's resident memory needs its own graph. 3. **Detail is still a deliberate step.** Summary mode names the subsystem, not the call site. So the standing setup is a set of signals, not one flag: - NMT summary mode on by default, or on a canary subset where measurement says the cost is too high. - Resident memory per container, split into anonymous and file-backed where possible, graphed beside NMT's committed total. - A documented, rehearsed way to run async-profiler's `nativemem` mode on demand for allocations outside NMT. - Detail mode reserved for an instance started specifically to reproduce a growth. ## How a lead would phrase the policy A defensible written policy reads something like: every JVM service starts with NMT in summary mode unless a benchmark shows a cost the service owner cannot accept, in which case a canary subset keeps it; continuous recordings keep the native memory events; and the runbook for a container memory kill starts by comparing NMT's diff with resident memory before any flag is changed. The policy is reviewed when the JDK changes how NMT works or when a service's allocation profile changes, because the cost side of the trade is workload-specific.
- What would make you leave NMT off for a particular service?A measured cost that matters: a service whose hot path makes very many small JVM-internal allocations, where the per-allocation header and counter updates show up in throughput or footprint benchmarks. Then I would keep it on a canary subset of that service, so a baseline-and-diff is still possible on some instances, rather than leave the whole service blind.
- Why not run detail mode everywhere and get call sites for free when something leaks?Detail mode is not free: it walks and stores a native call stack for each tracked allocation, which costs noticeably more CPU and memory than summary. It is better started deliberately on one instance that reproduces the growth, where the call-site attribution of `detail.diff` justifies the overhead.
- Which signals would you keep beside NMT so its blind spot does not bite?The resident set per container, preferably split into anonymous and file-backed memory, graphed beside NMT's committed total so a widening gap is visible; and a documented way to run async-profiler's `nativemem` mode on demand, because native libraries' direct `malloc` calls and C-library retention never appear in NMT.
- NMT can be attached later with jcmd when an incident starts.
- Summary mode adds no overhead, so there is nothing to measure.
- With NMT on everywhere, native memory incidents are fully covered.
- Detail mode should be the fleet default because it shows call sites.
- Restarting a leaking pod to add NMT preserves the evidence of the leak.