Skip to main content

Memory Customization

Endive ships two implementations of linear memory, and withMemoryFactory selects which one backs an instance:

var instance = Instance.builder(module).
withMemoryFactory(ByteArrayMemory::new).
build();

Choosing an implementation​

ImplementationPlain loads and storesAtomic instructionsWorks on
ByteBufferMemory (default)GoodAlways take a lockEvery JDK and Android
ByteArrayMemoryFastest on every JDKLock-free up to JDK 21, locked from JDK 23Every JDK

Atomic instructions only matter for modules built with the threads proposal that share their memory between threads. ByteArrayMemory is the faster choice on a standard JVM, and ByteBufferMemory is the safe default where that cannot be assumed, such as Android. From JDK 23 the JDK no longer provides lock-free multi-byte atomics on byte arrays, so a shared memory performs the same with either implementation there.

Workloads use memory in very different ways, so benchmark before changing the default.

Redline keeps linear memory off-heap with lock-free atomics; that memory comes with the Redline runners and is not selectable here.

Any class implementing Memory can be passed to withMemoryFactory, for example an off-heap implementation of your own. The factory receives the module's declared limits, or the ones set with withMemoryLimits.