22.1 C
New York
Wednesday, June 12, 2024

What’s incorrect with Java’s solar.misc.Unsafe?

Java’s solar.misc.Unsafe class has been in use since 2002. It supplies important low-level strategies that framework builders use to ship in any other case unobtainable options and efficiency. Sadly, Unsafe additionally has long-standing issues associated to JVM maintainability. And, because the title implies, it is not precisely secure to make use of. A more recent JEP proposes to take away solar.misc.Unsafe’s reminiscence entry strategies in a future Java launch. However what is going to exchange them?

This text seems on the Unsafe class, why a few of its strategies are slated for removing, and what builders can do to arrange for this alteration.

Why Java’s Unsafe strategies are being deprecated

Java’s solar.misc.Unsafe class does some particular issues which can be in any other case not allowed. Its powers fall into two normal classes:

  • Low-level, pointer-like reminiscence entry and allocation
  • Class development exterior of regular means (even past reflection)

In exercising these powers, some strategies of the Unsafe class break customary protections and reminiscence administration constructed into the JVM. Due to this, JVM implementations fluctuate in how they accommodate Unsafe. The code is extra brittle in consequence, and is probably not moveable throughout JVM variations or working programs. Having it in use additionally hinders builders’ skill to evolve JVM internals.

Why builders use Unsafe

Unsafe is not all unhealthy, which is why it has been maintained for thus lengthy. Baeldung’s Information to solar.misc.Unsafe consists of an outline of what builders can do with the Unsafe class:

  • Class instantiation with out constructors
  • Direct manipulation of sophistication fields
  • Throwing checked exceptions when they aren’t dealt with by the scope
  • Direct entry to heap reminiscence operations
  • Entry to the examine and swap (CAS) operations

Java’s compare-and-swap operation is an effective instance of why we have now an Unsafe class. CAS is an instruction on the {hardware} degree, permitting atomic reminiscence entry. Atomicity offers important efficiency advantages to concurrent threads as a result of it permits us to switch reminiscence with out blocking. Historically, customary Java APIs could not leverage this characteristic as a result of it’s particular to the working system.

Right here’s a snippet of a compare-and-swap operation utilizing Unsafe, from Oracle’s introduction to solar.misc.Unsafe:

public last class AtomicCounter implements Counter {
    non-public static last Unsafe unsafe;
    non-public static last lengthy valueOffset;

    non-public risky int worth = 0;

    static {
        strive {
            Area f = Unsafe.class.getDeclaredField("theUnsafe"); // (1)
            f.setAccessible(true); // (2)
            unsafe = (Unsafe) f.get(null); // (3)
            valueOffset = unsafe.objectFieldOffset
                (AtomicCounter.class.getDeclaredField("worth")); // (4)
        } catch (Exception ex) { throw new Error(ex); }

    public int increment() {
        return unsafe.getAndAddInt(this, valueOffset, 1) + 1; // (5)

    public int get() {
        return worth;

You’ll discover even the entry to Unsafe is unusual. It is a static class member that should first be accessed with reflection (1), then manually set to accessible (2), after which referenced, once more with reflection (3).

With the Unsafe class in hand, we get the offset of the worth discipline on the current AtomicCounter class (4). The offset tells us the place in reminiscence the sphere resides in relation to the category reminiscence allocation. Be aware that right here we’re coping with pointers, which can be unfamiliar to Java builders, though they’re customary fare in direct reminiscence administration languages like C/C++. Pointers give us direct entry to heap reminiscence by placing the onus of reminiscence entry on the developer—one thing Java’s rubbish assortment mechanism was explicitly designed to keep away from.

To execute the CAS instruction, the getAndAddInt() perform known as (5). This tells the working system to execute the atomic instruction, utilizing this because the body of reference, on the valueOffset location, and with the “delta” of 1—that means the working system ought to increment by 1.

Refactoring solar.misc.Unsafe

Now you have had a style of utilizing Unsafe. The instance additionally highlights a few of its drawbacks.

Performing incorrect reminiscence operations could cause the JVM to “exhausting crash” with out throwing a platform exception. Poor reminiscence administration also can result in reminiscence leaks, that are notoriously simple to create and exhausting to diagnose and repair in some languages. Direct reminiscence entry also can open up safety holes like buffer overflows. (See Stack Overflow for an extra overview of the drawbacks of utilizing solar.misc.Unsafe.)

However programmer-created bugs are solely a part of the issue. Utilizing Unsafe additionally ends in implementation-specific code. Which means the applications utilizing them is probably not as moveable. It additionally makes it harder for a JVM to vary its implementations in these areas.

For all these causes, Java’s builders are shifting to introduce a platform-ordained method to carry out the actions presently related to the Unsafe class. The issue is that code utilizing solar.misc.Unsafe is everywhere. It hits varied frameworks at crucial factors. And since Unsafe offers with sensitive low-level operations, the code is very dicey to refactor.

Alternate options to solar.misc.Unsafe

Java’s builders are presently within the strategy of changing Unsafe options with customary, much less problematic variations. Let us take a look at the alternate options, a few of which have been launched in Java 9.


Variable Handles is described in JEP 193. This characteristic covers one of many greatest makes use of of Unsafe, which is immediately accessing and manipulating fields on the heap. It’s price studying the JEP’s objectives to grasp what this characteristic does, however here is a abstract:

  • It should not be doable to position the JVM in a corrupt reminiscence state.
  • Entry to a discipline of an object follows the identical entry guidelines as with getfield and putfield byte codes along with the constraint {that a} last discipline of an object can’t be up to date.
  • Its efficiency traits should be the identical as or much like the equal solar.misc.Unsafe operations.
  • The API should be higher than the solar.misc.Unsafe API.

Basically, VarHandles offers us a safer, higher model of Unsafe’s options, and it does not compromise efficiency. You possibly can see the vary of operation sorts this characteristic helps by trying on the Javadoc for AccessModes. You’ll discover compare-and-exchange is supported, together with many different primitive operation sorts. 

Utilizing VarHandle situations is a clear, secure method to carry out many low-level operations immediately on fields, with out operating the dangers we do with Unsafe.


The MethodHandles class (JEP 274) holds a Lookup class that replaces the usage of reflection to acquire and alter permissions on fields (the method you noticed earlier within the CAS instance). With this class, permissions are granted in accordance with the entry of the containing code as a substitute of being set immediately with reflection.

The Stack-Strolling API

The unique Unsafe.getCallerClass() is partially changed with the Stack-Strolling API delivered in JEP 259. It supplies a safer albeit extra roundabout method to entry the caller class.

Options nonetheless wanted

Some excellent Unsafe options stay to get replaced. An essential instance is the capability for creating proxies and mocks. (Ben Evans supplies a very good overview of the problem in his Java Journal article, Unsafe at any velocity.)

It is also fascinating to have a look at how real-world tasks are wrestling with this concern. For instance, contemplate how the Objenesis Git challenge handled Unsafe.defineClass() being dropped from the API.

Based mostly on this instance, we will see that there’s nonetheless work to be completed to completely exchange Unsafe‘s capabilities in creating courses with out constructors. 


The brass ring for Java’s Unsafe journey is to utterly exchange all of the memory-access options of Unsafe with approved variations. As you possibly can see, this work is not occurring multi functional place. Bits and items can be added by way of newer APIs, though VarHandle does handle an enormous swath of the work.

Having new variations of the performance is simply half the battle. The varied frameworks and tasks that presently depend on Unsafe strategies should migrate to the brand new choices, which is on no account easy. In some circumstances, it should require critical refactoring.

Progressively, the JVM will in all probability proceed to deprecate and take away the Unsafe options as legitimate alternate options solidify. It seems that Java’s stewards are dedicated to responsibly patching this long-standing space of the platform whereas making certain the steadiness of the ecosystem that relies on it.

Copyright © 2024 IDG Communications, Inc.

Supply hyperlink

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles