How to check memory usage per process in Linux

How to check memory usage per process in Linux

https://www.golinuxcloud.com/check-memory-usage-per-process-linux/#:~:text=There%20are%20several%20metrics%20available%20to%20check%20memory,sum%20of%20all%20the%20regions%20shown%20in%20%2Fproc%2F%3CPID%3E%2Fmap.

Table of Contents

Linux check memory usage per process. how to check which process is using more memory in Linux. Linux how much memory is a process using. Linux track process memory usage over time. linux check memory usage per process in mb. Linux check memory. linux show memory usage. linux see memory usage. linux memory usage info. linux process memory usage. how to check memory usage in linux.


There are several metrics available to check memory usage per process in Linux. I will begin with the two that are easiest to obtain

  • the virtual set size (vss)
  • the resident memory size (rss)

both of which are available in most implementations of the ps and top commands.

 

  • Vss: called VSZ in the ps command and VIRT in top, is the total amount of memory mapped by a process. It is the sum of all the regions shown in /proc/<PID>/map. This number is of limited interest, since only part of the virtual memory is committed to physical memory at any one time.
  • Rss: called RSS in ps and RES in top, is the sum of memory that is mapped to physical pages of memory. This gets closer to the actual memory budget of the process, but there is a problem, if you add up the Rss of all the processes, you will get an overestimate the memory in use because some pages will be shared.

ALSO READ:

 

A detailed overview on Linux memory management and different types on memory in Linux

 

Using top and ps to check memory usage per process (VSS and RSS)

The ps command shows Vss (VSZ) and Rss (RSS) with the options, -Aly, and a custom format which includes vsz and rss, as shown here

# ps -eo pid,tid,class,rtprio,stat,vsz,rss,comm
  PID   TID CLS RTPRIO STAT    VSZ   RSS COMMAND
 1283  1283 TS       - S<sl  59636  1116 auditd
 1337  1337 TS       - Ssl  476848  9996 NetworkManager
 1366  1366 TS       - Ssl  553488 13196 polkitd
 1456  1456 TS       - S    267684 31768 sssd_nss
 1617  1617 TS       - Ss+  116376   872 agetty
 2955  2955 TS       - Ss    93728  2236 master
 4843  4843 TS       - Ss   156356  5156 sshd

Likewise free and top also shows a summary of the free memory and memory usage per process:

# free -m
              total        used        free      shared  buff/cache   available
Mem:         128814        7603       52253         153       68957      120183
Swap:          4091           0        4091

NOTE:

 

These simple commands give you a feel of the memory usage and give the first indication that you have a memory leak when you see that the Rss of a process keeps on increasing. However, they are not very accurate in the absolute measurements of memory usage.

 

Using smem to check memory usage per process

In 2009, Matt Mackall began looking at the problem of accounting for shared pages in process memory measurement and added two new metrics called the unique set size or Uss, and the proportional set size or Pss

  • Uss: This is the amount of memory that is committed to physical memory and is unique to a process; it is not shared with any other. It is the amount of memory that would be freed if the process were to terminate.
  • Pss: This splits the accounting of shared pages that are committed to physical memory between all the processes that have them mapped. For example, if an area of library code is 12 pages long and is shared by six processes, each will accumulate two pages in Pss. Thus, if you add the Pss numbers for all processes, you will get the actual amount of memory being used by those processes. In other words, Pss is the number we have been looking for.

ALSO READ:

 

Shell script to check top memory & cpu consuming process in Linux

 

The information is available in /proc/<PID>/smaps, which contains additional information for each of the mappings shown in /proc/<PID>/maps. Here is one section from such a file which provides information about the mapping for the libc code segment:

# cat /proc/31768/smaps | grep -i pss
Pss:                1132 kB
Pss:                   4 kB
Pss:                  44 kB
Pss:                  48 kB
Pss:               54632 kB
Pss:                   1 kB

<< Output trimmed >>

Pss:                   0 kB
Pss:                   0 kB
Pss:                   4 kB
Pss:                   4 kB
Pss:                   4 kB
Pss:                   0 kB
# cat /proc/31768/smaps | grep -i pss |  awk '{Total+=$2} END {print Total/1024" MB"}'
56.4102 MB

# cat /proc/31768/smaps | grep -i rss |  awk '{Total+=$2} END {print Total/1024" MB"}'
58.7109 MB

NOTE:

 

Note that the Rss is 58.7 M but because it is shared between many other processes, the Pss is only 56.4 MB.

There is a tool named smem that collates the information from the smaps files and presents it in various ways, including as pie or bar charts. The project page for smem is https://www.selenic.com/smem.

There are various filters which you can apply with smem as shown below with the latest available release (1.4) at the time of writing this article

# ./smem --help
Usage: smem [options]

Options:
  -h, --help            show this help message and exit
  -H, --no-header       disable header line
  -c COLUMNS, --columns=COLUMNS
                        columns to show
  -t, --totals          show totals
  -R REALMEM, --realmem=REALMEM
                        amount of physical RAM
  -K KERNEL, --kernel=KERNEL
                        path to kernel image
  -m, --mappings        show mappings
  -u, --users           show users
  -w, --system          show whole system
  -P PROCESSFILTER, --processfilter=PROCESSFILTER
                        process filter regex
  -M MAPFILTER, --mapfilter=MAPFILTER
                        map filter regex
  -U USERFILTER, --userfilter=USERFILTER
                        user filter regex
  -n, --numeric         numeric output
  -s SORT, --sort=SORT  field to sort on
  -r, --reverse         reverse sort
  -p, --percent         show percentage
  -k, --abbreviate      show unit suffixes
  --pie=PIE             show pie graph
  --bar=BAR             show bar graph
  -S SOURCE, --source=SOURCE
                        /proc data source

To check memory usage per process in total we can execute below command

# ./smem -t -k
  PID User     Command                         Swap      USS      PSS      RSS
 5496 ssrun    ssRelay -pidfile /opt/mgtse        0   112.0K   152.0K   460.0K
 5522 ssrun    ssProbeframework -pidfile /        0   116.0K   156.0K   464.0K
 2072 root     rhnsd                              0   172.0K   194.0K   616.0K
 2013 root     /usr/bin/rhsmcertd                 0   172.0K   195.0K   684.0K
 1617 root     /sbin/agetty --noclear tty1        0   184.0K   196.0K   876.0K
 1493 root     /usr/sbin/atd -f                   0   260.0K   271.0K     1.1M
22058 root     /usr/sbin/rpc.idmapd               0   380.0K   384.0K   676.0K
 7802 root     /usr/sbin/xinetd -stayalive        0   396.0K   408.0K     1.1M

<< Output trimmed >> 
 
10901 rpc      /sbin/rpcbind -w                   0   608.0K   655.0K     1.4M
11885 oamsys   -bash                              0   524.0K   739.0K     2.2M
 4847 oamsys   -bash                              0   528.0K   743.0K     2.2M
 2008 root     /usr/sbin/oddjobd -n -p /va        0   632.0K   764.0K     1.9M
22202 sufuser  /usr/sbin/httpd -DFOREGROUN        0   372.0K   768.0K     5.4M
22203 sufuser  /usr/sbin/httpd -DFOREGROUN        0   372.0K   768.0K     5.4M
27326 sufuser  /usr/sbin/httpd -DFOREGROUN        0   372.0K   768.0K     5.4M
22199 sufuser  /usr/sbin/httpd -DFOREGROUN        0   372.0K   769.0K     5.4M
27327 sufuser  /usr/sbin/httpd -DFOREGROUN        0   372.0K   769.0K     5.4M
22201 sufuser  /usr/sbin/httpd -DFOREGROUN        0   380.0K   775.0K     5.4M
22200 sufuser  /usr/sbin/httpd -DFOREGROUN        0   380.0K   776.0K     5.4M
12096 root     -bash                              0   580.0K   809.0K     2.3M
 7989 ntp      /usr/sbin/ntpd -u ntp:ntp -        0   780.0K   814.0K     1.7M
11564 oamsys   /usr/libexec/openssh/sftp-s        0   744.0K   834.0K     2.4M
 5523 ssrun    ssProbeframework -pidfile /        0   185.3M   191.3M   198.4M
-------------------------------------------------------------------------------
   93 11                                          0     1.9G     2.0G     2.1G

This will give you memory usage detail of every process active on your system.

To get the memory usage of a single process we can grep the process from the list

# ./smem -k | sed -e '1p' -e '/amsHelper/!d' | grep -v sed
  PID User     Command                         Swap      USS      PSS      RSS
31768 root     /sbin/amsHelper -f                 0    56.0M    56.4M    58.7M

Here as you see we get similar results as above with /proc/31768/smaps and with smem tool. So the actual memory usage of amsHelper is 56.4 MB

 

Lastly I hope the steps from the article to check memory usage per process on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值