bash 在 Linux 中查找进程的内存使用情况

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22261452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:50:30  来源:igfitidea点击:

Finding memory usage of a process in Linux

linuxbash

提问by supereater14

I am trying to find the current memory usage of a particular process. Every guide I've found online so far gives instructions on how to get the usage as a percentage of total memory. I want thr process' ram usae as a discrete value (i.e. in megabytes).

我正在尝试查找特定进程的当前内存使用情况。到目前为止,我在网上找到的每个指南都提供了有关如何获得使用占总内存百分比的说明。我希望 thr 进程的 ram 使用作为一个离散值(即以兆字节为单位)。

回答by mklement0

To only get a single memory-usage number of interest, try:

要只获得一个感兴趣的内存使用数量,请尝试:

 ps -o rss= $pid   # resident set in kbytes; e.g., 2461016
 ps -o vsz= $pid   # virtual size in kbytes; e.g., 1048

To convert to MB (rounded to an integer, adjust as needed), try something like:

要转换为 MB(四舍五入为整数,根据需要进行调整),请尝试以下操作:

ps -o rss= $pid | awk '{printf "%.0f\n",  / 1024}' # e.g., 1

回答by Sammitch

If you know the process ID:

如果您知道进程 ID:

cat /proc/$pid/status

If you want the usage from within the process:

如果您希望在流程中使用:

cat /proc/self/status # or read the file with fopen() or whatnot

Which will output something like:

这将输出如下内容:

# cat /proc/7049/status
Name:   bash
State:  S (sleeping)
Tgid:   7049
Pid:    7049
PPid:   7042
TracerPid:      0
Uid:    500     500     500     500
Gid:    500     500     500     500
Utrace: 0
FDSize: 256
Groups: 10 500
VmPeak:   108524 kB
VmSize:   108492 kB
VmLck:         0 kB
VmHWM:      2080 kB
VmRSS:      2072 kB
VmData:      532 kB
VmStk:        88 kB
VmExe:       848 kB
VmLib:      1876 kB
VmPTE:        60 kB
VmSwap:        0 kB
Threads:        1
SigQ:   0/14888
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000010000
SigIgn: 0000000000384004
SigCgt: 000000004b813efb
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: ffffffffffffffff
Cpus_allowed:   1
Cpus_allowed_list:      0
Mems_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list:      0
voluntary_ctxt_switches:        164
nonvoluntary_ctxt_switches:     4

The Vm*fields contain the information you seek.

这些Vm*字段包含您要查找的信息。

Note: the exact format of this can vary from distro to distro.

注意:这个的确切格式可能因发行版而异。

Or, more simply:

或者,更简单地说:

ps up $pid

Which gives something like:

这给出了类似的东西:

# ps up 7049
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
stack     7049  0.0  0.1 108492  2072 pts/0    Ss   20:23   0:00 -bash

回答by Gio

You can use

您可以使用

ps aux --sort=%mem,%cpu

To sort all running processes by their memory & CPU usage.

按内存和 CPU 使用情况对所有正在运行的进程进行排序。

If you want to limit to just the process which you are running from the current terminal:

如果您只想限制在当前终端运行的进程:

ps v

If you know the pid use:

如果您知道 pid 使用:

ps v <pid>

Example output:

示例输出:

PID    TTY STAT  TIME PGIN  SIZE   RSS   LIM  TSIZ   TRS %CPU %MEM COMMAND
36626  pts/3 A     0:00    0   316   408 32768    51    60  0.0  0.0 ps v

where

在哪里

PGINNumber of page-ins caused by page faults. Since all I/O is classified as page faults, this is basically a measure of I/O volume.

PGIN页错误引起的页码数。由于所有 I/O 都被归类为页面错误,因此这基本上是对 I/O 量的衡量。

SIZEVirtual size (in paging space) in kilobytes of the data section of the process (displayed as SZ by other flags). This number is equal to the number of working segment pages of the process that have been touched times 4. If some working segment pages are currently paged out, this number is larger than the amount of real memory being used. SIZE includes pages in the private segment and the shared-library data segment of the process.

SIZE进程数据部分的虚拟大小(在分页空间中)(以千字节为单位)(由其他标志显示为 SZ)。这个数字等于进程的工作段页面已经被触摸的数量的4倍。如果当前有一些工作段页面被调出,这个数字大于正在使用的实内存量。SIZE 包括进程的私有段和共享库数据段中的页。

RSSReal-memory (resident set) size in kilobytes of the process. This number is equal to the sum of the number of working segment and code segment pages in memory times 4. Remember that code segment pages are shared among all of the currently running instances of the program. If 26 ksh processes are running, only one copy of any given page of the ksh executable program would be in memory, but the ps command would report that code segment size as part of the RSS of each instance of the ksh program.

RSS进程的实内存(驻留集)大小(以千字节为单位)。这个数字等于内存中工作段和代码段页数的总和乘以 4。记住代码段页是在所有当前运行的程序实例之间共享的。如果 26 个 ksh 进程正在运行,则 ksh 可执行程序的任何给定页面只有一个副本在内存中,但 ps 命令会将该代码段大小报告为每个 ksh 程序实例的 RSS 的一部分。

TSIZSize of text (shared-program) image. This is the size of the text section of the executable file. Pages of the text section of the executable program are only brought into memory when they are touched, that is, branched to or loaded from. This number represents only an upper bound on the amount of text that could be loaded. The TSIZ value does not reflect actual memory usage. This TSIZ value can also be seen by executing the dump -ov command against an executable program (for example, dump -ov /usr/bin/ls).

TSIZ文本(共享程序)图像的大小。这是可执行文件的文本部分的大小。可执行程序的文本部分的页面只有在它们被触摸时才会被带入内存,即分支到或从中加载。该数字仅代表可以加载的文本量的上限。TSIZ 值不反映实际内存使用情况。也可以通过对可执行程序(例如,dump -ov /usr/bin/ls)执行 dump -ov 命令来查看该 TSIZ 值。

TRSSize of the resident set (real memory) of text. This is the number of code segment pages times 4. This number exaggerates memory use for programs of which multiple instances are running. The TRS value can be higher than the TSIZ value because other pages may be included in the code segment such as the XCOFF header and the loader section.

TRS文本常驻集(真实内存)的大小。这是代码段页数乘以 4。这个数字夸大了运行多个实例的程序的内存使用情况。TRS 值可以高于 TSIZ 值,因为其他页面可能包含在代码段中,例如 XCOFF 标头和加载器部分。

%MEMCalculated as the sum of the number of working segment and code segment pages in memory times 4 (that is, the RSS value), divided by the size of the real memory in use, in the machine in KB, times 100, rounded to the nearest full percentage point. This value attempts to convey the percentage of real memory being used by the process. Unfortunately, like RSS, it tends the exaggerate the cost of a process that is sharing program text with other processes. Further, the rounding to the nearest percentage point causes all of the processes in the system that have RSS values under 0.005 times real memory size to have a %MEM of 0.0.

%MEM计算为内存中工作段和代码段页数之和乘以4(即RSS值),除以实际使用的内存大小,以KB为单位,乘以100,四舍五入到最接近的完整百分点。该值试图传达进程正在使用的实内存的百分比。不幸的是,与 RSS 一样,它倾向于夸大与其他进程共享程序文本的进程的成本。此外,四舍五入到最接近的百分比会导致系统中 RSS 值低于实际内存大小 0.005 倍的所有进程的 %MEM 为 0.0。

回答by Basile Starynkevitch

You probably want to learn about the virtual memoryand the address spaceused by your processsince e.g. C memory management(i.e. malloc(3)etc...) is consuming (and releasing) virtual memory (gotten with mmap(2)etc...).

您可能想了解进程使用的虚拟内存地址空间,因为例如C 内存管理(即malloc(3)等)正在消耗(和释放)虚拟内存(通过mmap(2)等获得。 ...)。

Then you should use the /proc/file system (see proc(5)....). In particular, for process of pid 1234, try cat /proc/1234/mapsand cat /proc/1234/status. See also pmap(1)and try pmap 1234

然后你应该使用/proc/文件系统(参见proc(5)....)。特别是对于pid 1234的进程,尝试cat /proc/1234/mapscat /proc/1234/status。另请参阅pmap(1)并尝试pmap 1234

BTW, /proc/is the way the kernel gives information about processes (and most system status; see also /sys/). In particular, ps, top, pmap, htopare all using /proc/

顺便说一句,/proc/是内核提供有关进程(以及大多数系统状态;另请参阅/sys/)的信息的方式。特别是,ps, top, pmap,htop都在使用/proc/

FWIW, I'm using zshas my interactive shell, and here is what cat /proc/$$/mapsgives me:

FWIW,我zsh用作我的交互式 shell,这是cat /proc/$$/maps给我的:

 00400000-004a5000 r-xp 00000000 08:01 2883633                            /bin/zsh5
 006a4000-006a5000 r--p 000a4000 08:01 2883633                            /bin/zsh5
 006a5000-006ab000 rw-p 000a5000 08:01 2883633                            /bin/zsh5
 006ab000-006bf000 rw-p 00000000 00:00 0 
 01e5c000-01faf000 rw-p 00000000 00:00 0                                  [heap]
 7f2fa1533000-7f2fa1564000 rw-p 00000000 00:00 0 
 7f2fa1564000-7f2fa1574000 r-xp 00000000 08:01 1049819                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/computil.so
 7f2fa1574000-7f2fa1773000 ---p 00010000 08:01 1049819                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/computil.so
 7f2fa1773000-7f2fa1774000 r--p 0000f000 08:01 1049819                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/computil.so
 7f2fa1774000-7f2fa1775000 rw-p 00010000 08:01 1049819                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/computil.so
 7f2fa1775000-7f2fa1785000 rw-p 00000000 00:00 0 
 7f2fa1785000-7f2fa1788000 r-xp 00000000 08:01 1049839                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/rlimits.so
 7f2fa1788000-7f2fa1988000 ---p 00003000 08:01 1049839                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/rlimits.so
 7f2fa1988000-7f2fa1989000 r--p 00003000 08:01 1049839                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/rlimits.so
 7f2fa1989000-7f2fa198a000 rw-p 00004000 08:01 1049839                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/rlimits.so
 7f2fa198a000-7f2fa19c0000 rw-p 00000000 00:00 0 
 7f2fa19c0000-7f2fa19c3000 r-xp 00000000 08:01 1049830                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/stat.so
 7f2fa19c3000-7f2fa1bc2000 ---p 00003000 08:01 1049830                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/stat.so
 7f2fa1bc2000-7f2fa1bc3000 r--p 00002000 08:01 1049830                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/stat.so
 7f2fa1bc3000-7f2fa1bc4000 rw-p 00003000 08:01 1049830                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/stat.so
 7f2fa1bc4000-7f2fa1bcd000 r-xp 00000000 08:01 1055723                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/parameter.so
 7f2fa1bcd000-7f2fa1dcd000 ---p 00009000 08:01 1055723                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/parameter.so
 7f2fa1dcd000-7f2fa1dce000 r--p 00009000 08:01 1055723                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/parameter.so
 7f2fa1dce000-7f2fa1dcf000 rw-p 0000a000 08:01 1055723                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/parameter.so
 7f2fa1dcf000-7f2fa1dd6000 r-xp 00000000 08:01 1056697                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/zutil.so
 7f2fa1dd6000-7f2fa1fd5000 ---p 00007000 08:01 1056697                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/zutil.so
 7f2fa1fd5000-7f2fa1fd6000 r--p 00006000 08:01 1056697                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/zutil.so
 7f2fa1fd6000-7f2fa1fd7000 rw-p 00007000 08:01 1056697                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/zutil.so
 7f2fa1fd7000-7f2fa1ff9000 r-xp 00000000 08:01 1049816                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/complete.so
 7f2fa1ff9000-7f2fa21f9000 ---p 00022000 08:01 1049816                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/complete.so
 7f2fa21f9000-7f2fa21fa000 r--p 00022000 08:01 1049816                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/complete.so
 7f2fa21fa000-7f2fa21fb000 rw-p 00023000 08:01 1049816                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/complete.so
 7f2fa21fb000-7f2fa21fc000 rw-p 00000000 00:00 0 
 7f2fa21fc000-7f2fa223d000 r-xp 00000000 08:01 1049825                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/zle.so
 7f2fa223d000-7f2fa243d000 ---p 00041000 08:01 1049825                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/zle.so
 7f2fa243d000-7f2fa243e000 r--p 00041000 08:01 1049825                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/zle.so
 7f2fa243e000-7f2fa2445000 rw-p 00042000 08:01 1049825                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/zle.so
 7f2fa2445000-7f2fa2446000 rw-p 00000000 00:00 0 
 7f2fa2446000-7f2fa2448000 r-xp 00000000 08:01 1055724                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/terminfo.so
 7f2fa2448000-7f2fa2647000 ---p 00002000 08:01 1055724                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/terminfo.so
 7f2fa2647000-7f2fa2648000 r--p 00001000 08:01 1055724                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/terminfo.so
 7f2fa2648000-7f2fa2649000 rw-p 00002000 08:01 1055724                    /usr/lib/x86_64-linux-gnu/zsh/5.0.5/zsh/terminfo.so
 7f2fa2649000-7f2fa2655000 r-xp 00000000 08:01 4985164                    /lib/x86_64-linux-gnu/libnss_files-2.18.so
 7f2fa2655000-7f2fa2854000 ---p 0000c000 08:01 4985164                    /lib/x86_64-linux-gnu/libnss_files-2.18.so
 7f2fa2854000-7f2fa2855000 r--p 0000b000 08:01 4985164                    /lib/x86_64-linux-gnu/libnss_files-2.18.so
 7f2fa2855000-7f2fa2856000 rw-p 0000c000 08:01 4985164                    /lib/x86_64-linux-gnu/libnss_files-2.18.so
 7f2fa2856000-7f2fa2860000 r-xp 00000000 08:01 4985173                    /lib/x86_64-linux-gnu/libnss_nis-2.18.so
 7f2fa2860000-7f2fa2a5f000 ---p 0000a000 08:01 4985173                    /lib/x86_64-linux-gnu/libnss_nis-2.18.so
 7f2fa2a5f000-7f2fa2a60000 r--p 00009000 08:01 4985173                    /lib/x86_64-linux-gnu/libnss_nis-2.18.so
 7f2fa2a60000-7f2fa2a61000 rw-p 0000a000 08:01 4985173                    /lib/x86_64-linux-gnu/libnss_nis-2.18.so
 7f2fa2a61000-7f2fa2a76000 r-xp 00000000 08:01 4985159                    /lib/x86_64-linux-gnu/libnsl-2.18.so
 7f2fa2a76000-7f2fa2c75000 ---p 00015000 08:01 4985159                    /lib/x86_64-linux-gnu/libnsl-2.18.so
 7f2fa2c75000-7f2fa2c76000 r--p 00014000 08:01 4985159                    /lib/x86_64-linux-gnu/libnsl-2.18.so
 7f2fa2c76000-7f2fa2c77000 rw-p 00015000 08:01 4985159                    /lib/x86_64-linux-gnu/libnsl-2.18.so
 7f2fa2c77000-7f2fa2c79000 rw-p 00000000 00:00 0 
 7f2fa2c79000-7f2fa2c80000 r-xp 00000000 08:01 4985166                    /lib/x86_64-linux-gnu/libnss_compat-2.18.so
 7f2fa2c80000-7f2fa2e7f000 ---p 00007000 08:01 4985166                    /lib/x86_64-linux-gnu/libnss_compat-2.18.so
 7f2fa2e7f000-7f2fa2e80000 r--p 00006000 08:01 4985166                    /lib/x86_64-linux-gnu/libnss_compat-2.18.so
 7f2fa2e80000-7f2fa2e81000 rw-p 00007000 08:01 4985166                    /lib/x86_64-linux-gnu/libnss_compat-2.18.so
 7f2fa2e81000-7f2fa2e85000 r-xp 00000000 08:01 4980783                    /lib/x86_64-linux-gnu/libattr.so.1.1.0
 7f2fa2e85000-7f2fa3084000 ---p 00004000 08:01 4980783                    /lib/x86_64-linux-gnu/libattr.so.1.1.0
 7f2fa3084000-7f2fa3085000 r--p 00003000 08:01 4980783                    /lib/x86_64-linux-gnu/libattr.so.1.1.0
 7f2fa3085000-7f2fa3086000 rw-p 00004000 08:01 4980783                    /lib/x86_64-linux-gnu/libattr.so.1.1.0
 7f2fa3086000-7f2fa3226000 r-xp 00000000 08:01 4985168                    /lib/x86_64-linux-gnu/libc-2.18.so
 7f2fa3226000-7f2fa3425000 ---p 001a0000 08:01 4985168                    /lib/x86_64-linux-gnu/libc-2.18.so
 7f2fa3425000-7f2fa3429000 r--p 0019f000 08:01 4985168                    /lib/x86_64-linux-gnu/libc-2.18.so
 7f2fa3429000-7f2fa342b000 rw-p 001a3000 08:01 4985168                    /lib/x86_64-linux-gnu/libc-2.18.so
 7f2fa342b000-7f2fa342f000 rw-p 00000000 00:00 0 
 7f2fa342f000-7f2fa3530000 r-xp 00000000 08:01 4985165                    /lib/x86_64-linux-gnu/libm-2.18.so
 7f2fa3530000-7f2fa3730000 ---p 00101000 08:01 4985165                    /lib/x86_64-linux-gnu/libm-2.18.so
 7f2fa3730000-7f2fa3731000 r--p 00101000 08:01 4985165                    /lib/x86_64-linux-gnu/libm-2.18.so
 7f2fa3731000-7f2fa3732000 rw-p 00102000 08:01 4985165                    /lib/x86_64-linux-gnu/libm-2.18.so
 7f2fa3732000-7f2fa3757000 r-xp 00000000 08:01 4980894                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
 7f2fa3757000-7f2fa3957000 ---p 00025000 08:01 4980894                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
 7f2fa3957000-7f2fa395b000 r--p 00025000 08:01 4980894                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
 7f2fa395b000-7f2fa395c000 rw-p 00029000 08:01 4980894                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
 7f2fa395c000-7f2fa395f000 r-xp 00000000 08:01 4985160                    /lib/x86_64-linux-gnu/libdl-2.18.so
 7f2fa395f000-7f2fa3b5e000 ---p 00003000 08:01 4985160                    /lib/x86_64-linux-gnu/libdl-2.18.so
 7f2fa3b5e000-7f2fa3b5f000 r--p 00002000 08:01 4985160                    /lib/x86_64-linux-gnu/libdl-2.18.so
 7f2fa3b5f000-7f2fa3b60000 rw-p 00003000 08:01 4985160                    /lib/x86_64-linux-gnu/libdl-2.18.so
 7f2fa3b60000-7f2fa3b64000 r-xp 00000000 08:01 4980821                    /lib/x86_64-linux-gnu/libcap.so.2.22
 7f2fa3b64000-7f2fa3d63000 ---p 00004000 08:01 4980821                    /lib/x86_64-linux-gnu/libcap.so.2.22
 7f2fa3d63000-7f2fa3d64000 rw-p 00003000 08:01 4980821                    /lib/x86_64-linux-gnu/libcap.so.2.22
 7f2fa3d64000-7f2fa3d84000 r-xp 00000000 08:01 4985158                    /lib/x86_64-linux-gnu/ld-2.18.so
 7f2fa3d85000-7f2fa3d91000 rw-p 00000000 00:00 0 
 7f2fa3d91000-7f2fa3f59000 r--p 00000000 08:01 7083454                    /usr/lib/locale/locale-archive
 7f2fa3f59000-7f2fa3f5e000 rw-p 00000000 00:00 0 
 7f2fa3f60000-7f2fa3f70000 rw-p 00000000 00:00 0 
 7f2fa3f72000-7f2fa3f7a000 rw-p 00000000 00:00 0 
 7f2fa3f7a000-7f2fa3f81000 r--s 00000000 08:01 7112364                    /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
 7f2fa3f81000-7f2fa3f83000 rw-p 00000000 00:00 0 
 7f2fa3f83000-7f2fa3f84000 r--p 0001f000 08:01 4985158                    /lib/x86_64-linux-gnu/ld-2.18.so
 7f2fa3f84000-7f2fa3f85000 rw-p 00020000 08:01 4985158                    /lib/x86_64-linux-gnu/ld-2.18.so
 7f2fa3f85000-7f2fa3f86000 rw-p 00000000 00:00 0 
 7ffff69af000-7ffff69d0000 rw-p 00000000 00:00 0                          [stack]
 7ffff69fe000-7ffff6a00000 r-xp 00000000 00:00 0                          [vdso]
 ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

Read also about ASLRand read Advanced Linux Programming.

另请阅读ASLR高级 Linux 编程

回答by Basile Starynkevitch

In the Terminal you can run top. It will give you a KiloByte representation of the memory usage per process. You can even use grepto make a snapshot of a certain process' usage.

在终端中,您可以运行top. 它将为您提供每个进程的内存使用情况的 KiloByte 表示。您甚至可以grep用来制作某个进程使用情况的快照。