在 CentOS 上跑 Rust 程序或 rustc / cargo 编译 时出现“卡顿”,原因和解决办法要看卡在哪里。下面按常见场景给你一套排查和解决思路。
Debug 编译本身慢且生成的程序也慢。
✅ 解决:
cargo build --release
Rust 编译器是内存大户,尤其:
✅ 查看资源:
top
htop
free -h
✅ 限制并行编译(避免 OOM):
export CARGO_BUILD_JOBS=2
或写进 ~/.cargo/config.toml:
[build]
jobs = 2
CentOS 7 的 glibc 很老,Rust 新版本工具链可能不稳定。
✅ 建议:
查看:
rustc --version
ldd --version
/target 目录大量小文件,HDD 会卡死。
✅ 解决:
export CARGO_TARGET_DIR=/tmp/cargo-target
✅ 排查:
cargo flamegraph
或:
perf top -p <pid>
例如:
std::thread::sleep
std::net::TcpStream
✅ 解决:
tokio有时网络程序“假死”。
✅ 检查:
getenforce
systemctl status firewalld
vim /etc/ssh/sshd_config
改为:
UseDNS no
重启:
systemctl restart sshd
rustc -V
cargo -V
nproc
free -h
df -h
如果你愿意,可以告诉我:
我可以直接给你针对性优化方案。