Hard👤 8-15 years 1 min read

What is off-heap / direct memory, and how do you track and bound it?

Asked inAmazonGoogleMicrosoft
#direct memory#off-heap#directbytebuffer#native memory tracking#netty
Report issue

⚡ 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.

Coffee Chat Question

Concept Made Simple

What is off-heap / direct memory, and how do you track and bound it?

🧠Mind Map Answer

Remember It Faster

Direct buffernative memory, GC-free I/O
Freed whenbuffer's Cleaner runs (GC-triggered)
Bound-XX:MaxDirectMemorySize
TrackNative Memory Tracking (NMT)

🔥What If?

Think Beyond the Expected

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 heapDirectByteBuffer/NIO/NettyMaxDirectMemorySize boundNMT/jcmd trackingRSS > 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

  • 1How is a DirectByteBuffer eventually freed?
  • 2How do you read jcmd VM.native_memory output?
  • 3Why does Netty pool direct buffers?

🧩Related Technologies

DirectByteBufferNettyNMTjcmd

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions