Install/Compile Consuming lots of memory

I was trying to install a crate GitHub - rust-cross/cargo-zigbuild: Compile Cargo project with zig as linker using cargo install cargo-zigbuild on a lima running on mac m1. I was trying to install the binary for the tirplet x86_64-unknown-linux-musl ( for cross compilation on aarch64) and it often gets killed with OOM error as shown.

[ 2539.766705] Out of memory: Killed process 3478 (cargo) total-vm:8043844kB, anon-rss:7748312kB, file-rss:0kB, shmem-rss:2116kB, UID:0 pgtables:18508kB oom_score_adj:0

If I increase the memory to 16 GiB things seem to move. Any thoughts on what I am missing ?


Do I read it correctly that thee process was consuming 2GB (2116KB) of memory? It doesn't look excessive to me. Note that cargo compiles several crates in parallel, which can easily make the peak memory consumption an order of magnitude larger than single-process compilation.

Raising memory to 16GB seems like the best solution, but if that's unreasonable, you can try reducing the parallelism of compilation. --jobs command-line option is the easiest one. This controls how many crates cargo can try to build in parallel.

rustc can also do some internal parallelism, e.g. splitting a single crate into separate codegen units. You can change the number of codegen units using the corresponding profile option, or directly as a rustc option. The latter is likely the one you should use when doing cargo install, using the RUSTFLAGS environment variable.

1 Like

@afetisov thanks for the reply.

Did further debug and found the process was consuming approx 10GB of RAM during Updates and fetch phase. After that I dont see a big increase. Adding --jobs=1 to the cargo install did not help. I am going to try other options typically using profiles/RUSTFLAGS.

The core issue is as detailed cargo build during a docker build takes forever and blows up memory on "Updating index" · Issue #10781 · rust-lang/cargo · GitHub, when using nightly and setting CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse helped solve the issue.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.