Skip to main content

Java Unsafe and Native Memory Access

· 2 min read
Sanjeev Sarda
High Performance Developer

Notes on the evolution of Java Unsafe and off heap memory access - touches on Unsafe, FFM API and the Agrona DirectBuffer.

capo

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:

alt text

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.

alt text

Chronicle

The OpenHFT codebase also makes use of Unsafe extensively.

They also use their own Arena abstraction.

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

https://inside.java/2025/06/12/ffm-vs-unsafe/