Skip to content
Latchkey

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.

cargo
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

  1. Reduce parallelism with CARGO_BUILD_JOBS or -j.
  2. Set codegen-units higher to lower peak memory, or disable lto for the wasm profile.
  3. Re-run the build.
Cargo.toml
[profile.release]
lto = false
codegen-units = 16

Use a larger runner

Move the wasm build to a runner with more memory so peak usage fits.

.github/workflows/ci.yml
jobs:
  build:
    runs-on: ubuntu-latest-8-cores

How to prevent it

  • Disable LTO or raise codegen-units for memory-heavy wasm crates.
  • Cap CARGO_BUILD_JOBS on small runners.
  • Size the runner to the crate so the compiler is not OOM-killed.

Frequently asked questions

What causes ""memory allocation ... failed""?
Link-time optimization and many monomorphized generics balloon peak memory; a small runner has too little and the kernel kills the process.
How do I fix "memory allocation ... failed"?
Lower the build memory ceiling

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card