Low-level unsafe memory operations and platform-specific functionality providing direct memory access, copying, allocation, and platform feature detection using sun.misc.Unsafe.
Direct memory read and write operations for primitive types, bypassing Java's memory safety mechanisms for maximum performance.
/**
* Core unsafe memory operations and platform-specific functionality
*/
final class Platform {
// Integer operations
/**
* Read an int value from memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @return int value at the location
*/
public static int getInt(Object object, long offset);
/**
* Write an int value to memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @param value int value to store
*/
public static void putInt(Object object, long offset, int value);
// Long operations
/**
* Read a long value from memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @return long value at the location
*/
public static long getLong(Object object, long offset);
/**
* Write a long value to memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @param value long value to store
*/
public static void putLong(Object object, long offset, long value);
// Boolean operations
/**
* Read a boolean value from memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @return boolean value at the location
*/
public static boolean getBoolean(Object object, long offset);
/**
* Write a boolean value to memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @param value boolean value to store
*/
public static void putBoolean(Object object, long offset, boolean value);
// Byte operations
/**
* Read a byte value from memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @return byte value at the location
*/
public static byte getByte(Object object, long offset);
/**
* Write a byte value to memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @param value byte value to store
*/
public static void putByte(Object object, long offset, byte value);
// Short operations
/**
* Read a short value from memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @return short value at the location
*/
public static short getShort(Object object, long offset);
/**
* Write a short value to memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @param value short value to store
*/
public static void putShort(Object object, long offset, short value);
// Float operations
/**
* Read a float value from memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @return float value at the location
*/
public static float getFloat(Object object, long offset);
/**
* Write a float value to memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @param value float value to store
*/
public static void putFloat(Object object, long offset, float value);
// Double operations
/**
* Read a double value from memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @return double value at the location
*/
public static double getDouble(Object object, long offset);
/**
* Write a double value to memory at the specified offset
* @param object Base object (null for off-heap addresses)
* @param offset Offset within object or direct address
* @param value double value to store
*/
public static void putDouble(Object object, long offset, double value);
}Usage Examples:
import org.apache.spark.unsafe.Platform;
// Reading and writing to a byte array
byte[] data = new byte[16];
Platform.putLong(data, Platform.BYTE_ARRAY_OFFSET, 42L);
long value = Platform.getLong(data, Platform.BYTE_ARRAY_OFFSET);
// Working with off-heap memory
long address = Platform.allocateMemory(1024);
try {
Platform.putInt(null, address, 123);
int storedValue = Platform.getInt(null, address);
} finally {
Platform.freeMemory(address);
}Memory operations with volatile semantics for concurrent programming scenarios.
/**
* Read an object reference with volatile semantics
* @param object Base object
* @param offset Offset within object
* @return Object reference at the location
*/
public static Object getObjectVolatile(Object object, long offset);
/**
* Write an object reference with volatile semantics
* @param object Base object
* @param offset Offset within object
* @param value Object reference to store
*/
public static void putObjectVolatile(Object object, long offset, Object value);Direct memory allocation and deallocation operations for off-heap memory management.
/**
* Allocate off-heap memory
* @param size Size in bytes to allocate
* @return Address of allocated memory
* @throws OutOfMemoryError if allocation fails
*/
public static long allocateMemory(long size);
/**
* Free previously allocated off-heap memory
* @param address Address of memory to free
*/
public static void freeMemory(long address);
/**
* Reallocate off-heap memory to a new size
* @param address Address of existing memory
* @param oldSize Previous size in bytes
* @param newSize New size in bytes
* @return Address of reallocated memory (may be different)
*/
public static long reallocateMemory(long address, long oldSize, long newSize);
/**
* Allocate a direct ByteBuffer
* @param size Size in bytes
* @return Direct ByteBuffer of specified size
*/
public static ByteBuffer allocateDirectBuffer(int size);Efficient memory copying and filling operations optimized for bulk data movement.
/**
* Copy memory from source to destination
* @param src Source object (null for off-heap)
* @param srcOffset Source offset or address
* @param dst Destination object (null for off-heap)
* @param dstOffset Destination offset or address
* @param length Number of bytes to copy
*/
public static void copyMemory(Object src, long srcOffset, Object dst, long dstOffset, long length);
/**
* Fill memory region with a specific byte value
* @param object Base object (null for off-heap)
* @param offset Offset within object or direct address
* @param size Number of bytes to fill
* @param value Byte value to fill with
*/
public static void setMemory(Object object, long offset, long size, byte value);
/**
* Fill off-heap memory region with a specific byte value
* @param address Direct memory address
* @param value Byte value to fill with
* @param size Number of bytes to fill
*/
public static void setMemory(long address, byte value, long size);Usage Examples:
import org.apache.spark.unsafe.Platform;
// Memory copying between arrays
byte[] source = "Hello".getBytes();
byte[] dest = new byte[source.length];
Platform.copyMemory(
source, Platform.BYTE_ARRAY_OFFSET,
dest, Platform.BYTE_ARRAY_OFFSET,
source.length
);
// Fill array with zeros
byte[] buffer = new byte[1024];
Platform.setMemory(buffer, Platform.BYTE_ARRAY_OFFSET, buffer.length, (byte) 0);
// Working with off-heap memory
long srcAddr = Platform.allocateMemory(100);
long dstAddr = Platform.allocateMemory(100);
try {
Platform.setMemory(srcAddr, (byte) 0xFF, 100);
Platform.copyMemory(null, srcAddr, null, dstAddr, 100);
} finally {
Platform.freeMemory(srcAddr);
Platform.freeMemory(dstAddr);
}Methods to detect platform capabilities and features for optimization purposes.
/**
* Check if platform supports unaligned memory access
* @return true if unaligned access is supported
*/
public static boolean unaligned();
/**
* Check if cleaner creation method is available
* @return true if cleaner method is defined
*/
public static boolean cleanerCreateMethodIsDefined();Utility for bypassing Java's exception checking mechanisms.
/**
* Throw an exception bypassing compiler checks
* @param t Throwable to throw
*/
public static void throwException(Throwable t);Pre-calculated offsets for different array types to optimize array access operations.
// Array base offsets for different primitive types
public static final int BOOLEAN_ARRAY_OFFSET; // Offset for boolean arrays
public static final int BYTE_ARRAY_OFFSET; // Offset for byte arrays
public static final int SHORT_ARRAY_OFFSET; // Offset for short arrays
public static final int INT_ARRAY_OFFSET; // Offset for int arrays
public static final int LONG_ARRAY_OFFSET; // Offset for long arrays
public static final int FLOAT_ARRAY_OFFSET; // Offset for float arrays
public static final int DOUBLE_ARRAY_OFFSET; // Offset for double arraysUsage Examples:
import org.apache.spark.unsafe.Platform;
// Efficient array access using pre-calculated offsets
int[] array = {1, 2, 3, 4, 5};
// Read first element
int first = Platform.getInt(array, Platform.INT_ARRAY_OFFSET);
// Write to second element
Platform.putInt(array, Platform.INT_ARRAY_OFFSET + 4, 999);
// Check platform capabilities
if (Platform.unaligned()) {
// Can use unaligned memory access optimizations
System.out.println("Platform supports unaligned access");
}⚠️ Important: Platform operations bypass Java's memory safety mechanisms: