Quick revision
The short answer
sun.misc.Unsafe is an internal JDK class, now in the jdk.unsupported module, that gives raw access to memory and objects: reading and writing addresses, field offsets, compare-and-swap, creating objects without running constructors, and memory fences. Libraries used it for off-heap memory, fast serialisation, lock-free data structures and atomic field updates. Its memory-access methods were deprecated for removal in Java 23 (JEP 471) and warn at run time since Java 24 (JEP 498); the supported replacements are VarHandle for atomic and ordered access and the FFM API's MemorySegment and Arena for off-heap memory.
What to remember
In this answer, we saw that sun.misc.Unsafe is an internal JDK class in the jdk.unsupported module that offers raw memory access, field offsets, compare-and-swap, constructor-less object creation and fences. Libraries used it for off-heap memory, fast serialisation and lock-free structures because there was no supported alternative. Its memory-access methods were deprecated for removal in Java 23 and warn on use since Java 24, controlled by --sun-misc-unsafe-memory-access. VarHandle replaces its atomic and ordered access, the FFM API replaces its off-heap memory, and Lookup methods such as defineHiddenClass replace other internal tricks.
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.
sun.misc.Unsafe is an internal JDK class, now in the jdk.unsupported module, that gives raw access to memory and objects: reading and writing addresses, field offsets, compare-and-swap, creating objects without running constructors, and memory fences. Libraries used it for off-heap memory, fast serialisation, lock-free data structures and atomic field updates. Its memory-access methods were deprecated for removal in Java 23 (JEP 471) and warn at run time since Java 24 (JEP 498); the supported replacements are VarHandle for atomic and ordered access and the FFM API's MemorySegment and Arena for off-heap memory.
Why this question is asked
Unsafe is a class most Java developers never use directly, yet it sits underneath many of the libraries we rely on every day. Now that recent JDKs print warnings about it, developers see its name in their logs and ask what it is. Interviewers ask this question to check that we understand what it did, why libraries needed it, why the JDK is removing it, and what to use instead. In this answer, we will see what Unsafe is and how libraries got hold of it, go through its main abilities with a small example, look at its current status, and then map each use to its supported replacement.
1What Unsafe Is
Unsafe is a class with methods that skip the normal safety rules of Java. In ordinary Java code, every array access is bounds-checked, memory is managed by the garbage collector, and objects are created only through constructors. Unsafe lets code read and write any memory address, find the raw offset of a field inside an object, perform atomic compare-and-swap on that offset, allocate and free native memory, create an object without running any constructor, and insert memory fences that control how the CPU orders reads and writes.
It was written for the JDK's own use, in classes such as the atomic types and concurrent collections. Its constructor is private and its static getUnsafe() method refuses ordinary callers, so libraries took the singleton instance through reflection. In Java 9 it moved into a module named jdk.unsupported, a name chosen to make its status plain: it is available for compatibility, but it was never a supported API.
Notice the last read. Nothing stops us from reading memory we have already freed, or an address far outside our allocation. There is no exception; at best we read garbage, at worst the JVM dies with a segmentation fault. That is the trade Unsafe always offered: speed and power with no safety net.
2What Libraries Used It For
Off-heap memory was a big one. Netty's buffers, Chronicle's data structures and several caches allocated large native memory regions with allocateMemory and read them with getLong and putLong, without the bounds checks of ByteBuffer, to keep large data away from the garbage collector.
Fast serialisation was another. Serialisation libraries used field offsets to read and write fields directly instead of through reflection, and allocateInstance to create objects without calling a constructor, which is what deserialisation needs when a class has no no-argument constructor.
Lock-free data structures and atomic field updates used compareAndSwapInt, compareAndSwapLong and compareAndSwapObject on field offsets, together with the fence methods, to build queues, counters and ring buffers without locks. Before Java 9 there was no supported way to do an atomic operation on a field of your own class without wrapping it in an AtomicInteger.
3Its Status in Current JDKs
Once supported replacements existed, the JDK began removing the dangerous parts. JEP 471 in Java 23 deprecated the memory-access methods for removal; those are the large majority of the class, covering on-heap and off-heap reads, writes, allocation and the CAS methods. JEP 498 in Java 24 made the JVM print a warning the first time any of them is called. The plan is that a later release throws an exception by default, and a release after that removes the methods. Check your JDK version for the current stage.
Behaviour is controlled by a launcher option, --sun-misc-unsafe-memory-access, with the values allow, warn, debug and deny. So we can silence the warning temporarily, get a stack trace for every caller, or make any use fail, which is the best way to find hidden callers in tests.
| Release | Default behaviour |
|---|---|
| Java 22 and earlier | works silently |
| Java 23 | works; methods deprecated for removal (JEP 471) |
| Java 24 and 25 | works; warning on first use (JEP 498) |
| A later release | planned: throws by default |
| After that | planned: methods removed |
4The Supported Replacements
For atomic and ordered access to fields and array elements, the replacement is VarHandle, added in Java 9. A VarHandle is a typed reference to a variable that offers compareAndSet, getAndAdd, and access modes from plain to volatile, plus static fence methods. The JIT compiles it to the same machine instructions as the Unsafe calls, but it checks types and respects access control.
For off-heap memory, the replacement is the Foreign Function and Memory API, final in Java 22. A MemorySegment is a bounded region of memory, and an Arena controls when it is freed. Every access is checked against the segment's size and lifetime, so the bug from our first example becomes an exception.
| Use | Unsafe method | Supported replacement |
|---|---|---|
| Atomic update of a field | compareAndSwapInt(obj, offset, ...) | VarHandle.compareAndSet |
| Ordered or fenced access | putOrderedInt, fullFence | VarHandle setRelease, fullFence |
| Off-heap memory | allocateMemory, getLong(address) | Arena + MemorySegment |
| Defining classes at run time | defineAnonymousClass (removed) | Lookup.defineHiddenClass |
| Private field access | objectFieldOffset + getInt | MethodHandles.privateLookupIn |
Other tricks have their own replacements. Defining classes at run time, once done with Unsafe.defineAnonymousClass, now uses MethodHandles.Lookup.defineHiddenClass; the old method was removed in Java 17. Private access to another class's fields goes through MethodHandles.privateLookupIn, which respects module boundaries. Creating objects without constructors for deserialisation has no simple general-purpose replacement, which is one reason serialisation libraries are among the slower ones to migrate.
5Follow-up questions
What JVM option controls the Unsafe memory-access warnings?
The launcher option --sun-misc-unsafe-memory-access, available since Java 23. Its values are allow (no warning), warn (one warning on first use, the default since Java 24), debug (a warning with a stack trace for each use) and deny (throw UnsupportedOperationException). We use debug to find callers and deny in CI to stop new ones. The option is expected to go away once the methods are removed.
If VarHandle is just as fast, why did libraries keep using Unsafe?
Mostly compatibility. Many libraries supported Java 8 for years, where VarHandle and FFM do not exist, so they kept one code path that worked everywhere. As their minimum Java version rises, they switch, often using multi-release jars to pick the supported API on newer JDKs.
Is all of Unsafe being removed?
The deprecation targets the memory-access methods. A few other methods have had separate fates, and some uses such as allocateInstance are not part of JEP 471. Check the release notes of your JDK for the exact methods.
6Conclusion
In this answer, we saw that sun.misc.Unsafe is an internal JDK class in the jdk.unsupported module that offers raw memory access, field offsets, compare-and-swap, constructor-less object creation and fences. Libraries used it for off-heap memory, fast serialisation and lock-free structures because there was no supported alternative. Its memory-access methods were deprecated for removal in Java 23 and warn on use since Java 24, controlled by --sun-misc-unsafe-memory-access. VarHandle replaces its atomic and ordered access, the FFM API replaces its off-heap memory, and Lookup methods such as defineHiddenClass replace other internal tricks.
Open with
sun.misc.Unsafe is an internal JDK class, now in the jdk.unsupported module, that lets code bypass Java's safety rules. It can read and write raw memory addresses, allocate and free native memory, get a field's offset and do compare-and-swap on it, create objects without running a constructor, and issue memory fences.
Then, if they want more
It was never a supported API; libraries got the instance through reflection on the theUnsafe field. They used it for off-heap memory, like Netty buffers and Chronicle, for fast serialisation using field offsets and allocateInstance, and for lock-free queues and atomic field updates.
The catch is there is no checking at all, so a wrong address crashes the JVM instead of throwing. Its status today: JEP 471 in Java 23 deprecated the memory-access methods for removal, and JEP 498 in Java 24 prints a warning the first time one is used.
The option --sun-misc-unsafe-memory-access with allow, warn, debug or deny controls that, and later releases plan to throw and then remove them. The replacements are VarHandle, since Java 9, for atomic and ordered field and array access, and the Foreign Function and Memory API, MemorySegment and Arena, for off-heap memory, plus Lookup methods like defineHiddenClass for other tricks.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 23, Java 9, Java 17
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "What is sun.misc.Unsafe, what did libraries use it for, and what is its status in...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/c2ae06b45f/