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.
getUnsafe
| Modifier and Type | Class and Description |
|---|---|
| private static enum | Unsafe.
Represents the options for the deprecated method-access methods. |
| private static class | Unsafe.
Holder for StackWalker that retains class references. |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | |
| public static final int | ARRAY_BOOLEAN_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(boolean[].class).
|
| public static final int | ARRAY_BOOLEAN_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(boolean[].class).
|
| public static final int | ARRAY_BYTE_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(byte[].class).
|
| public static final int | ARRAY_BYTE_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(byte[].class).
|
| public static final int | ARRAY_CHAR_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(char[].class).
|
| public static final int | ARRAY_CHAR_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(char[].class).
|
| public static final int | ARRAY_DOUBLE_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(double[].class).
|
| public static final int | ARRAY_DOUBLE_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(double[].class).
|
| public static final int | ARRAY_FLOAT_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(float[].class).
|
| public static final int | ARRAY_FLOAT_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(float[].class).
|
| public static final int | ARRAY_INT_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(int[].class).
|
| public static final int | ARRAY_INT_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(int[].class).
|
| public static final int | ARRAY_LONG_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(long[].class).
|
| public static final int | ARRAY_LONG_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(long[].class).
|
| public static final int | ARRAY_OBJECT_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(Object[].class).
|
| public static final int | ARRAY_OBJECT_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(Object[].class).
|
| public static final int | ARRAY_SHORT_BASE_OFFSET
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayBaseOffset(short[].class).
|
| public static final int | ARRAY_SHORT_INDEX_SCALE
Deprecated
for removal since 23. Not needed when using The value of VarHandle or java..
arrayIndexScale(short[].class).
|
| public static final int | INVALID_FIELD_OFFSET
Deprecated
for removal since 23. Not needed when using This constant differs from all results that will ever be returned from
VarHandle or java..
staticFieldOffset, objectFieldOffset,
or arrayBaseOffset.
|
| private static final Unsafe. | |
| private static final Object | |
| private static final long | |
| private static boolean | |
| private static final Unsafe | |
| private static final Unsafe |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method 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 |
| public Object | |
| public long | allocateMemory(long bytes)
Deprecated
for removal since 23. Use
java. 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..
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..
Reports the scale factor for addressing elements in the storage allocation of a given array class. |
| private static void | |
| 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
Deprecated
for removal since 23. Use
VarHandle#compareAndExchange(Object. instead.
Atomically updates Java variable to |
| public final boolean | Returns: true if successful
Deprecated
for removal since 23. Use
VarHandle#compareAndExchange(Object. instead.
Atomically updates Java variable to |
| public final boolean | Returns: true if successful
Deprecated
for removal since 23. Use
VarHandle#compareAndExchange(Object. instead.
Atomically updates Java variable to |
| 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. to allocate and free off-heap memory.
Disposes of a block of native memory, as obtained from |
| 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. to access off-heap memory.
Fetches a native pointer from a given memory address. |
| public final int | Returns: the previous valueobject/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 |
| public final long | Returns: the previous valueobject/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 |
| public final int | Returns: the previous valueobject/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 |
| public final long | Returns: the previous valueobject/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 |
| public final Object | Returns: the previous valueobject/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 |
| public boolean | getBoolean(Object o, long offset)
Deprecated
for removal since 23. Use
VarHandle#get(Object. or
MemorySegment#get(ValueLayout. instead.
|
| public boolean | getBooleanVolatile(Object o, long offset)
Deprecated
for removal since 23. Use
VarHandle#getVolatile(Object. instead.
Volatile version of |
| public byte | getByte(Object o, long offset)
Deprecated
for removal since 23. Use
VarHandle#get(Object. or
MemorySegment#get(ValueLayout. instead.
|
| public byte | getByte(long address)
Deprecated
for removal since 23. Use
java. 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 |
| public char | getChar(Object o, long offset)
Deprecated
for removal since 23. Use
VarHandle#get(Object. or
MemorySegment#get(ValueLayout. instead.
|
| public char | getChar(long address)
Deprecated
for removal since 23. Use
java. 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 |
| public double | getDouble(Object o, long offset)
Deprecated
for removal since 23. Use
VarHandle#get(Object. or
MemorySegment#get(ValueLayout. instead.
|
| public double | getDouble(long address)
Deprecated
for removal since 23. Use
java. 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 |
| public float | getFloat(Object o, long offset)
Deprecated
for removal since 23. Use
VarHandle#get(Object. or
MemorySegment#get(ValueLayout. instead.
|
| public float | getFloat(long address)
Deprecated
for removal since 23. Use
java. 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 |
| public int | Returns: the value fetched from the indicated Java variableJava 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. instead.
Fetches a value from a given Java variable. |
| public int | getInt(long address)
Deprecated
for removal since 23. Use
java. 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 |
| public int | Returns: the number of samples actually retrieved; or -1 if the load average is unobtainable.an array of double of size nelems loadavg, int the number of samples to be retrieved and
must be 1 to 3. nelems)
Deprecated
for removal since 22. Use
java.
instead.
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. instead.
|
| public long | getLong(long address)
Deprecated
for removal since 23. Use
java. 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 |
| 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. instead.
|
| public short | getShort(long address)
Deprecated
for removal since 23. Use
java. 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 |
| public static Unsafe | |
| 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 | |
| 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 | |
| public void | park(boolean isAbsolute, long time)
Deprecated
for removal since 22. Use
java. or
java. instead.
Blocks current thread, returning when a balancing
|
| public void | putAddress(long address, long x)
Deprecated
for removal since 23. Use
java. to access off-heap memory.
Stores a native pointer into a given memory address. |
| public void | putBoolean(Object o, long offset, boolean x)
Deprecated
for removal since 23. Use
VarHandle#set(Object. or
MemorySegment#set(ValueLayout. instead.
|
| public void | putBooleanVolatile(Object o, long offset, boolean x)
Deprecated
for removal since 23. Use
VarHandle#setVolatile(Object. instead.
Volatile version of |
| public void | putByte(Object o, long offset, byte x)
Deprecated
for removal since 23. Use
VarHandle#set(Object. or
MemorySegment#set(ValueLayout. instead.
|
| public void | putByte(long address, byte x)
Deprecated
for removal since 23. Use
java. 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 |
| public void | putChar(Object o, long offset, char x)
Deprecated
for removal since 23. Use
VarHandle#set(Object. or
MemorySegment#set(ValueLayout. instead.
|
| public void | putChar(long address, char x)
Deprecated
for removal since 23. Use
java. 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 |
| public void | putDouble(Object o, long offset, double x)
Deprecated
for removal since 23. Use
VarHandle#set(Object. or
MemorySegment#set(ValueLayout. instead.
|
| public void | putDouble(long address, double x)
Deprecated
for removal since 23. Use
java. 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 |
| public void | putFloat(Object o, long offset, float x)
Deprecated
for removal since 23. Use
VarHandle#set(Object. or
MemorySegment#set(ValueLayout. instead.
|
| public void | putFloat(long address, float x)
Deprecated
for removal since 23. Use
java. 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 |
| 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. instead.
Stores a value into a given Java variable. |
| public void | putInt(long address, int x)
Deprecated
for removal since 23. Use
java. 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 |
| public void | putLong(Object o, long offset, long x)
Deprecated
for removal since 23. Use
VarHandle#set(Object. or
MemorySegment#set(ValueLayout. instead.
|
| public void | putLong(long address, long x)
Deprecated
for removal since 23. Use
java. 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 |
| 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 |
| public void | putOrderedLong(Object o, long offset, long x)
Deprecated
for removal since 23. Use
VarHandle#setRelease(Object. instead.
Ordered/Lazy version of |
| public void | putOrderedObject(Object o, long offset, Object x)
Deprecated
for removal since 23. Use
VarHandle#setRelease(Object. instead.
Version of |
| public void | putShort(Object o, long offset, short x)
Deprecated
for removal since 23. Use
VarHandle#set(Object. or
MemorySegment#set(ValueLayout. instead.
|
| public void | putShort(long address, short x)
Deprecated
for removal since 23. Use
java. 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 |
| public long | reallocateMemory(long address, long bytes)
Deprecated
for removal since 23. Use
java. 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 |
| 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 |
| 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 | |
| private static boolean | |
| public void | unpark(Object
the thread to unpark. thread)
Deprecated
for removal since 22. Use
java. instead.
Unblocks the given thread blocked on |