Video in progress
The sun.misc.Unsafe memory methods were deprecated (JEP 471). What does that mean for your libraries and services?
In JDK 23, Unsafe's memory-access methods were deprecated for removal. From JDK 24 a warning appears on the first call, and in later versions they will throw errors and then be removed. The replacements are VarHandle (heap) and the FFM API (off-heap). For services, the main work is finding which dependencies use Unsafe and updating them.
📖 Answer
Unsafe was never a public API, but Netty, Kryo, LZ4, Chronicle, old Guava, Hazelcast and many serialization libraries used it for speed. Now there are standard alternatives:
- atomic/volatile operations on heap fields and array elements:
VarHandle(Java 9); - off-heap memory:
MemorySegmentandArena(Java 22); - other tricks, like creating a class instance without constructing an object, partly remain without a replacement.
The stages (controlled by the --sun-misc-unsafe-memory-access option):
- JDK 23: deprecated, a warning at compile time;
- JDK 24: a runtime warning on the first call (default
warn); - later: default
deny, which means an exception, and then the methods are removed.
A practical plan for a service team:
# see which library is using it
java --sun-misc-unsafe-memory-access=debug -jar app.jar 2>&1 | grep -i unsafe
jdeps --jdk-internals app.jar
Then: update the dependencies (many popular libraries have already moved or are moving to VarHandle/FFM), and plan to replace the ones that aren't updated. Running tests in deny mode in CI gives an early signal for a future JDK.
If you write an internal library: don't use Unsafe yourself. With a multi-release JAR, keep the old path for old JDKs and FFM for new ones. Many libraries are moving exactly this way now.
This is part of one big trend, "integrity by default": dynamic agents, JNI, Unsafe and internal APIs are all starting to require explicit permission (a flag). The platform team has to manage these flags consciously at the base image level.
Sign up to mark studied questions and save your progress.