Skip to content
Latchkey

Rust "ran out of memory" during LTO codegen in CI

Link-time optimization (especially fat LTO) merges every crate into one module for the optimizer, multiplying peak memory. On a memory-tight runner LLVM aborts with an out-of-memory error during codegen.

What this error means

The build fails late, during the final codegen/link, with LLVM ERROR: out of memory or an OOM kill of the codegen process. It appears with lto = "fat" or lto = true release profiles.

cargo
LLVM ERROR: out of memory
Allocation failed
error: could not compile `app` (bin "app")
# during the LTO codegen phase of a --release build

Common causes

Fat LTO holds the whole program in memory

lto = "fat" (or true) merges all crates into one LLVM module, so peak memory scales with total program size and overruns the runner.

High codegen parallelism during LTO

Multiple parallel codegen threads each consume memory, compounding the LTO peak.

How to fix it

Use thin LTO or disable it for CI

Thin LTO keeps modules separate, drastically lowering peak memory.

Cargo.toml
# Cargo.toml
[profile.release]
lto = "thin"   # or lto = false for CI builds
codegen-units = 16

Build on a larger-memory runner

.github/workflows/ci.yml
runs-on: latchkey-large

How to prevent it

  • Prefer lto = "thin" over fat LTO in CI.
  • Reserve fat LTO for release artifacts built on high-memory hosts.
  • On self-healing managed runners (Latchkey), an OOM during LTO codegen is auto-retried with larger RAM and the registry is cached, so a memory spike does not break the build.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →