Java Unsafe and Native Memory Access
Notes on the evolution of Java Unsafe and off heap memory access - touches on Unsafe, FFM API and the Agrona DirectBuffer.
Java Unsafe and Native Memory Access
Java's Unsafe API offers a number of features, including:
- Off heap memory access
- CAS
- Operate on an object's fields directly via memory addresses
- Park and unpark
Different techniques have been used in different versions of the JDK in order to access Unsafe
:
- Static method -
getUnsafe()
- Reflection based -
Unsafe.class.getDeclaredField("theUnsafe")
- Using JVM args to increase scope -
--add-opens java.base/jdk.internal.misc=ALL-UNNAMED
- Various open source utils including Spring
Over time the ability to access Unsafe has become restricted and features deprecated in favour of 2 APIs:
- VarHandles - for on heap memory manipulation
- Foreign Functions and Memory API - off heap memory access
These API's offer additional levels of safety through checks we were previously looking to skip by using Unsafe
, including things like bounds checking and memory alignment.
Comparing Unsafe and FFM API
Using the data from this page:
The above url and result surprised me as previously I had seen reports and benchmarks that Unsafe was much faster, so although we're talking about differences measured in single or sub 1 nanoseconds that can still add up to millions of messages per second difference, and so the benefit was clear.
Agrona and DirectBuffer
The Agrona library has a pretty nice abstraction of how it uses Unsafe
operations in an UnsafeApi
class which combines a Gradle buildSrc plugin with ByteBuddy to use code injection for the actual implementation.
This is the "common data layer" of Aeron - a core component for data access across Transport, Archive and Cluster.
Chronicle
The OpenHFT codebase also makes use of Unsafe extensively.
They also use their own Arena
abstraction.
Links and Resources
https://www.infoq.com/news/2024/06/jep-456-removing-unsafe-methods/
https://blogs.oracle.com/javamagazine/post/the-unsafe-class-unsafe-at-any-speed