如何在 Linux 上测量应用程序结束后的峰值内存

des*_*ser 17 linux memory

如何测量在 Linux 上运行的应用程序的峰值内存?

我批量运行此应用程序,因此无法使用 RSS,因为它会报告当前内存。我需要应用程序最后报告的峰值内存。

VmPeak 也不是一个解决方案,因为它报告分配的内存并且也不是从真实的 Ram 计算,也不是从硬盘计算。

slm*_*slm 17

这里有 2 种跟踪进程峰值内存使用情况的方法。

糖浆

我没有使用过这个工具,但它听起来像你正在寻找的东西。它被称为糖浆

描述

Syrupy 是一个 Python 脚本,它定期对一个或多个正在运行的进程的内存和 CPU 负载进行快照,以便动态地构建它们对系统资源使用情况的配置文件。

例子

$ syrupy.py myprog

  PID DATE        TIME     ELAPSED  CPU   MEM    RSS   VSIZE
14634 2008-10-10  20:45:25   00:00  0.0   0.0   2996    6680
14634 2008-10-10  20:45:26   00:01  105   0.2   7804   12592
14634 2008-10-10  20:45:27   00:02  103   0.2   8996   13776
14634 2008-10-10  20:45:28   00:03  103   0.2  10468   15348
14634 2008-10-10  20:45:29   00:04  103   0.3  11412   16396
14634 2008-10-10  20:45:30   00:05  104   0.3  12492   17444
Run Code Online (Sandbox Code Playgroud)

/usr/bin/time -v

是的,具有讽刺意味的是,GNU 时间命令可以为您提供进程的峰值内存使用量。它报告的峰值内存像这样:Maximum resident set size (kbytes)

例子

$ /usr/bin/time -v ~/projects/prime_numbers/eratosthenes_prime_sieve.pl 10 1000000
...

    Command being timed: "/home/saml/projects/prime_numbers/eratosthenes_prime_sieve.pl 10 1000000"
    User time (seconds): 1.12
    System time (seconds): 0.05
    Percent of CPU this job got: 54%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:02.19
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 79304
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 20014
    Voluntary context switches: 83
    Involuntary context switches: 274
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0
Run Code Online (Sandbox Code Playgroud)

参考


Vla*_*lov 5

尽管这个话题已经很老了,但我想分享另一个从 cgroups Linux 内核功能中出现的项目。

https://github.com/gsauthof/cgmemtime

cgmemtime 测量进程及其后代进程的高水位 RSS+CACHE 内存使用情况。

为了能够这样做,它将进程放入自己的 cgroup 中。

例如,进程 A 分配 10 MiB 并分叉分配 20 MiB 的子进程 B,并分叉分配 30 MiB 的子进程 C。所有三个进程共享一个时间窗口,它们的分配导致相应的 RSS(常驻集大小)内存使用。

现在的问题是:运行A的结果是实际使用了多少内存?

答案:60 MiB

cgmemtime 是回答此类问题的工具。

用法示例是:

$ sudo ./cgmemtime --setup -g <myusergroup> --perm 775

$ ./cgmemtime ./testa x 10 20 30
Parent PID is 27189
Allocating 10 MiBs
New Child: 27193
Allocating 20 MiBs
New Child: 27194
Allocating 30 MiBs
Child user:    0.000 s
Child sys :    0.005 s
Child wall:    6.006 s
Child high-water RSS                    :      11648 KiB
Recursive and acc. high-water RSS+CACHE :      61840 KiB

$ ./cgmemtime python -c 'print range(100000)[48517]'
48517
Child user:    0.014 s
Child sys :    0.014 s
Child wall:    0.029 s
Child high-water RSS                    :       9948 KiB
Recursive and acc. high-water RSS+CACHE :       5724 KiB
Run Code Online (Sandbox Code Playgroud)

  • @terdon 是的,我做到了!因为我搜索了同样的问题,问题几乎一样,答案都一样:'解析ps输出',这太疯狂了!我花了一个星期才不小心碰到了 cgmemtime 项目,这是我评论过的所有这些问题的完美匹配! (2认同)