Concurrency

VarHandles and the End of Unsafe

The release store measured 28 per cent more expensive than the volatile store it is weaker than. Access modes are a trade, and the deadline for learning them is fixed.

VarHandles and the End of Unsafe — Concurrency article cover
On this page

Sixteen release stores cost 8.245 ns. The same sixteen as volatile stores cost 6.445.

The weaker guarantee measured 28 per cent more expensive than the stronger one, with intervals of ±0.056 and ±0.087 that come nowhere near each other. Whatever else the access modes are, they are not a ladder where each rung down is cheaper — which is the practical reason to treat them as a trade to be measured rather than a default to be chosen once.

All measurements: JDK 26.0.2.1 (Homebrew build), macOS 26.6.2, Apple M4 Pro, JMH 1.37, three forks with five warmup and five measurement iterations. Each benchmark performs sixteen accesses to varying slots of a long[], which is what keeps the numbers off the harness floor.

Four modes, one array

Access ns/op (16 accesses) per access
readPlainField 4.566 ± 0.018 0.285
readOpaque 5.579 ± 0.101 0.349
readVolatile 6.717 ± 0.021 0.420
readAcquire 6.955 ± 0.241 0.435
writePlain 4.975 ± 0.010 0.311
writeOpaque 5.943 ± 0.212 0.371
writeVolatile 6.445 ± 0.087 0.403
writeRelease 8.245 ± 0.056 0.515
compareAndSet 16.606 ± 0.073 1.038

The read side is unsurprising and, more usefully, narrow: the entire range from plain to acquire is 2.4 ns across sixteen accesses, or about 0.15 ns per access. On this hardware the ordering a read buys is close to free, which is an argument for asking what the call site needs rather than for economising.

compareAndSet is the outlier that matches intuition. At 1.04 ns per operation against 0.31 for a plain write, atomicity — not ordering — is what costs. A counter that needs to be atomic pays roughly three times a counter that only needs to be ordered, and that is the trade worth thinking about.

The ordering the numbers do not follow

writeRelease above writeVolatile inverts what the modes’ strength implies. A release store makes prior writes visible before it; a volatile store does that and more.

This run does not establish why. The measurement is stable — three forks, tight intervals, repeated — so it is a property of this build and this hardware rather than noise, but naming the mechanism would require reading the generated code, which is a different article. What it establishes is narrower and more useful: the strength ordering of the access modes is not a cost ordering, and a call site that picks setRelease because it sounds cheaper than setVolatile has made an assumption this machine contradicts.

The first harness measured the loop

The first version of this benchmark did one access per operation. It reported setPlain at 0.276 ns, setOpaque at 0.275, setVolatile at 0.345 and setRelease at 0.689.

Those numbers are the harness. A score near 0.3 ns/op on this machine is the JMH loop rather than the code, and three of those four rows sit on it. The inversion happened to survive the redesign, which is luck rather than vindication: had the redesign removed it, the original numbers would have been just as confidently wrong in the other direction.

The other half is a deadline

Access modes are not only a better way to say what volatile implied. They are the supported replacement for a large part of what sun.misc.Unsafe was used for, and that matters because the methods are going away on a published schedule.

JEP 498 sets it out in phases: deprecation in JDK 23, warnings by default in JDK 24, and phase 3 — “throw an exception whenever a memory-access method is used”, default deny — targeted at “JDK 26 or later”.

This is JDK 26, so it is worth checking rather than assuming. Calling objectFieldOffset on this build:

WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
RESULT=returned offset 16

It warns and it returns. Phase 3 has not landed in this build. The same call with the option set explicitly does what the phase will do:

$ java --sun-misc-unsafe-memory-access=deny UnsafeProbe.java
RESULT=threw java.lang.UnsupportedOperationException: objectFieldOffset

That flag is the useful part. It converts a future breakage into a present test, and it can be run against a whole dependency tree today.

It is worth knowing how close this is. Every JMH run in this plan’s own experiments printed the warning, naming the caller:

WARNING: sun.misc.Unsafe::objectFieldOffset has been called by org.openjdk.jmh.util.Utils

The most widely used benchmarking harness on the platform is in the affected set. This is not a migration confined to code somebody else maintains.

Choosing a mode

The method is the same as everywhere else in this series. Decide what ordering the call site requires — the memory model is what that question is asked in — and then measure the mode that provides it on the hardware that will run it, because on this machine the cost ordering and the strength ordering are not the same ordering.

And run the deny flag once against everything you depend on. The schedule is published, the check takes a minute, and the alternative is finding out from a release note.

Frequently asked

Is a weaker access mode always cheaper?
No, and this run is a counterexample. On JDK 26.0.2.1 on Apple silicon the release store measured 8.245 ns against 6.445 for the volatile store, repeatedly and with tight intervals. Pick the mode from the ordering the call site needs, then measure it on the target rather than assuming the strength ordering is a cost ordering.
When does sun.misc.Unsafe stop working?
JEP 498's phased plan deprecated the memory-access methods in JDK 23, made warnings the default in JDK 24, and schedules throwing by default for JDK 26 or later. On the JDK 26.0.2.1 build measured here it had not happened - the call warned and returned. Running with --sun-misc-unsafe-memory-access=deny shows what the change will do to your dependency tree today.

Search the site

Arrow keys to move, Enter to open.