How can I measure the actual memory usage of an application or process?

后端 未结 30 3220
心在旅途
心在旅途 2020-11-22 03:01

This question is covered here in great detail.

How do you measure the memory usage of an application or process in Linux?

From the blog articl

30条回答
  •  爱一瞬间的悲伤
    2020-11-22 03:32

    There isn't a single answer for this because you can't pin point precisely the amount of memory a process uses. Most processes under Linux use shared libraries.

    For instance, let's say you want to calculate memory usage for the 'ls' process. Do you count only the memory used by the executable 'ls' (if you could isolate it)? How about libc? Or all these other libraries that are required to run 'ls'?

    linux-gate.so.1 =>  (0x00ccb000)
    librt.so.1 => /lib/librt.so.1 (0x06bc7000)
    libacl.so.1 => /lib/libacl.so.1 (0x00230000)
    libselinux.so.1 => /lib/libselinux.so.1 (0x00162000)
    libc.so.6 => /lib/libc.so.6 (0x00b40000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x00cb4000)
    /lib/ld-linux.so.2 (0x00b1d000)
    libattr.so.1 => /lib/libattr.so.1 (0x00229000)
    libdl.so.2 => /lib/libdl.so.2 (0x00cae000)
    libsepol.so.1 => /lib/libsepol.so.1 (0x0011a000)
    

    You could argue that they are shared by other processes, but 'ls' can't be run on the system without them being loaded.

    Also, if you need to know how much memory a process needs in order to do capacity planning, you have to calculate how much each additional copy of the process uses. I think /proc/PID/status might give you enough information of the memory usage at a single time. On the other hand, Valgrind will give you a better profile of the memory usage throughout the lifetime of the program.

提交回复
热议问题