Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce CPU usage for cargo run

During cargo run, rustc by default takes up all CPU resources. Is it possible to limit CPU usage? For example, cpulimit -l 40 works for cargo, but rustc processes still use 100% of the CPU.

  1. Fedora release 40 (Forty): 6.9.7-200.fc40.x86_64 #1 SMP PREEMPT_DYNAMIC
  2. rustc 1.79.0 (129f3b996 2024-06-10)
  3. cargo 1.79.0 (ffa9cf99a 2024-06-03)
  4. stable-x86_64-unknown-linux-gnu unchanged -rustc 1.79.0 (129f3b996 2024-06-10)

UPD: cargo build -j x or CARGO_BUILD_JOBS=x this helped.

like image 823
Artem Baranov Avatar asked Sep 03 '25 03:09

Artem Baranov


1 Answers

question was reducing cpu usage during cargo run. -j 1 does only work for cargo build.

so the combined answer of all those helpful comments would be something like this:

CARGO_BUILD_JOBS=1 systemd-run --scope --user --property CPUQuota=50% cargo run

sidenote: my problem was memory usage slowing the process down. to reduce the memory use: CARGO_BUILD_JOBS=1 systemd-run --scope --user --property CPUQuota=50% --property MemoryMax=500M --property MemorySwapMax=0

like image 191
me 5 Avatar answered Sep 05 '25 01:09

me 5