• 如何使用memory-stats crate来分析Rust进程使用了多少内存?
  • 发布于 2个月前
  • 576 热度
    0 评论
  • pckillers
  • 0 粉丝 50 篇博客
  •   
在篇文章中,我们使用memory-stats crate来报告和分析Rust进程使用了多少内存,它依赖于操作系统的内存计算。
使用以下命令创建一个Rust新项目:
cargo new memory-stats-example
加入以下依赖项:
[dependencies]
memory-stats = { version = "1.1.0", features = ["always_use_statm"] }
thousands = "0.2.0"
基本上我们分析两种内存:
物理内存:对应于Linux和MacOS上的常驻内存集大小和Windows上的工作内存集大小。
虚拟内存:对应于Linux和MacOS上的虚拟内存大小和Windows上的页面内存使用情况。

在我们的例子中,创建了包含许多字符的变量,在创建变量之前和之后,打印内存差异。

在src/main.rs文件中写入以下代码:
use memory_stats::memory_stats;
use thousands::Separable;

fn main() {
    show_mem();

    println!("         字节          物理内存       虚拟内存  ");
    check_mem(10000);
    check_mem(100000);
    check_mem(1000000);
    check_mem(10000000);
    check_mem(100000000);
    check_mem(1000000000);
    check_mem(10000000000);
}

fn check_mem(bytes: usize) {
    let before = memory_stats().unwrap();
    let _text = "x".repeat(bytes);
    let after = memory_stats().unwrap();

    let physical_mem = after.physical_mem - before.physical_mem;
    let virtual_mem = after.virtual_mem - before.virtual_mem;
    println!(
        "{:>15} {:>15} {:>15}",
        bytes.separate_with_commas(),
        physical_mem.separate_with_commas(),
        virtual_mem.separate_with_commas()
    )
}

fn show_mem() {
    if let Some(usage) = memory_stats() {
        println!(
            "物理内存使用: {:>15}",
            usage.physical_mem.separate_with_commas()
        );
        println!(
            "虚拟内存使用:  {:>15}",
            usage.virtual_mem.separate_with_commas()
        );
    } else {
        println!("Couldn't get the current memory usage :(");
    }
}
把这个程序运行了3次,看看结果是否一致。
cargo run -q
物理内存使用:       1,966,080
虚拟内存使用:        3,338,240
            字节         物理内存         虚拟内存
         10,000               0               0
        100,000               0               0
      1,000,000       1,048,576       1,003,520
     10,000,000       9,961,472      10,002,432
    100,000,000      99,876,864     100,003,840
  1,000,000,000     999,948,288   1,000,001,536
 10,000,000,000   9,999,876,096  10,000,003,072

cargo run -q
物理内存使用:       1,966,080
虚拟内存使用:        3,338,240
            字节         物理内存         虚拟内存
         10,000               0               0
        100,000               0               0
      1,000,000       1,048,576       1,003,520
     10,000,000       9,961,472      10,002,432
    100,000,000      99,876,864     100,003,840
  1,000,000,000     999,817,216   1,000,001,536
 10,000,000,000   9,999,876,096  10,000,003,072

cargo run -q
物理内存使用:       1,966,080
虚拟内存使用:        3,338,240
            字节         物理内存         虚拟内存
         10,000         131,072               0
        100,000               0               0
      1,000,000       1,048,576       1,003,520
     10,000,000       9,961,472      10,002,432
    100,000,000      99,876,864     100,003,840
  1,000,000,000     999,948,288   1,000,001,536
 10,000,000,000   9,999,876,096  10,000,003,072
对于10,000和100,000字节,在两次执行中得到0更改,并且在第三次运行中得到单个131,072更改。从1,000,000字节开始,结果在3次运行中相当一致,它们也表明已使用内存的变化类似于创建字符串的大小。
用户评论