Quick revision
The short answer
The strategy is to treat off-heap memory as an exception with an owner, not a default. The memory-access methods of sun.misc.Unsafe are deprecated for removal since JDK 23 and warn when used from JDK 24, so code and libraries relying on them have a deadline, and the Foreign Function and Memory API, with MemorySegment and Arena, is the supported replacement with bounds checks and deterministic deallocation. Because off-heap data needs manual lifetime management and its own budget outside GC and heap tools, we prefer on-heap data with modern collectors unless measurements show a clear need, inventory Unsafe use, and give every off-heap component an owner and monitoring through RSS and NMT.
What to remember
In this answer, we saw that Java's off-heap options grew from direct ByteBuffers through sun.misc.Unsafe to the FFM API. Unsafe's memory-access methods are deprecated for removal and warn from JDK 24, while FFM offers MemorySegment and Arena with bounds checks and deterministic release. Off-heap data costs manual lifetime management and its own budget outside GC and heap tools, so the strategy is on heap by default, off heap only with measured need, an inventory and migration plan for Unsafe, and an owner and monitoring for every off-heap component.
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.
The strategy is to treat off-heap memory as an exception with an owner, not a default. The memory-access methods of sun.misc.Unsafe are deprecated for removal since JDK 23 and warn when used from JDK 24, so code and libraries relying on them have a deadline, and the Foreign Function and Memory API, with MemorySegment and Arena, is the supported replacement with bounds checks and deterministic deallocation. Because off-heap data needs manual lifetime management and its own budget outside GC and heap tools, we prefer on-heap data with modern collectors unless measurements show a clear need, inventory Unsafe use, and give every off-heap component an owner and monitoring through RSS and NMT.
Background to this question
Systems such as caches, market data engines, search indexes and stream processors have long kept large data off the Java heap to avoid GC pauses and object overhead. They built on whatever Java offered at the time: direct ByteBuffers with their 2 GB limit, and sun.misc.Unsafe, an internal class never meant for applications. Both the platform and its collectors have changed since then, and a technical leader has to decide what to keep, what to migrate and what to stop doing. In this answer, we will look at the history and the deadline for Unsafe, see what the FFM API gives us, weigh the real cost of off-heap data against modern on-heap options, and then set the strategy.
1Three Generations of Off-Heap Memory
Direct ByteBuffers, since Java 1.4, give a buffer of native memory with bounds checks, but each is limited to about 2 GB because it is indexed by int, and its memory is freed only when the GC finds the buffer unreachable. sun.misc.Unsafe gave libraries raw access: allocateMemory, putLong at any address, freeMemory. It is fast and unchecked, so a wrong address corrupts memory or crashes the JVM. The FFM API, final in JDK 22, is the modern answer: memory segments of any size, bounds and lifetime checks, explicit deallocation, and a supported, documented contract.
| Direct ByteBuffer | sun.misc.Unsafe | FFM API (JDK 22+) | |
|---|---|---|---|
| Size limit | About 2 GB per buffer | None | None (long offsets) |
| Bounds checks | Yes | No | Yes |
| Deallocation | After GC, via Cleaner | Manual freeMemory | Deterministic, when the Arena closes |
| Use after free | Not possible | Silent corruption or crash | IllegalStateException |
| Status | Supported | Memory access deprecated for removal | Supported, final |
2Why Unsafe Has a Deadline
Unsafe was always internal, but so many libraries used it that the JDK kept it working for years. Once FFM provided a supported alternative, the JDK started to retire it. JEP 471 deprecated the memory-access methods of sun.misc.Unsafe for removal in JDK 23, and JEP 498 made JDK 24 print a warning the first time one of them is used. Later releases are planned to throw an exception by default and finally remove the methods, so check the release notes of each JDK for the current step.
For a company, that turns Unsafe from a technical detail into a dependency risk. Our own code may not use it, but serialization libraries, caches, off-heap stores and older networking libraries may. A JDK upgrade could one day break them.
Running test suites with the deny setting is a cheap way to build the inventory: every library that still depends on Unsafe memory access fails and names itself.
3What the FFM API Gives Us
The FFM API models native memory as a MemorySegment, a bounded region with an owner, and an Arena, which controls when segments are freed. A confined arena belongs to one thread and frees everything when it is closed; a shared arena can be used by many threads; an automatic arena leaves release to the GC, like a direct buffer.
Notice the two checks that Unsafe never had. An index past the end fails with an exception instead of reading someone else's memory, and use after close fails instead of corrupting the process. That alone changes off-heap code from a crash risk into ordinary Java code with clear errors.
4The Real Cost of Keeping Data Off Heap
Off-heap memory is outside the garbage collector, which is the point, but also the problem. Every byte has a lifetime that our code must manage, so bugs show up as leaks or use-after-free instead of being impossible. It is outside heap tools too: heap dumps, allocation profilers and the usual GC dashboards do not see it, so it needs its own monitoring through resident memory and NMT. And it needs its own budget inside the container limit, because MaxRAMPercentage does not cover it.
Meanwhile, the original reason for going off heap has weakened. Generational ZGC keeps pauses under a millisecond on heaps of hundreds of gigabytes, compact object headers reduce per-object overhead, and primitive arrays and compact layouts hold large data on the heap efficiently. Many systems built off heap in the era of long full GCs would be simpler and just as fast on heap today.
5The Strategy
The default is on heap. A new component may keep data off heap only when a load test with a modern collector shows a clear need, such as data sets larger than any practical heap, memory shared with native code, or memory-mapped files. Existing off-heap components are reviewed in the same way when they are next changed.
Then we inventory and migrate. The inventory lists every library and module that uses Unsafe memory access, from tests with the deny setting and from dependency scans, with each library's migration plan and the JDK version by which we need it. Our own Unsafe code moves to FFM; libraries that will not migrate are replaced.
Finally, every off-heap component has a named owner, a documented budget that is added to the service's memory budget, and monitoring through RSS, NMT and its own metrics, such as allocated bytes per arena. Off-heap memory without an owner is how kernel OOM kills begin.
- Does the data fit in a practical heap?With generational ZGC or G1YesMeasure on heap firstNoOff heap is justified
- On-heap load test meets latency and cost?Compare with the off-heap designYesKeep it on heapNoOff heap with FFM
- Off heap: owner, budget and monitoring set?RSS, NMT, arena metricsYesApproveNoNot ready for production
3 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Can we still use direct ByteBuffers?
Yes, they are supported and remain the natural type for NIO channels. For large or long-lived data, an FFM MemorySegment can be viewed as a ByteBuffer with asByteBuffer() when an API needs one, while the segment keeps control of its lifetime.
What if a critical library will not migrate off Unsafe in time?
We can stay on an older LTS for that service or allow the access explicitly with the command-line setting while it exists, but both only buy time. The plan should name a replacement library or a fork, with a date tied to the JDK version where the methods throw by default.
Is FFM slower than Unsafe?
For memory access, the JIT removes most of the checks in hot loops, so well-written FFM code is usually close to Unsafe. It is worth benchmarking critical paths with JMH, but performance is rarely a reason to stay on an unsupported API.
7Conclusion
In this answer, we saw that Java's off-heap options grew from direct ByteBuffers through sun.misc.Unsafe to the FFM API. Unsafe's memory-access methods are deprecated for removal and warn from JDK 24, while FFM offers MemorySegment and Arena with bounds checks and deterministic release. Off-heap data costs manual lifetime management and its own budget outside GC and heap tools, so the strategy is on heap by default, off heap only with measured need, an inventory and migration plan for Unsafe, and an owner and monitoring for every off-heap component.
Open with
My starting point is that off-heap memory is an exception that needs a reason and an owner. Historically we had direct ByteBuffers, which are safe but limited to about 2 GB each and freed only after GC, and sun.misc.Unsafe, which is fast but unchecked.
Then, if they want more
That second one now has a deadline: its memory-access methods were deprecated for removal in JDK 23 and print a warning from JDK 24, with later releases planned to block them.
So I would build an inventory, partly by running our tests with the Unsafe memory access setting on deny, and track each library's migration plan against our JDK upgrade schedule.
Our own code moves to the FFM API, which is final since JDK 22 and gives MemorySegment and Arena: bounds checks, errors on use after close, and deterministic release when the arena closes.
I would also be honest about the cost: off-heap data needs manual lifetime management, it is invisible to heap dumps and GC dashboards, and it needs its own budget inside the container limit.
Since generational ZGC and compact headers make large heaps much cheaper than they were, my default is on heap, and off heap only when a load test shows a clear need. Any off-heap component gets a named owner, a documented budget, and monitoring through RSS, NMT and its own metrics.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 23, Java 22
- 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 "Java gained several ways to manage memory outside the heap over the years: direct...": https://scrivio-interview-library.netlify.app/java/memory-layout/0ca16a61e9/