jdk.internal.misc.Unsafe#allocateUninitializedArray.
Unlike new T[length], the returned arrays are not zeroed by the JVM. Callers must
overwrite every element they use before reading it, otherwise they will observe stale memory
contents. Use only on the hot path where the zeroing cost is measurable and the array is
guaranteed to be fully written before being read.
Requires the JVM to be started with
--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED so that reflective access to
the singleton Unsafe instance is permitted. When the reflective bootstrap fails
(e.g. the flag is missing or a future JDK changes the internals), this class transparently
falls back to zero-initialized new T[length] allocation after logging a warning.
TLAB-size limit: the HotSpot _allocateUninitializedArray intrinsic
only skips zeroing on the JIT-inlined TLAB fast path. Allocations that don't fit in the
current thread's TLAB (typically arrays larger than a few hundred KB, depending on
-XX:TLABSize and occupancy) fall back to OptoRuntime::new_array_C →
MemAllocator::allocate, which always zeros the array body before returning it.
The intrinsic cannot override that — large heap allocations are zeroed by the GC's
allocator regardless of which API requested them. As a result this helper is only a win
for small scratch arrays (per-batch decode buffers, tens of KB). For multi-MB
buffers, the only way to avoid the zeroing cost is to pool and reuse the buffer instead
of allocating a fresh one.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidstatic booleanReturnstruewhen the reflective bootstrap ofjdk.internal.misc.Unsafe#allocateUninitializedArraysucceeded and thenewXxxArraymethods will return uninitialized arrays.static boolean[]newBooleanArray(int length) static byte[]newByteArray(int length) static char[]newCharArray(int length) static double[]newDoubleArray(int length) static float[]newFloatArray(int length) static int[]newIntArray(int length) static long[]newLongArray(int length) static short[]newShortArray(int length)
-
Method Details
-
isUnsafeEnabled
public static boolean isUnsafeEnabled()Returnstruewhen the reflective bootstrap ofjdk.internal.misc.Unsafe#allocateUninitializedArraysucceeded and thenewXxxArraymethods will return uninitialized arrays. Returnsfalsewhen the JVM is missing--add-opens=java.base/jdk.internal.misc=ALL-UNNAMED(or the JDK internals changed), in which case thenewXxxArraymethods silently fall back to zero-initializednew T[length]. -
ensureUnsafeEnabled
public static void ensureUnsafeEnabled()ThrowsIllegalStateExceptionifisUnsafeEnabled()isfalse. Use from production code paths or tests that require the uninitialized fast path and must fail loudly rather than silently fall back to zero-initialized allocation. -
newBooleanArray
public static boolean[] newBooleanArray(int length) -
newByteArray
public static byte[] newByteArray(int length) -
newShortArray
public static short[] newShortArray(int length) -
newCharArray
public static char[] newCharArray(int length) -
newIntArray
public static int[] newIntArray(int length) -
newLongArray
public static long[] newLongArray(int length) -
newFloatArray
public static float[] newFloatArray(int length) -
newDoubleArray
public static double[] newDoubleArray(int length)
-