Java Off-Heap / Direct Memory — Tracking and Bounding It
Reviewed by Gurusankar M. · Updated Aug 24, 2026
Short Answer
Direct (off-heap) memory is native memory outside the GC heap, allocated via DirectByteBuffer / Unsafe (used by NIO, Netty, caches). It avoids copy/GC overhead for I/O but isn't reclaimed by normal GC — bound it with -XX:MaxDirectMemorySize and watch it via Native Memory Tracking.
On this page
Concept Made Simple
Coffee Chat Question
“What is off-heap / direct memory, and how do you track and bound it?”
Remember It Faster
Mind Map Answer
- Direct buffer
- native memory, GC-free I/O
- Freed when
- buffer's Cleaner runs (GC-triggered)
- Bound
- -XX:MaxDirectMemorySize
- Track
- Native Memory Tracking (NMT)
Think Beyond the Expected
What If?
Container RSS keeps climbing but heap is flat and GC is healthy — prime suspect?
Off-heap/native growth: leaking DirectByteBuffers (Netty/NIO), mmapped files, or thread stacks. Heap metrics won't show it. Enable Native Memory Tracking (-XX:NativeMemoryTracking) and inspect with jcmd VM.native_memory to find the growing category.
Real World
Native-memory growth with a flat heap is a notoriously hard production problem (often Netty direct buffers); NMT and jcmd are the tools that make off-heap usage visible.
Interviewer's Expectation
Keywords they're listening for:
- off-heap = native, not GC heap
- DirectByteBuffer/NIO/Netty
- MaxDirectMemorySize bound
- NMT/jcmd tracking
- RSS > heap symptom
Common Mistakes
- Assuming heap metrics capture all JVM memory
- Not bounding MaxDirectMemorySize
- Ignoring native memory in container sizing
Best Practices
- Enable NMT to see native memory
- Bound direct memory explicitly
- Budget off-heap in container limits
Follow-up Questions
- How is a DirectByteBuffer eventually freed?
- How do you read jcmd VM.native_memory output?
- Why does Netty pool direct buffers?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Troubleshooting (JVM) Interview question: "What is off-heap / direct memory, and how do you track and bound it?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair