Memory usage of process in Linux

When designing an application for embedded IOT on linux platforms, memory usage of process is major constrain. To helps us understand memory usage of process, linux provides multiple commands for that purpose. This article explores the essential commands by using an example memdump process with PID#2117, i.e already running and correlates the memory information between these commands.

size -A memDumpExp | grep Total
Total                    416014    ---> ~416K bytes of memDumpExp codesize

/proc/$PID/smaps provide range of start and end address
/ # cat /proc/2117/maps
address                    perms offset      dev     inode       pathname
7f5bc000-7f61b000  r-xp     00000    00:0f   6136       /usr/bin/memDumpExp  -->389,120bytes 
7f61c000-7f622000  r--p      05f000   00:0f   6136       /usr/bin/memDumpExp  -->24576 bytes
7f622000-7f623000  rw-p    065000  00:0f    6136       /usr/bin/memDumpExp  -->4096 bytes  
7f623000-7f624000  rw-p    000000  00:00   0
802ad000-804dc000 rw-p   000000  00:00   0              [heap]        -->  2,289,664 bytes

Total code space allocated = ~417k bytes
Heap size allocated for memDumpExp = 2,289,664 bytes 

/proc/$PID/status provides memory usage per region
/ # cat /proc/2117/status
VmPeak:    58140 kB
VmSize:     58140 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:      4732 kB
VmRSS:       4732 kB    ---> sum of RssAnon + RssFile + RssShmem = Total RSS
RssAnon:     1476 kB    ---> RssAnon = Size of resident anonymous memory.(since Linux 4.5)
                                             RssAnon mentioned as dirty in pmap and top   
RssFile:            3256 kB  ---> RssFile = Number of pages the process has in real memory 
                                              (text, data, or stack space.)
RssShmem:      0 kB
VmData:    49036 kB
VmStk:       132 kB     ---> Size of data, stack, and text segments = 49168Kbytes
                                         mentioned as VSZRW in top 
.....
.....

Pmap provides memory mapping withing a process
/ # pmap -x 2117
2117: memDumpExp
Address   Kbytes     PSS   Dirty   Swap  Mode  Mapping
7f5bc000     380       220      0        0         r-xp    /usr/bin/memDumpExp
7f61c000      24        24        24      0         r--p     /usr/bin/memDumpExp
7f622000       4         4          4        0         rw-p   /usr/bin/memDumpExp   ---> Total ~412kBytes
7f623000       4         4          4        0         rw-p    [ anon ]
802ad000    2236    476     476      0         rw-p    [heap]                          --> current heap usage
.....other linkable process also considered....
.....ex: dynamic linked files.....
--------  ------  ------  ------  ------
total        58140    2252    1476      0   

htop alternative for traditional top command
/ # htop command output for memDumpExp process
PID     USER    PR  NI  CPU%  S  #THR     VSS         RSS     PCY    Name
2117     root      20   0    0.00 % S     8        58140K     4732K   fg         memDumpExp             
Notice,Vss and Rss are same as mentioned in smaps entry
 
top provides process overview in system
top command with option '-m' provide memory usage details of a process in kbytes.
 / # top -m 
  PID   VSZ   VSZRW   RSS  (SHR)  DIRTY  (SHR) STACK   COMMAND
 2117 58140  49168     4732   2920   1476        0      132        {main} memDumpExp


Notice the relation between commands below:
VSZ VmSize in smaps commad = total value of Kbytes in pmap command = 58140kBytes
RSS  RssFile+RssAnon in smaps command
VSZRW VmData + VmStk in smaps command
Dirty = RssAnon in smaps command = Dirty value in pmap command = = 1476kBytes

Comments

Popular posts from this blog

Compiler toolchains naming convention