Rust 在 Linux 的内存管理配置指南
一 全局分配器与编译配置
[profile.release]
debug = "full"
split-debuginfo = "off"
strip = "none"
[dependencies]
tikv-jemallocator = { version = "0.6", optional = true, features = ["stats", "profiling"] }
tikv-jemalloc-ctl = { version = "0.6", optional = true }
tikv-jemalloc-sys = { version = "0.6", optional = true }
[features]
default = ["jemalloc-stats", "jemalloc-prof"]
jemalloc = ["dep:tikv-jemallocator"]
jemalloc-prof = ["jemalloc", "dep:tikv-jemalloc-ctl", "tikv-jemallocator/profiling", "tikv-jemalloc-sys/profiling"]
jemalloc-stats = ["jemalloc", "dep:tikv-jemalloc-ctl", "dep:tikv-jemalloc-sys", "tikv-jemalloc-sys/stats", "tikv-jemallocator/stats"]
#[cfg(feature = "jemalloc")]
#[global_allocator]
static JEMALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
#[cfg(feature = "jemalloc-prof")]
#[export_name = "_rjem_malloc_conf"]
static JEMALLOC_CONF: &[u8] = b"prof:true,prof_active:true,lg_prof_sample:16,prof_accum:true\0";
cargo install flamegraph
RUSTFLAGS="-C target-cpu=native" cargo flamegraph --bin your_program
sudo perf record -g target/release/your_program
sudo perf report
二 运行时内存行为调优
三 系统层面的内存与资源配置
ulimit -n 65535
sudo sysctl -w vm.max_map_count=262144
四 监控诊断与验证
sudo perf record -g target/release/your_program
sudo perf report
cargo install flamegraph
RUSTFLAGS="-C target-cpu=native" cargo flamegraph --bin your_program