Quick revision
The short answer
sun.misc.Unsafe was never a supported API; it survived because important libraries depended on it, and the JDK is now removing its memory-access methods as part of its integrity by default direction, with VarHandle and the FFM API as replacements. An organisation should inventory which dependencies use it, track and adopt the upstream releases that move to supported APIs, use the JDK's own run-time warnings in tests to find the remaining uses, stop adding new internal code that touches Unsafe, and set aside budget for the few libraries that migrate slowly or not at all.
What to remember
In this answer, we saw that Unsafe was always an unsupported internal class, and that the JDK is now deprecating its memory-access methods, then warning, and later removing them, because supported replacements exist and the platform wants integrity by default. An organisation responds by inventorying its dependency tree with jdeps and the JDK's debug warnings, tracking and adopting clean upstream releases through shared dependency management, making CI strict while production stays on warnings, forbidding new internal use, and budgeting early for libraries that will not migrate on their own.
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 was never a supported API; it survived because important libraries depended on it, and the JDK is now removing its memory-access methods as part of its integrity by default direction, with VarHandle and the FFM API as replacements. An organisation should inventory which dependencies use it, track and adopt the upstream releases that move to supported APIs, use the JDK's own run-time warnings in tests to find the remaining uses, stop adding new internal code that touches Unsafe, and set aside budget for the few libraries that migrate slowly or not at all.
Why this question is asked
Most teams have never written a line of code that uses sun.misc.Unsafe, yet almost every Java service depends on it indirectly, through a serialisation library, a cache, a networking stack or a compression codec. That is why the JDK's decision to deprecate and then remove its memory-access methods is an organisational problem rather than a coding one. Interviewers ask senior leaders this question to see whether they can turn an upstream change into a calm, funded plan. In this answer, we will recap why Unsafe exists and why it is going away, then walk through the five parts of the response: inventory, tracking upstream, using the JDK's warnings, a rule for internal code, and budget for the stragglers.
1Why Libraries Used Unsafe and Why It Is Going Away
Unsafe is a class inside the JDK that gives raw access to memory: read or write any address, allocate native memory, compare-and-swap a field at a given offset, and so on. It was meant for the JDK's own use, but for many years it was the only way to get that performance from Java, so libraries reached in and took it with reflection. It ended up in the jdk.unsupported module, a name that tells us everything about its status.
Over time the JDK added supported replacements. VarHandle, in Java 9, covers atomic and ordered access to fields and arrays. The Foreign Function and Memory API, final in Java 22, covers off-heap memory with bounds and lifetime checks. With replacements in place, the JDK started removing the risky parts: JEP 471 deprecated the memory-access methods for removal in Java 23, and JEP 498 made Java 24 print a warning the first time one of them is used. The motivation is integrity: if any library can write to any address, the JDK cannot guarantee that its own invariants hold, which blocks optimisations and weakens security.
| Release | Change |
|---|---|
| Java 9 | VarHandle added; Unsafe moved to the jdk.unsupported module |
| Java 22 | FFM API final: MemorySegment, Arena |
| Java 23 | Unsafe memory-access methods deprecated for removal (JEP 471) |
| Java 24 | Warning on first use of those methods by default (JEP 498) |
| Later releases | Planned: throw by default, then remove (check your version) |
2Part 1: Inventory the Dependency Tree
We cannot manage what we cannot see, so the first step is a list. For each service we want to know which artifacts, direct or transitive, call Unsafe memory-access methods. Static scanning gives a first picture: jdeps with --jdk-internals reports uses of JDK internal APIs, including sun.misc.Unsafe, class by class. Run-time evidence is more precise, because many libraries check for Unsafe and fall back if it is missing, so we also run test suites with the JDK's warning option set to report every use.
We collect the results centrally, by library and version rather than by service, because the same five or six libraries usually account for nearly every hit. Typical names are serialisation libraries, caches, networking stacks such as Netty, compression and hashing libraries, and high-performance collections.
3Part 2: Track Upstream and Upgrade
Most active libraries are already moving. The well-maintained ones either use VarHandle and FFM when they run on a new enough JDK, or ship versions that drop Unsafe entirely. Our job is to know which version of each library is clean and to get services onto it. We assign each library on the list an owner who watches its releases, and we add the clean version to our shared dependency management, such as a Maven BOM or a Gradle platform, so that every service picks it up with a routine upgrade rather than a special project.
4Part 3: Use the JDK's Warnings as a Test
The warnings are a gift: the JDK itself tells us when Unsafe memory access happens. We use them in stages. In local runs and integration tests we use the debug mode to see every caller. Once the known uses are fixed, we run CI with the deny mode, so any new use fails the build instead of reaching production.
- Inventory with jdeps and debug-mode test runsgroup results by library and version
- Assign an owner per librarywho tracks upstream releases
- Upgrade through the shared BOMclean versions reach every service
- Does the library have a clean release?YesUpgrade and verify in CINoPatch, replace or fund the migration
- CI runs with deny modenew uses fail the build
- Ready for the JDK that throws by default
6 steps in order. Play them, or step through them one at a time.
Notice the order. Production stays on the default warning mode during the transition so nothing breaks, while CI gets strict first. That gives us early failure where it is cheap and no surprises where it is expensive.
5Part 4: A Rule for Our Own Code
Internal code is the easiest part to control. The rule is simple: no new code uses Unsafe, and existing internal uses are migrated with a deadline. Compare-and-swap on fields moves to VarHandle or the atomic classes; off-heap buffers move to MemorySegment and Arena. A static analysis rule or an ArchUnit test that forbids references to sun.misc.Unsafe in our packages keeps the rule enforced without anyone having to remember it.
6Part 5: Budget for the Stragglers
Some libraries will be slow. An old serialisation library with one volunteer maintainer may never release a clean version. For each of those we have three options, and each costs something. We can replace the library, which costs migration work and testing. We can contribute the fix upstream, which costs engineering time but helps everyone. Or we can fork and patch it, which costs us its maintenance forever. We should decide early, while the JDK only warns, and put the work into the roadmap with a date tied to our next LTS upgrade, rather than discovering it when a JDK release starts throwing exceptions in production.
7Follow-up questions
Is the whole of Unsafe going away?
The deprecation for removal targets the memory-access methods, which are the large majority of the class. A few other methods have their own history; for example defineAnonymousClass was already removed in Java 17 in favour of hidden classes. Check the JDK release notes for each method a library depends on.
Why not just keep running with the warnings?
Because the warnings are a countdown, not a final state. The plan is for a later release to throw by default and then remove the methods. A service that ignores the warnings today will fail on a future JDK upgrade, and at that point the fix is urgent instead of planned.
How do we stop a new dependency from bringing Unsafe back?
Run CI with the deny setting and add a jdeps check to the dependency review step. A new library that uses Unsafe then fails the build, and the team must either choose another library or get an explicit, time-limited exception.
8Conclusion
In this answer, we saw that Unsafe was always an unsupported internal class, and that the JDK is now deprecating its memory-access methods, then warning, and later removing them, because supported replacements exist and the platform wants integrity by default. An organisation responds by inventorying its dependency tree with jdeps and the JDK's debug warnings, tracking and adopting clean upstream releases through shared dependency management, making CI strict while production stays on warnings, forbidding new internal use, and budgeting early for libraries that will not migrate on their own.
Open with
First I would explain that Unsafe was never a supported API. It lives in the jdk.unsupported module, and libraries used it because for years there was no supported way to do CAS on fields or manage off-heap memory.
Then, if they want more
Now there is, VarHandle since Java 9 and the FFM API since Java 22, so under the integrity by default direction the JDK deprecated the memory-access methods in Java 23, started warning on first use in Java 24, and plans to throw and then remove them. My response would have five parts.
Inventory: run jdeps with jdk-internals across our artifacts and run test suites with the debug value of the sun-misc-unsafe-memory-access option, grouping results by library and version. Track upstream: give each library an owner and move clean versions into our shared BOM so services get them through routine upgrades.
Use the warnings: keep production on the default while CI runs with deny, so new uses fail early. Internal code: no new Unsafe, enforced with a static check, and existing uses moved to VarHandle or MemorySegment. And budget: for slow or abandoned libraries, decide early whether to replace, contribute upstream or fork, and tie it to our next LTS upgrade.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 17, Java 22, Java 23
- 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 "For decades, high-performance Java libraries relied on sun.misc.Unsafe. The JDK is now...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/a009af113c/