APIdia Launch Partner
Top Description Inners Fields Constructors Methods
sun.misc

public final Class Unsafe

extends Object
Class Inheritance
Imports
java.lang.foreign.Arena, .MemoryLayout, .MemorySegment, .ValueLayout, java.lang.invoke.VarHandle, java.lang.reflect.Field, java.security.CodeSource, java.util.List, .Set, jdk.internal.vm.annotation.ForceInline, .Stable, jdk.internal.misc.VM, jdk.internal.reflect.CallerSensitive, .Reflection

A collection of methods for performing low-level, unsafe operations. Although the class and all methods are public, use of this class is limited because only trusted code can obtain instances of it

Note

It is the responsibility of the caller to make sure arguments are checked before methods of this class are called. While some rudimentary checks are performed on the input, the checks are best effort and when performance is an overriding priority, as when methods of this class are optimized by the runtime compiler, some or all checks (if any) may be elided. Hence, the caller must not rely on the checks and corresponding exceptions!

API Note

This class pre-dates the introduction of VarHandle, low-level access to memory with java.lang.foreign, and other standard APIs. New code should not use this API.

Author
John R. Rose
See Also
getUnsafe

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum
Unsafe.MemoryAccessOption

Represents the options for the deprecated method-access methods.

private static class
Unsafe.StackWalkerHolder

Holder for StackWalker that retains class references.

Field Summary

Modifier and TypeField and Description
public static final int
ADDRESS_SIZE

Deprecated for removal since 23. Use ValueLayout#ADDRESS.
The value of addressSize().
public static final int
ARRAY_BOOLEAN_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(boolean[].class).
public static final int
ARRAY_BOOLEAN_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(boolean[].class).
public static final int
ARRAY_BYTE_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(byte[].class).
public static final int
ARRAY_BYTE_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(byte[].class).
public static final int
ARRAY_CHAR_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(char[].class).
public static final int
ARRAY_CHAR_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(char[].class).
public static final int
ARRAY_DOUBLE_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(double[].class).
public static final int
ARRAY_DOUBLE_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(double[].class).
public static final int
ARRAY_FLOAT_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(float[].class).
public static final int
ARRAY_FLOAT_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(float[].class).
public static final int
ARRAY_INT_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(int[].class).
public static final int
ARRAY_INT_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(int[].class).
public static final int
ARRAY_LONG_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(long[].class).
public static final int
ARRAY_LONG_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(long[].class).
public static final int
ARRAY_OBJECT_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(Object[].class).
public static final int
ARRAY_OBJECT_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(Object[].class).
public static final int
ARRAY_SHORT_BASE_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayBaseOffset(short[].class).
public static final int
ARRAY_SHORT_INDEX_SCALE

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
The value of arrayIndexScale(short[].class).
public static final int
INVALID_FIELD_OFFSET

Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.
This constant differs from all results that will ever be returned from staticFieldOffset, objectFieldOffset, or arrayBaseOffset.
private static final Unsafe.MemoryAccessOption
private static final Object
private static final long
private static boolean
private static final Unsafe
private static final Unsafe

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
addressSize()
Deprecated for removal since 23. Use ValueLayout#ADDRESS.

Reports the size in bytes of a native pointer, as stored via putAddress.

public Object
allocateInstance(Class<?> cls)

Allocates an instance but does not run any constructor.

public long
allocateMemory(long bytes)
Deprecated for removal since 23. Use java.lang.foreign to allocate off-heap memory.

Allocates a new block of native memory, of the given size in bytes.

public int
arrayBaseOffset(Class<?> arrayClass)
Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.

Reports the offset of the first element in the storage allocation of a given array class.

public int
arrayIndexScale(Class<?> arrayClass)
Deprecated for removal since 23. Not needed when using VarHandle or java.lang.foreign.

Reports the scale factor for addressing elements in the storage allocation of a given array class.

private static void
beforeMemoryAccess()

Invoked by all memory-access methods.

private static void
private static String
callerAndLocation(Class<?> callerClass)

Returns a string with the caller class and the location URL from the CodeSource.

public final boolean

Returns:

true if successful
compareAndSwapInt
(Object o, long offset, int expected, int x)
Deprecated for removal since 23. Use VarHandle#compareAndExchange(Object...) instead.

Atomically updates Java variable to x if it is currently holding expected.

public final boolean

Returns:

true if successful
compareAndSwapLong
(Object o, long offset, long expected, long x)
Deprecated for removal since 23. Use VarHandle#compareAndExchange(Object...) instead.

Atomically updates Java variable to x if it is currently holding expected.

public final boolean

Returns:

true if successful
compareAndSwapObject
(Object o, long offset, Object expected, Object x)
Deprecated for removal since 23. Use VarHandle#compareAndExchange(Object...) instead.

Atomically updates Java variable to x if it is currently holding expected.

public void
copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes)
Deprecated for removal since 23. Use MemorySegment and its bulk copy methods instead.

Sets all bytes in a given block of memory to a copy of another block.

public void
copyMemory(long srcAddress, long destAddress, long bytes)
Deprecated for removal since 23. Use MemorySegment and its bulk copy methods instead.

Sets all bytes in a given block of memory to a copy of another block.

public void
freeMemory(long address)
Deprecated for removal since 23. Use java.lang.foreign to allocate and free off-heap memory.

Disposes of a block of native memory, as obtained from allocateMemory or reallocateMemory.

public void
fullFence()
Deprecated for removal since 22. Use VarHandle#fullFence() instead.

Ensures that loads and stores before the fence will not be reordered with loads and stores after the fence.

public long
getAddress(long address)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

Fetches a native pointer from a given memory address.

public final int

Returns:

the previous value
getAndAddInt
(Object
object/array to update the field/element in
o
,
long
field/element offset
offset
,
int
the value to add
delta
)
Deprecated for removal since 23. Use VarHandle#getAndAdd(Object...) instead.

Atomically adds the given value to the current value of a field or array element within the given object o at the given offset.

public final long

Returns:

the previous value
getAndAddLong
(Object
object/array to update the field/element in
o
,
long
field/element offset
offset
,
long
the value to add
delta
)
Deprecated for removal since 23. Use VarHandle#getAndAdd(Object...) instead.

Atomically adds the given value to the current value of a field or array element within the given object o at the given offset.

public final int

Returns:

the previous value
getAndSetInt
(Object
object/array to update the field/element in
o
,
long
field/element offset
offset
,
int
new value
newValue
)
Deprecated for removal since 23. Use VarHandle#getAndAdd(Object...) instead.

Atomically exchanges the given value with the current value of a field or array element within the given object o at the given offset.

public final long

Returns:

the previous value
getAndSetLong
(Object
object/array to update the field/element in
o
,
long
field/element offset
offset
,
long
new value
newValue
)
Deprecated for removal since 23. Use VarHandle#getAndAdd(Object...) instead.

Atomically exchanges the given value with the current value of a field or array element within the given object o at the given offset.

public final Object

Returns:

the previous value
getAndSetObject
(Object
object/array to update the field/element in
o
,
long
field/element offset
offset
,
Object
new value
newValue
)
Deprecated for removal since 23. Use VarHandle#getAndAdd(Object...) instead.

Atomically exchanges the given reference value with the current reference value of a field or array element within the given object o at the given offset.

public boolean
getBoolean(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#get(Object...) or MemorySegment#get(ValueLayout.OfBoolean, long) instead.

public boolean
getBooleanVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Volatile version of getBoolean(Object, long).

public byte
getByte(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#get(Object...) or MemorySegment#get(ValueLayout.OfByte, long) instead.

public byte
getByte(long address)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

Fetches a value from a given memory address.

public byte
getByteVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Volatile version of getByte(Object, long).

public char
getChar(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#get(Object...) or MemorySegment#get(ValueLayout.OfChar, long) instead.

public char
getChar(long address)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public char
getCharVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Volatile version of getChar(Object, long).

public double
getDouble(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#get(Object...) or MemorySegment#get(ValueLayout.OfDouble, long) instead.

public double
getDouble(long address)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public double
getDoubleVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Volatile version of getDouble(Object, long).

public float
getFloat(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#get(Object...) or MemorySegment#get(ValueLayout.OfFloat, long) instead.

public float
getFloat(long address)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public float
getFloatVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Volatile version of getFloat(Object, long).

public int

Returns:

the value fetched from the indicated Java variable
getInt
(Object
Java heap object in which the variable resides, if any, else null
o
,
long
indication of where the variable resides in a Java heap object, if any, else a memory address locating the variable statically
offset
)
Deprecated for removal since 23. Use VarHandle#get(Object...) or MemorySegment#get(ValueLayout.OfInt, long) instead.

Fetches a value from a given Java variable.

public int
getInt(long address)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public int
getIntVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Volatile version of getInt(Object, long).

public int

Returns:

the number of samples actually retrieved; or -1 if the load average is unobtainable.
getLoadAverage
(double[]
an array of double of size nelems
loadavg
,
int
the number of samples to be retrieved and must be 1 to 3.
nelems
)

Gets the load average in the system run queue assigned to the available processors averaged over various periods of time.

public long
getLong(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#get(Object...) or MemorySegment#get(ValueLayout.OfLong, long) instead.

public long
getLong(long address)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public long
getLongVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Volatile version of getLong(Object, long).

public Object
getObject(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#get(Object...) instead.

Fetches a reference value from a given Java variable.

public Object
getObjectVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Fetches a reference value from a given Java variable, with volatile load semantics.

public short
getShort(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#get(Object...) or MemorySegment#get(ValueLayout.OfShort, long) instead.

public short
getShort(long address)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public short
getShortVolatile(Object o, long offset)
Deprecated for removal since 23. Use VarHandle#getVolatile(Object...) instead.

Volatile version of getShort(Object, long).

public static Unsafe
getUnsafe()

Provides the caller with the capability of performing unsafe operations.

public void
invokeCleaner(ByteBuffer
a direct byte buffer
directBuffer
)
Deprecated for removal since 23. Use a MemorySegment allocated in an Arena with the appropriate temporal bounds.

Invokes the given direct byte buffer's cleaner, if any.

private static boolean
public void
loadFence()
Deprecated for removal since 22. Use VarHandle#acquireFence() instead.

Ensures that loads before the fence will not be reordered with loads and stores after the fence; a "LoadLoad plus LoadStore barrier".

private static void
log(String message)

Prints the given message to the standard error.

private static String
multiLineWarning(Class<?> callerClass, String methodName)

Return the multi-line warning message for when the given class invokes the given the Unsafe method.

public long
objectFieldOffset(Field f)
Deprecated for removal since 18. The guarantee that a field will always have the same offset and base may not be true in a future release.

Reports the location of a given field in the storage allocation of its class.

public int
pageSize()

Reports the size in bytes of a native memory page (whatever that is).

public void
park(boolean isAbsolute, long time)

Blocks current thread, returning when a balancing unpark occurs, or a balancing unpark has already occurred, or the thread is interrupted, or, if not absolute and time is not zero, the given time nanoseconds have elapsed, or if absolute, the given deadline in milliseconds since Epoch has passed, or spuriously (i.e., returning for no "reason").

public void
putAddress(long address, long x)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

Stores a native pointer into a given memory address.

public void
putBoolean(Object o, long offset, boolean x)

public void
putBooleanVolatile(Object o, long offset, boolean x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Volatile version of putBoolean(Object, long, boolean).

public void
putByte(Object o, long offset, byte x)
Deprecated for removal since 23. Use VarHandle#set(Object...) or MemorySegment#set(ValueLayout.OfByte, long, byte) instead.

public void
putByte(long address, byte x)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

Stores a value into a given memory address.

public void
putByteVolatile(Object o, long offset, byte x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Volatile version of putByte(Object, long, byte).

public void
putChar(Object o, long offset, char x)
Deprecated for removal since 23. Use VarHandle#set(Object...) or MemorySegment#set(ValueLayout.OfChar, long, char) instead.

public void
putChar(long address, char x)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public void
putCharVolatile(Object o, long offset, char x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Volatile version of putChar(Object, long, char).

public void
putDouble(Object o, long offset, double x)
Deprecated for removal since 23. Use VarHandle#set(Object...) or MemorySegment#set(ValueLayout.OfDouble, long, double) instead.

public void
putDouble(long address, double x)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public void
putDoubleVolatile(Object o, long offset, double x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Volatile version of putDouble(Object, long, double).

public void
putFloat(Object o, long offset, float x)
Deprecated for removal since 23. Use VarHandle#set(Object...) or MemorySegment#set(ValueLayout.OfFloat, long, float) instead.

public void
putFloat(long address, float x)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public void
putFloatVolatile(Object o, long offset, float x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Volatile version of putFloat(Object, long, float).

public void
putInt(Object
Java heap object in which the variable resides, if any, else null
o
,
long
indication of where the variable resides in a Java heap object, if any, else a memory address locating the variable statically
offset
,
int
the value to store into the indicated Java variable
x
)
Deprecated for removal since 23. Use VarHandle#set(Object...) or MemorySegment#set(ValueLayout.OfInt, long, int) instead.

Stores a value into a given Java variable.

public void
putInt(long address, int x)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public void
putIntVolatile(Object o, long offset, int x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Volatile version of putInt(Object, long, int).

public void
putLong(Object o, long offset, long x)
Deprecated for removal since 23. Use VarHandle#set(Object...) or MemorySegment#set(ValueLayout.OfLong, long, long) instead.

public void
putLong(long address, long x)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public void
putLongVolatile(Object o, long offset, long x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Volatile version of putLong(Object, long, long).

public void
putObject(Object o, long offset, Object x)
Deprecated for removal since 23. Use VarHandle#set(Object...) instead.

Stores a reference value into a given Java variable.

public void
putObjectVolatile(Object o, long offset, Object x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Stores a reference value into a given Java variable, with volatile store semantics.

public void
putOrderedInt(Object o, long offset, int x)
Deprecated for removal since 23. Use VarHandle#setRelease(Object...) instead.

Ordered/Lazy version of putIntVolatile(Object, long, int).

public void
putOrderedLong(Object o, long offset, long x)
Deprecated for removal since 23. Use VarHandle#setRelease(Object...) instead.

Ordered/Lazy version of putLongVolatile(Object, long, long).

public void
putOrderedObject(Object o, long offset, Object x)
Deprecated for removal since 23. Use VarHandle#setRelease(Object...) instead.

Version of putObjectVolatile(Object, long, Object) that does not guarantee immediate visibility of the store to other threads.

public void
putShort(Object o, long offset, short x)
Deprecated for removal since 23. Use VarHandle#set(Object...) or MemorySegment#set(ValueLayout.OfShort, long, short) instead.

public void
putShort(long address, short x)
Deprecated for removal since 23. Use java.lang.foreign to access off-heap memory.

public void
putShortVolatile(Object o, long offset, short x)
Deprecated for removal since 23. Use VarHandle#setVolatile(Object...) instead.

Volatile version of putShort(Object, long, short).

public long
reallocateMemory(long address, long bytes)
Deprecated for removal since 23. Use java.lang.foreign to allocate off-heap memory.

Resizes a new block of native memory, to the given size in bytes.

public void
setMemory(Object o, long offset, long bytes, byte value)
Deprecated for removal since 23. MemorySegment#fill(byte) fills the contents of a memory segment with a given value.

Sets all bytes in a given block of memory to a fixed value (usually zero).

public void
setMemory(long address, long bytes, byte value)
Deprecated for removal since 23. MemorySegment#fill(byte) fills the contents of a memory segment with a given value.

Sets all bytes in a given block of memory to a fixed value (usually zero).

private static String
singleLineWarning(Class<?> callerClass, String methodName)

Return the single-line warning message for when the given class invokes the given the Unsafe method.

public Object
staticFieldBase(Field f)
Deprecated for removal since 18. The guarantee that a field will always have the same offset and base may not be true in a future release.

Reports the location of a given static field, in conjunction with staticFieldOffset.

public long
staticFieldOffset(Field f)
Deprecated for removal since 18. The guarantee that a field will always have the same offset and base may not be true in a future release.

Reports the location of a given static field, in conjunction with staticFieldBase.

public void
storeFence()
Deprecated for removal since 22. Use VarHandle#releaseFence() instead.

Ensures that loads and stores before the fence will not be reordered with stores after the fence; a "StoreStore plus LoadStore barrier".

public void
throwException(Throwable ee)

Throws the exception without telling the verifier.

private static boolean
public void
unpark(Object
the thread to unpark.
thread
)
Deprecated for removal since 22. Use java.util.concurrent.locks.LockSupport#unpark(Thread) instead.

Unblocks the given thread blocked on park, or, if it is not blocked, causes the subsequent call to park not to block.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait