Skip to main content

Advanced Wasm Memory Customization

Different Wasm workloads will be using the memory in very different ways.

Make sure you don't do changes without proper benchmarking and analysis.

It's possible to provide a custom implementation of the entire Memory used by the Wasm module:

var instance = Instance.builder(module).withMemoryFactory(limits -> {
return new ByteArrayMemory(limits);
}).build();

NOTE: Since Chicory 1.1.0, an optimized memory implementation called ByteArrayMemory is also available. We recommend plugging this implementation on all recent OpenJDK systems for enhanced performance. On different Java runtimes (in particular, on Android VMs) you should stick to ByteBufferMemory.

It's also possible to customize just the Memory allocation algorithm, for example, is easy to swap out from the default allocator and get the legacy behavior of mapping one to one the backing ByteBuffer to the actual request of the Wasm module:

import com.dylibso.chicory.runtime.alloc.ExactMemAllocStrategy;

var instance = Instance.builder(module).withMemoryFactory(limits -> {
var allocator = new ExactMemAllocStrategy();
return new ByteBufferMemory(limits, allocator);
}).build();