Open Source Projects:
Pricing
Chat
Contact
Back to top

Using sun.misc.Unsafe in Java 9

February 28, 2017

The Java 9 EA version is out and we can now see how to use sun.misc.Unsafe. I led the public campaign to retain access to it in Java 9 which was ultimately successful, leading to the amendments to JEP 260.

So, how did things end up?

Getting Set Up

First you need to download Java 9 EA. For an IDE I use IntelliJ IDEA. You need the new 2017.1 Public Preview which came out 27 February 2017. Earlier versions don’t work with Java 9.

The jdk.unsupported module

sun.misc.Unsafe is now available in the jdk.unsupported module. This module is present in the full JRE and JDK images.

Here is the module declaration for jdk.unsupported:

module jdk.unsupported {
    exports sun.misc;
    exports sun.reflect;
    exports com.sun.nio.file;

    opens sun.misc;
    opens sun.reflect;
}

As you can see sun.misc is exported.

Using It

I have a sample project with a package java9unsafe and a module with the same name.

To use Unsafe, you need to add jdk.unsupported to your code’s module declaration:

module java9unsafe {
    requires jdk.unsupported;
}

Fortunately, IDEA will detect the declaration if missing and suggest adding it for you when you hover over your import statement.

Then you can use Unsafe. Note you have to indirectly get at the Unsafe instance via reflection otherwise you get a security exception.

module jdk.unsupported {

public class Java9Unsafe {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        System.out.println("The address size is: " + getUnsafe().addressSize());
    }

    @SuppressWarnings("restriction")
    private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
        Field singleoneInstanceField = Unsafe.class.getDeclaredField("theUnsafe");
        singleoneInstanceField.setAccessible(true);
        return (Unsafe) singleoneInstanceField.get(null);
    }

}

And the answer: The address size is: 8

About the Author

About the Author

Greg Luck

Greg Luck

Chief Technical Officer

Greg Luck is a leading technology entrepreneur with more than 15 years of experience in high-performance in-memory computing. He is the founder and inventor of Ehcache, a widely used open source Java distributed cache that was acquired by Software AG (Terracotta) in 2009, where he served as CTO. Prior to that, Greg was the Chief Architect at Australian start-up Wotif.com that went public on the Australian Stock Exchange (ASX:WTF) in 2006. Greg is a current member of the Java Community Process (JCP) Executive Committee, and since 2007 has been the Specification Lead for JSR 107 (Java Specification Requests) JCACHE. Greg has a master's degree in Information Technology from Queensland University of Technology and a Bachelor of Commerce from the University of Queensland.

Latest Blogs

Redis Load Handling vs Data Integrity: Tradeoffs in Distributed Data Store Design

The One Beam to Rule Them All

The One Beam to Rule Them All

Hazelcast Responds to Redis Labs' Benchmark

Hazelcast Responds to Redis Labs’ Benchmark

View all blogs by the author

Free Hazelcast Online Training Center

Whether you're interested in learning the basics of in-memory systems, or you're looking for advanced, real-world production examples and best practices, we've got you covered.