Standard Java arrays live on the JVM heap, which means the garbage collector can move or scan them at any moment. When TornadoVM needs to transfer data to a GPU it must either pin the array (blocking GC) or copy it to a separate native buffer — either way introducing latency and unpredictability. TornadoVM solves this by providing a set of off-heap data types built on Java’s Foreign Function & Memory API (Project Panama). These types allocate aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Deepak-Sangle/TornadoVM/llms.txt
Use this file to discover all available pages before exploring further.
MemorySegment — a contiguous block of native memory outside the JVM heap — that the TornadoVM runtime can map directly to device buffers, eliminate unnecessary copies, and free at deterministic points without GC involvement. Starting from TornadoVM v1.0, using these types is the recommended approach for all GPU-accelerated code.
Primitive Array Types
Each Java primitive array type has a direct off-heap counterpart:| Java heap array | TornadoVM off-heap type |
|---|---|
int[] | IntArray |
float[] | FloatArray |
double[] | DoubleArray |
long[] | LongArray |
short[] | ShortArray |
byte[] | ByteArray |
char[] | CharArray |
HalfFloat[] | HalfFloatArray |
uk.ac.manchester.tornado.api.types.arrays.
Unlike Java primitive arrays, off-heap types are not zero-initialised on allocation. Always call
.clear() or .init(value) before use unless you immediately overwrite every element.Creating Off-Heap Arrays
- Constructor
- fromArray (copy from heap)
- fromElements (varargs)
- fromSegment (Memory API)
Allocate a segment for a fixed number of elements. Contents are uninitialised.
Core Instance Methods
The following methods are available on all off-heap array types. The examples below useFloatArray, but the API is identical for IntArray, DoubleArray, LongArray, ShortArray, ByteArray, and CharArray.
get(int index) — read a value
get(int index) — read a value
set(int index, T value) — write a value
set(int index, T value) — write a value
init(T value) — fill the entire array with one value
init(T value) — fill the entire array with one value
clear() — zero all elements
clear() — zero all elements
getSize() — element count
getSize() — element count
toHeapArray() — convert back to Java array
toHeapArray() — convert back to Java array
getNumBytesOfSegment() / getNumBytesOfSegmentWithHeader()
getNumBytesOfSegment() / getNumBytesOfSegmentWithHeader()
Migration from Java Primitive Arrays
Migrating existing TornadoVM code from on-heap primitive arrays to the off-heap API requires three targeted changes:Vector Types
TornadoVM ships SIMD-aware vector types that map to GPU hardware vector registers. Vector types use stack-like construction and support element-wise math operations. All vector types reside inuk.ac.manchester.tornado.api.types.vectors.
Float vectors
Float2, Float3, Float4, Float8, Float16Int vectors
Int2, Int3, Int4, Int8, Int16Double vectors
Double2, Double3, Double4, Double8, Double16Creating and Using Vector Types
TornadoMath utilities.
Matrix Types
TornadoVM provides 2D and 3D matrix types built on top ofFloatArray, DoubleArray, and IntArray.
| Type | Package |
|---|---|
Matrix2DFloat, Matrix2DDouble, Matrix2DInt | uk.ac.manchester.tornado.api.types.matrix |
Matrix3DFloat, Matrix3DDouble, Matrix3DInt | uk.ac.manchester.tornado.api.types.matrix |
Matrix2DFloat4 | uk.ac.manchester.tornado.api.types.matrix |
Matrix Usage Example
Matrix2DFloat internally stores data in a flat FloatArray in row-major order. You can access the backing store via .getArray() if you need to pass the raw segment to another API.Zero-Copy Semantics
When a TornadoVM off-heap array is registered with aTaskGraph, the runtime maps the underlying MemorySegment directly to the corresponding device buffer. This means:
- No intermediate copy is required when transferring to device — the native memory is passed to the OpenCL / CUDA driver as-is.
- Pinning is deterministic — the segment is pinned only during the actual transfer, not throughout the JVM session.
- No GC interference — because the segment is outside the Java heap, the garbage collector never moves or invalidates the backing memory.