Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Some libraries your services depend on still use sun.misc.Unsafe for CAS and off-heap memory. The JDK deprecated Unsafe memory-access methods for removal and now warns on their use. How would you plan for this across the organisation?

The supported replacements already exist: VarHandles (Java 9) cover atomic and ordered access to fields and array elements, and the Foreign Function and Memory API (final in Java 22) covers off-heap memory. My plan would be to inventory which dependencies still call Unsafe, using the runtime warnings and the --sun-misc-unsafe-memory-access option in test runs, upgrade the libraries that have moved to supported APIs, push or replace those that have not, schedule the work ahead of each LTS migration because a later release will deny and then remove the methods, and forbid direct Unsafe use in our own code.

Why this question is asked

sun.misc.Unsafe was never a public API, yet for twenty years high-performance libraries used it for compare-and-set on fields, for reading and writing memory outside the heap, and for other tricks. The JDK is now taking the memory-access part away in stages, and the risk for an organisation is not our own code but the dozens of libraries underneath it. Interviewers ask this at senior level because it is a typical platform problem: a change with a long fuse, spread across many teams, that becomes urgent the week someone tries to upgrade the JDK. In this answer, we will recap what Unsafe was used for and what replaces it, look at the deprecation stages, and then build an organisation-wide plan covering inventory, upgrades, timing and guardrails.

1What Unsafe Was Used For

Unsafe is a class in the jdk.unsupported module that gives raw access to memory. Libraries used it for two main purposes. The first was atomic and ordered access to fields: compareAndSwapInt on a field identified by its offset, or putOrderedObject for a cheap ordered write. This is how many queues, caches and buffer libraries built lock-free code before Java 9. The second was off-heap memory: allocateMemory, getLong(address) and putLong(address, value) to manage memory that the garbage collector does not see, used by networking frameworks, serialization libraries and caches to avoid copying and GC pressure.

Both uses now have supported, safe replacements. VarHandle, added in Java 9, gives typed compareAndSet, getAndAdd, getAcquire, setRelease and similar operations on any field or array element, with the same performance, because the JIT compiles them to the same instructions. The Foreign Function and Memory API in java.lang.foreign, final since Java 22, gives MemorySegment and Arena for allocating, reading and freeing off-heap memory with bounds checks and deterministic release.

Unsafe uses and their replacements
Unsafe useReplacementSince
compareAndSwapInt on a fieldVarHandle.compareAndSetJava 9
putOrderedObject, volatile get/putVarHandle setRelease, getAcquireJava 9
allocateMemory, freeMemoryArena.allocate, Arena.closeJava 22
getLong(address), putLongMemorySegment.get/set with ValueLayoutJava 22
Direct ByteBuffer cleaning hacksArena-scoped MemorySegmentJava 22

2How the JDK Is Phasing It Out

The removal happens in steps so that libraries have time to move. JEP 471 in Java 23 deprecated the memory-access methods of sun.misc.Unsafe for removal, which means compile-time warnings for code that calls them. JEP 498 in Java 24 went further: by default the JVM prints a warning on the first use of one of these methods at run time. The same JEPs introduced a launcher option, --sun-misc-unsafe-memory-access, with the values allow, warn, debug and deny, so we can choose the behaviour explicitly.

The announced direction is that a later release will make deny the default, so calls throw an exception, and a release after that will remove the methods. The exact releases are not fixed in advance, so check the JDK release notes for each version we adopt. The practical consequence is clear, though: a library that still relies on Unsafe memory access will at some point stop working on a new JDK, and the warning in Java 24 and later is our early notice.

3Find Out Who Uses It

We cannot plan without an inventory. The cheapest source is the JVM itself: on Java 24 or later, every service already prints a warning naming the class that first used a deprecated method. We collect those lines from logs across all services. For a fuller picture, we run each service's test suite in CI with the stricter settings, which turn every use into a stack trace or a failure, so the report names the library and the code path.

Java
# run a service on a recent JDK: report each use with a stack trace
java --sun-misc-unsafe-memory-access=debug -jar build/libs/orders.jar
// build.gradle.kts: make the test JVM refuse the deprecated methods
tasks.test { jvmArgs("--sun-misc-unsafe-memory-access=deny") } // a use now fails the test

Static scanning helps too: searching dependency jars for references to sun.misc.Unsafe gives a list of candidates, although it also flags libraries that only use Unsafe on old JDKs and switch to VarHandles at run time. We put the results into one shared table per dependency: which services use it, which version, whether a fixed version exists, and who owns the upgrade.

4Upgrade, Push or Replace

Most widely used libraries, networking frameworks, serialization libraries, caches and concurrency libraries among them, have released or are releasing versions that use VarHandles and the FFM API on newer JDKs. So the bulk of the work is ordinary dependency upgrades, which we can roll out through our shared bill of materials, the central list of approved versions that services inherit, so that one change reaches every team.

For a library with no fixed version, we check its issue tracker, add our voice or a pull request, and set a date. If the project is inactive, we plan a replacement in good time. Some libraries can also be configured to avoid Unsafe already, for example through a system property that selects a safe implementation; check each library's documentation. The point of the shared table is that no team discovers this alone during an urgent upgrade.

5Tie It to the LTS Calendar

Most organisations upgrade from one long-term support release to the next: Java 21 to Java 25, and later to the next LTS. Each of those jumps may cross a step of the Unsafe phase-out. So the work belongs in the migration plan, months ahead, not in the migration itself. A sensible rule is: before a service moves to a new LTS, its test suite must pass with --sun-misc-unsafe-memory-access=deny, which proves it is ready for the release after it as well. Then the actual switch of the default is a non-event.

Step throughOrganisation plan for Unsafe
  1. Collect warnings and CI reportsevery service, recent JDK
  2. Build the dependency tablelibrary, version, fix available, owner
  3. Fixed version exists?
    YesUpgrade through the shared BOMNoPush upstream or plan a replacement
  4. Gate in CItests pass with deny before each LTS move
  5. Ban new direct usebuild rule in our own code

5 steps in order. Play them, or step through them one at a time.

6Forbid It in Our Own Code

Finally, we stop the problem from growing. Our own code should never call sun.misc.Unsafe directly; VarHandle and java.lang.foreign cover every legitimate case, and the few teams that truly need low-level memory access should use the FFM API. A build rule enforces this: an ArchUnit test or a static analysis check that fails the build on any import of sun.misc.Unsafe, with an exception process that requires review by the platform team. A short internal guide with the replacement table above makes the rule easy to follow instead of just a prohibition.

7Follow-up questions

Are the CAS methods of Unsafe affected or only off-heap access?

JEP 471 covers the memory-access methods, which include on-heap field access such as the get, put and compareAndSwap methods that take an object and an offset, as well as off-heap access. Other Unsafe methods are handled separately. Check the method list in the JEP for the exact set.

Why not just run with allow forever?

Because allow is a temporary escape hatch. Once the methods are removed, no flag brings them back, so staying on allow only postpones the work to a moment when it blocks a JDK upgrade, possibly one that brings a security fix.

What if a critical library cannot be replaced in time?

We stay on the last JDK where it works with the flag, document the risk and the date, and treat the replacement as a tracked project. Knowing this a year early is the whole value of the inventory.

8Conclusion

In this answer, we saw that Unsafe served two purposes, atomic field access and off-heap memory, and that VarHandles and the Foreign Function and Memory API now replace both. The JDK deprecated the memory-access methods in Java 23, warns on their use since Java 24, and will later deny and remove them. The plan follows from that: inventory usage from warnings and CI runs with the access flag, upgrade libraries through a shared BOM and push or replace the rest, gate each LTS migration on passing with deny, and forbid direct Unsafe use in our own code.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd treat it as a platform migration with a long fuse. First, the replacements exist: VarHandles since Java 9 cover compareAndSet and ordered access to fields and arrays, and the Foreign Function and Memory API, final in Java 22, covers off-heap memory with MemorySegment and Arena.

Then, if they want more

The JDK deprecated the Unsafe memory-access methods for removal in 23 and warns on first use from 24, and later releases will deny and then remove them, so a library that still depends on them will eventually break on a new JDK.

Step one is an inventory: collect those runtime warnings from every service, and run test suites in CI with the sun-misc-unsafe-memory-access flag set to debug or deny so we get stack traces naming the library. I'd put that into one table per dependency with the version, whether a fixed release exists and an owner.

Step two, upgrade through our shared BOM, since most popular libraries have moved, and for the rest, open issues or pull requests upstream, or plan a replacement with a date. Step three, tie it to the LTS calendar: before a service moves to the next LTS, its tests must pass with deny, so the default switch later is a non-event.

Finally, ban direct Unsafe in our own code with an ArchUnit or static analysis rule, plus a short guide showing the replacement for each use.

About this answer

Last updated
12 November 2026
Versions it names
Java 24, Java 9, 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.
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 "Some libraries your services depend on still use sun.misc.Unsafe for CAS and off-heap...": https://scrivio-interview-library.netlify.app/java/atomics/c6f82fea29/