Rust wasm "memory allocation of N bytes failed" / out of memory in CI
Building a large wasm crate (heavy generics or LTO) can use more memory than a small runner has. The process is OOM-killed or aborts with "memory allocation of N bytes failed", which looks like a crash, not a code error.
What this error means
A wasm build dies with "memory allocation of N bytes failed", an "Killed (signal 9)" message, or "rustc ... ended unexpectedly" without a normal compiler diagnostic.
error: rustc interrupted by SIGKILL, printing backtrace
memory allocation of 4294967296 bytes failed
error: could not compile `app` (lib); 1 previous error
Error: Command failed: wasm-pack build (Killed)Common causes
The compile exceeds available RAM
Link-time optimization and many monomorphized generics balloon peak memory; a small runner has too little and the kernel kills the process.
Too many parallel codegen units or jobs
High parallelism multiplies concurrent memory use, pushing total usage past the limit on a constrained runner.
How to fix it
Lower the build memory ceiling
- Reduce parallelism with
CARGO_BUILD_JOBSor-j. - Set
codegen-unitshigher to lower peak memory, or disableltofor the wasm profile. - Re-run the build.
[profile.release]
lto = false
codegen-units = 16Use a larger runner
Move the wasm build to a runner with more memory so peak usage fits.
jobs:
build:
runs-on: ubuntu-latest-8-coresHow to prevent it
- Disable LTO or raise codegen-units for memory-heavy wasm crates.
- Cap
CARGO_BUILD_JOBSon small runners. - Size the runner to the crate so the compiler is not OOM-killed.