dotnet/runtime · critical · Error
Out of memory
Error message
Out of memory
What it means
mono_wasm_load_bytes_into_heap (and the persistent variant) allocates room for a byte buffer via Module._malloc / Module._sbrk; when the allocator returns <= 0 the wasm linear memory is exhausted and the runtime throws 'Out of memory'. The persistent variant retries sbrk once before failing.
Source
Thrown at src/mono/browser/runtime/memory.ts:343
export function withStackAlloc<T1, T2, T3, TResult> (bytesWanted: number, f: (ptr: VoidPtr, ud1?: T1, ud2?: T2, ud3?: T3) => TResult, ud1?: T1, ud2?: T2, ud3?: T3): TResult {
const sp = Module.stackSave();
const ptr = Module.stackAlloc(bytesWanted);
try {
return f(ptr, ud1, ud2, ud3);
} finally {
if (loaderHelpers.is_runtime_running()) Module.stackRestore(sp);
}
}
// @bytes must be a typed array. space is allocated for it in the native heap
// and it is copied to that location. returns the address of the allocation.
export function mono_wasm_load_bytes_into_heap (bytes: Uint8Array): VoidPtr {
// pad sizes by 16 bytes for simd
const memoryOffset = malloc(bytes.length + 16);
if (<any>memoryOffset <= 0) {
mono_log_error(`malloc failed to allocate ${(bytes.length + 16)} bytes.`);
throw new Error("Out of memory");
}
const heapBytes = new Uint8Array(localHeapViewU8().buffer, <any>memoryOffset, bytes.length);
heapBytes.set(bytes);
return memoryOffset;
}
// @bytes must be a typed array. space is allocated for it in memory
// and it is copied to that location. returns the address of the data.
// the result pointer *cannot* be freed because malloc is bypassed for speed.
export function mono_wasm_load_bytes_into_heap_persistent (bytes: Uint8Array): VoidPtr {
// pad sizes by 16 bytes for simd
const desiredSize = bytes.length + 16;
// sbrk doesn't allocate whole pages so we can ask it for as many bytes as we want.
let memoryOffset = Module._sbrk(desiredSize);
if (<any>memoryOffset <= 0) {
// sbrk failed. Due to identical bugs in v8 and spidermonkey, this isn't guaranteed to be OOM.
// We use this function early during startup, when OOM shouldn't be possible anyway!
// Log a warning, then retry.View on GitHub (pinned to 290d5ab72c)
Solutions
- Increase EmccMaximumHeapSize (within the host's limit) so the heap can grow.
- Reduce the size of the buffer being loaded; chunk large payloads and free intermediate buffers.
- Free unmanaged allocations (free()) and release GCHandles/Span views to bring heap usage down.
- If during startup, also check EmccInitialHeapSize is large enough for early allocations.
Example fix
// before const ptr = mono_wasm_load_bytes_into_heap(hugeBytes); // throws when heap full // after // publish with a larger cap and chunk the payload dotnet publish -p:EmccMaximumHeapSize=2147483648 // and in JS: process in smaller slices, freeing each
Defensive patterns
Strategy: try-catch
Validate before calling
// Estimate available heap headroom before a large copy.
const bytesNeeded = bytes.length + 16;
// Module.HEAP8.byteLength vs MAXIMUM_MEMORY; this is a heuristic only.
if (Module.HEAP8 && (Module.HEAP8.buffer.byteLength + bytesNeeded) > maxMemoryBytes) {
throw new Error('Likely out of memory: requested bytes would exceed the configured MAXIMUM_MEMORY.');
} Try / catch
try {
const ptr = mono_wasm_load_bytes_into_heap(bytes);
} catch (e) {
if (String(e?.message) === 'Out of memory') {
// free unused buffers / chunk the payload and retry each chunk
}
throw e;
} Prevention
- Publish with a MAXIMUM_MEMORY large enough for peak heap use (within device limits).
- Chunk large byte payloads and free intermediate allocations.
- Release GCHandles/Span views and call free() on unmanaged buffers promptly.
- Monitor heap growth to catch leaks before they exhaust the cap.
When it happens
Trigger: Loading a large Uint8Array into the heap (mono_wasm_load_bytes_into_heap) when the wasm linear memory has reached MAXIMUM_MEMORY and cannot grow further; or persistent sbrk failing twice during early startup allocation.
Common situations: Streaming large payloads/files through interop; the heap already near EmccMaximumHeapSize; processing big binary assets; a memory leak growing the heap to the cap.
Related errors
- .NET runtime has failed to start, because too much memory wa
- value was not an integer
- value out of range
- Unsupported Webcil version: ${webcilVersion}
- Failed to initialize ICU data
AI-assisted analysis of dotnet/runtime@290d5ab72c (2026-08-06).
Data as JSON: /api/errors/224b40a6b517ea83.
Report an issue: GitHub.