Quick revision
The short answer
We would run it as a small, tracked programme: inventory every service with jdeps --jdk-internals and test runs that report Unsafe use through the --sun-misc-unsafe-memory-access option, upgrade shared libraries to versions that use VarHandle or the FFM API, replace abandoned ones and migrate in-house code, set a timeline tied to our next JDK LTS upgrade with CI testing the stricter settings early, and report status per service centrally while blocking new dependencies that use JDK internals without approval.
What to remember
In this answer, we managed the Unsafe risk across 200 services as one programme instead of 200 separate problems. We built an inventory with jdeps and debug-mode test runs, grouped it by library, fixed most of it once by upgrading clean versions through shared dependency management, replaced abandoned libraries and migrated in-house code, tied the deadline to our next JDK LTS upgrade while making CI strict early with the deny setting, and kept a central dashboard plus a dependency gate so the problem cannot quietly return.
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.
We would run it as a small, tracked programme: inventory every service with jdeps --jdk-internals and test runs that report Unsafe use through the --sun-misc-unsafe-memory-access option, upgrade shared libraries to versions that use VarHandle or the FFM API, replace abandoned ones and migrate in-house code, set a timeline tied to our next JDK LTS upgrade with CI testing the stricter settings early, and report status per service centrally while blocking new dependencies that use JDK internals without approval.
Why this question is asked
With 200 services, no single team can fix this problem, and no single service owner sees the whole picture. The same old serialisation library might sit in 80 services, the cache in 120, and the networking stack in nearly all of them. Interviewers ask this question to see whether a senior engineer can organise a cross-cutting change: measure it, fix it once at the shared level where possible, and keep it from coming back. In this answer, we will recap why the warnings appear, then go through the five steps of the plan: inventory, upgrades, replacements, the timeline with early CI enforcement, and central reporting with a gate for new dependencies.
1A Quick Recap of the Problem
sun.misc.Unsafe is an internal JDK class that lets code read and write raw memory and perform atomic operations on field offsets. Libraries used it for speed when there was no supported alternative. Since VarHandle (Java 9) and the Foreign Function and Memory API (final in Java 22) now cover those needs, the JDK deprecated Unsafe's memory-access methods for removal in Java 23 and, since Java 24, prints a warning the first time one is used. Later releases plan to throw by default and then remove the methods.
So the warnings are a schedule. Each service that still depends on these methods when the JDK starts throwing will fail at the first call, which could be at startup or deep in a rarely used code path. Our job is to make sure that day is a non-event.
2Build the Inventory
We combine static and dynamic evidence. Static scanning with jdeps finds every class in a service's jars that references JDK internals, including sun.misc.Unsafe. Dynamic evidence matters too, because many libraries reach Unsafe through reflection or only on certain code paths; running each service's test suite with the warning option in its debug mode logs a stack trace for each use. The option's values are allow, warn, debug and deny; check them for your JDK version.
| Library and version | Services | Clean version? | Action |
|---|---|---|---|
| netty 4.1.x (older) | 170 | yes, newer 4.x releases | upgrade via shared BOM |
| cache library (old major) | 120 | yes, next major | upgrade, small API changes |
| old serialisation library | 80 | no, unmaintained | replace with a maintained library |
| in-house ring buffer | 6 | ours | migrate to VarHandle |
Each CI job uploads its findings to a central place, keyed by library name and version. After a week we have a table that usually looks very concentrated: a handful of libraries explain almost every use across the 200 services. That concentration is good news, because it means most of the fix happens once, in shared dependency versions.
3Upgrade Through Shared Dependency Management
For libraries with a clean release, the fix is an upgrade. Most well-maintained libraries, including networking stacks such as Netty and the popular caches, have been moving to VarHandle and FFM, often through multi-release jars that pick the supported API on newer JDKs. We put the clean versions into the organisation's shared BOM or Gradle platform, so services get them through their normal dependency update rather than through 200 separate projects. A platform team owns this step and runs the compatibility tests once for the shared stack.
4Replace Abandoned Libraries and Migrate Our Own Code
Some libraries will never release a clean version. An unmaintained serialisation library is the typical case. Waiting is not a plan, so we choose: replace it with a maintained library, contribute the change upstream if the project still accepts patches, or fork it as a last resort, knowing that a fork is ours to maintain forever. Replacing a serialisation library is real work because of stored data formats, so it gets the earliest start and the most testing.
In-house code is the easiest part. Field CAS with Unsafe becomes a VarHandle or an atomic class, and raw off-heap memory becomes a MemorySegment with an Arena. The owning teams do this with a deadline, and a code review checklist or static analysis rule catches any new reference to sun.misc.Unsafe.
5A Timeline Tied to the JDK Upgrade Plan
We do not need to guess when the JDK will start throwing. We align the deadline with our own upgrade plan: every service must be clean before it moves to the next LTS release after Java 25, and in practice well before. The important trick is to make CI strict early while production stays on the default. Once a service's inventory is clean, its tests run with the deny setting, so any regression, such as a new library or an old one coming back, fails the build immediately.
- Scan every service in CIjdeps plus tests in debug mode
- Group findings by library and versiona few libraries explain most uses
- Does a clean version exist?YesUpgrade through the shared BOMNoReplace, contribute upstream or fork
- Migrate in-house code
VarHandle, atomics,MemorySegment - Switch the service's CI to denynew uses fail the build
- Service marked clean on the dashboardready for the next LTS
6 steps in order. Play them, or step through them one at a time.
6Report Centrally and Gate New Dependencies
A dashboard lists each service with its status: not scanned, uses found, clean in tests, enforced in CI. Leadership sees a single number, how many of the 200 are enforced, and the trend. Service owners see exactly which library is their blocker and which version fixes it. This turns a vague warning in logs into a list of concrete tasks.
Finally we keep it from coming back. The dependency review step for new libraries includes the jdeps check, and the deny setting in CI catches anything the scan misses. A team that truly needs a library that still uses JDK internals can ask for an exception, which is approved with an owner and an expiry date, not granted silently.
7Follow-up questions
Should we suppress the warnings in production in the meantime?
We can set the option to allow if the warning causes noise or alerts, but only as a temporary measure with the inventory already running. Suppressing without tracking just hides the countdown. Many organisations leave production on the default warning and alert on new occurrences.
Why scan both statically and at run time?
Static scanning finds references in bytecode, but some libraries call Unsafe through reflection or only when a feature is enabled. Run-time warnings in debug mode catch those, but only on code paths the tests execute. Together they give a much more complete picture than either alone.
What if the next LTS upgrade must happen before every library is clean?
The JDK's staged approach helps here: as long as the release still warns rather than throws, services can upgrade and keep working. If a release throws by default, the option can usually restore the old behaviour for a limited time. We would treat that as an approved, time-limited exception for the specific services, not a general setting.
8Conclusion
In this answer, we managed the Unsafe risk across 200 services as one programme instead of 200 separate problems. We built an inventory with jdeps and debug-mode test runs, grouped it by library, fixed most of it once by upgrading clean versions through shared dependency management, replaced abandoned libraries and migrated in-house code, tied the deadline to our next JDK LTS upgrade while making CI strict early with the deny setting, and kept a central dashboard plus a dependency gate so the problem cannot quietly return.
Open with
I would run this as one programme with a platform team owning it, because the fix is mostly in a few shared libraries, not in 200 codebases. Step one is the inventory: add a jdeps --jdk-internals scan to every service's CI job and run the test suites with the sun-misc-unsafe-memory-access option set to debug, so we get stack traces for every use.
Then, if they want more
All findings go to one place, grouped by library and version, and usually a handful of libraries explain almost everything. Step two: for libraries with clean releases, like newer Netty or cache versions that use VarHandle or the FFM API, we upgrade them once in the shared BOM so services pick them up through normal updates.
Step three: abandoned libraries, like an old serialisation library, get replaced early, because data formats make that slow, and in-house Unsafe code moves to VarHandle or MemorySegment.
Step four: the deadline follows our JDK plan, every service clean before the next LTS, and as soon as a service is clean its CI switches to deny so regressions fail the build while production stays on the default warning.
Step five: a dashboard with status per service, and a dependency review that blocks new libraries using JDK internals unless an owned, time-limited exception is approved.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 22, Java 23, Java 24, Java 25
- 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 "Your platform depends on several libraries (an old serialisation library, a cache, a...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/50863e080f/