如何找出我的 linux 应用程序的内存占用量?

How to find out the memory footprint of my linux application?

我正在 linux 上开发一个 c 应用程序 运行 作为守护进程。

我的应用程序正在加载 3 个共享库 (*.so)。有时它会执行 shell 脚本。

我是否应该在计算足迹时考虑脚本 shell 和 3 个共享库?

如何找出我的应用程序的内存占用量?

编辑

是否可以从 /proc/$pid/maps 中提取足迹?

root@Linux:~# cat /proc/25998/maps
00400000-00412000 r-xp 00000000 1f:00 2823       /usr/sbin/mydaemond
00422000-00423000 rw-p 00012000 1f:00 2823       /usr/sbin/mydaemond
005a9000-005db000 rwxp 00000000 00:00 0          [heap]
76bd3000-76bd4000 ---p 00000000 00:00 0
76bd4000-76dd3000 rw-p 00000000 00:00 0          [stack:26009]
76dd3000-76dd4000 ---p 00000000 00:00 0
76dd4000-76fd3000 rw-p 00000000 00:00 0          [stack:26008]
76fd3000-76fd4000 ---p 00000000 00:00 0
76fd4000-771d3000 rw-p 00000000 00:00 0          [stack:26007]
771d3000-771d4000 ---p 00000000 00:00 0
771d4000-773d3000 rw-p 00000000 00:00 0          [stack:26006]
773d3000-773d4000 ---p 00000000 00:00 0
773d4000-775d3000 rw-p 00000000 00:00 0          [stack:26005]
775d3000-775d6000 r-xp 00000000 1f:00 799        /lib/libdl-0.9.33.2.so
775d6000-775e5000 ---p 00000000 00:00 0
775e5000-775e6000 r--p 00002000 1f:00 799        /lib/libdl-0.9.33.2.so
775e6000-775e7000 rw-p 00003000 1f:00 799        /lib/libdl-0.9.33.2.so
775e7000-775f9000 r-xp 00000000 1f:00 1436       /usr/lib/libz.so.1.2.7
775f9000-77608000 ---p 00000000 00:00 0
77608000-77609000 rw-p 00011000 1f:00 1436       /usr/lib/libz.so.1.2.7
77609000-7764e000 r-xp 00000000 1f:00 1402       /usr/lib/libssl.so.1.0.0
7764e000-7765e000 ---p 00000000 00:00 0
7765e000-77662000 rw-p 00045000 1f:00 1402       /usr/lib/libssl.so.1.0.0
77662000-77781000 r-xp 00000000 1f:00 1253       /usr/lib/libcrypto.so.1.0.0
77781000-77790000 ---p 00000000 00:00 0
77790000-777a1000 rw-p 0011e000 1f:00 1253       /usr/lib/libcrypto.so.1.0.0
777a1000-777a2000 rw-p 00000000 00:00 0
777a2000-77806000 r-xp 00000000 1f:00 808        /lib/libuClibc-0.9.33.2.so
77806000-77815000 ---p 00000000 00:00 0
77815000-77816000 r--p 00063000 1f:00 808        /lib/libuClibc-0.9.33.2.so
77816000-77817000 rw-p 00064000 1f:00 808        /lib/libuClibc-0.9.33.2.so
77817000-7781d000 rw-p 00000000 00:00 0
7781d000-77830000 r-xp 00000000 1f:00 801        /lib/libgcc_s.so.1
77830000-77840000 ---p 00000000 00:00 0
77840000-77841000 rw-p 00013000 1f:00 801        /lib/libgcc_s.so.1
77841000-77849000 r-xp 00000000 1f:00 1329       /usr/lib/libjson-c.so.2.0.1
77849000-77858000 ---p 00000000 00:00 0
77858000-77859000 rw-p 00007000 1f:00 1329       /usr/lib/libjson-c.so.2.0.1
77859000-7785a000 r-xp 00000000 1f:00 1331       /usr/lib/libjson.so.0.1.0
7785a000-77869000 ---p 00000000 00:00 0
77869000-7786a000 rw-p 00000000 1f:00 1331       /usr/lib/libjson.so.0.1.0
7786a000-7787d000 r-xp 00000000 1f:00 804        /lib/libpthread-0.9.33.2.so
7787d000-7788c000 ---p 00000000 00:00 0
7788c000-7788d000 r--p 00012000 1f:00 804        /lib/libpthread-0.9.33.2.so
7788d000-7788e000 rw-p 00013000 1f:00 804        /lib/libpthread-0.9.33.2.so
7788e000-77890000 rw-p 00000000 00:00 0
77890000-778d5000 r-xp 00000000 1f:00 1255       /usr/lib/libcurl.so.4.3.0
778d5000-778e4000 ---p 00000000 00:00 0
778e4000-778e6000 rw-p 00044000 1f:00 1255       /usr/lib/libcurl.so.4.3.0
778e6000-778f0000 r-xp 00000000 1f:00 1346       /usr/lib/libmicroxml.so.1.0
778f0000-778ff000 ---p 00000000 00:00 0
778ff000-77900000 rw-p 00009000 1f:00 1346       /usr/lib/libmicroxml.so.1.0
77900000-77904000 r-xp 00000000 1f:00 810        /lib/libubus.so
77904000-77913000 ---p 00000000 00:00 0
77913000-77914000 rw-p 00003000 1f:00 810        /lib/libubus.so
77914000-7791b000 r-xp 00000000 1f:00 809        /lib/libubox.so
7791b000-7792a000 ---p 00000000 00:00 0
7792a000-7792b000 rw-p 00006000 1f:00 809        /lib/libubox.so
7792b000-77934000 r-xp 00000000 1f:00 811        /lib/libuci.so
77934000-77943000 ---p 00000000 00:00 0
77943000-77944000 rw-p 00008000 1f:00 811        /lib/libuci.so
77944000-7794b000 r-xp 00000000 1f:00 793        /lib/ld-uClibc-0.9.33.2.so
77957000-7795a000 rw-p 00000000 00:00 0
7795a000-7795b000 r--p 00006000 1f:00 793        /lib/ld-uClibc-0.9.33.2.so
7795b000-7795c000 rw-p 00007000 1f:00 793        /lib/ld-uClibc-0.9.33.2.so
7fb30000-7fb51000 rwxp 00000000 00:00 0          [stack]
7fff7000-7fff8000 r-xp 00000000 00:00 0          [vdso]

对于二进制文件 valgrind 的 massif tool is very useful. To help with processing the data I would also recommend massif-visualizer。使用这些工具,您可以找到正在分配内存的函数以及它们在任何特定时间占用的内存量。

鉴于您的应用程序旨在 运行 作为守护进程,最重要的考虑因素是内存使用量在其整个生命周期内保持不变。

您应该考虑共享库: https://en.wikipedia.org/wiki/Memory_footprint