On Linux, how to determine how many memory processes are used?

I think I might have a memory leak in my LAMP application (memory is used up, swap is getting used to it, etc.). If I could see how much memory the various processes use, this can help me solve my problem. Is there any way to see this information in * nix?

+45
memory-management linux unix memory-leaks
Oct 04 '10 at 8:27
source share
13 answers

Getting the right memory usage is harder than you might think. The best way I could find ::

echo 0 $(awk '/TYPE/ {print "+", $2}' /proc/`pidof PROCESS`/smaps) | bc 

Where "PROCESS" is the name of the process you want to check, and "TYPE" is one of:

  • Rss : use of resident memory, all the memory that the process uses, including all the memory that this process shares with other processes. It does not include swap;
  • Shared : the memory that this process shares with other processes;
  • Private : the private memory used by this process, you can look for memory leaks here;
  • Swap : swap memory used by the process;
  • Pss : Proportional dial size, good overall memory indicator. This Rss is configured for sharing: if a process has 1MiB private and 20MiB shared between 10 other processes, Pss is 1 + 20/10 = 3MiB

Other valid values ​​are Size (i.e. virtual size, which is almost pointless) and Referenced (amount of memory currently marked as a link or access).

You can use watch or some other bash-script-fu to keep track of these values ​​for the processes you want to control.

For more information on smaps : http://www.kernel.org/doc/Documentation/filesystems/proc.txt .

+82
04 Oct 2018-10-10 at
source share

Use ps to find the process id for the application, then use top -p1010 (replace 1010 for the real process id). The RES column is the physical memory used, and the VIRT column is the virtual memory used - including libraries and modified memory.

More information can be found using "man top"

+30
04 Oct 2018-10-10T00:
source share

I don't know why the answer seems so complicated ... It seems pretty simple to do this with ps :

 mem() { ps -eo rss,pid,euser,args:100 --sort %mem | grep -v grep | grep -i $@ | awk '{printf $1/1024 "MB"; $1=""; print }' } 

Usage example:

 $ mem mysql 0.511719MB 781 root /bin/sh /usr/bin/mysqld_safe 0.511719MB 1124 root logger -t mysqld -p daemon.error 2.53516MB 1123 mysql /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 
+24
Nov 29 '13 at 3:14
source share

You can use pmap to report memory usage.

Description:

 pmap [ -x | -d ] [ -q ] pids... 
+13
Dec 18
source share

First get the pid:

 ps ax | grep [process name] 

And then:

 top -p PID 

You can watch different processes at the same time:

 top -p PID1 -p PID2 
+9
Apr 16 '13 at
source share

Thank. I used this to create this simple bash script that you can use to view the process and use its memory:

$ watch watchmypid.sh

 #!/bin/bash # PROCESSNAME=changethistoyourprocessname MYPID=`pidof $PROCESSNAME` echo "======="; echo PID:$MYPID echo "--------" Rss=`echo 0 $(cat /proc/$MYPID/smaps | grep Rss | awk '{print $2}' | sed 's#^#+#') | bc;` Shared=`echo 0 $(cat /proc/$MYPID/smaps | grep Shared | awk '{print $2}' | sed 's#^#+#') | bc;` Private=`echo 0 $(cat /proc/$MYPID/smaps | grep Private | awk '{print $2}' | sed 's#^#+#') | bc;` Swap=`echo 0 $(cat /proc/$MYPID/smaps | grep Swap | awk '{print $2}' | sed 's#^#+#') | bc;` Pss=`echo 0 $(cat /proc/$MYPID/smaps | grep Pss | awk '{print $2}' | sed 's#^#+#') | bc;` Mem=`echo "$Rss + $Shared + $Private + $Swap + $Pss"|bc -l` echo "Rss " $Rss echo "Shared " $Shared echo "Private " $Private echo "Swap " $Swap echo "Pss " $Pss echo "================="; echo "Mem " $Mem echo "================="; 
+6
Jul 28 '11 at 5:01
source share

More elegant approach:

 echo "Memory usage for PID <>:"; for mem in {Private,Rss,Shared,Swap,Pss};do grep $mem /proc/<pid>/smaps | awk -v mem_type="$mem" '{i=i+$2} END {print mem_type,"memory usage:"i}' ;done 
+6
Jun 04 '14 at 13:14
source share

Use top or htop and pay attention to the column "RES" (memory resident).

+4
04 Oct '10 at 8:30
source share

The tool you need is ps. To get information about what java programs do:

 ps -F -C java 

To get http information:

 ps -F -C httpd 

If your program ends before you get the opportunity to run it, open another terminal and run:

 while true; do ps -F -C myCoolCode ; sleep 0.5s ; done 
+4
Dec 18 '12 at 19:50
source share

You can use pmap + awk .

Most likely, we are interested in RSS memory, which is the third column in the last line of the pmap example below (82564).

 $ pmap -x <pid> Address Kbytes RSS Dirty Mode Mapping .... 00007f9caf3e7000 4 4 4 r---- ld-2.17.so 00007f9caf3e8000 8 8 8 rw--- ld-2.17.so 00007fffe8931000 132 12 12 rw--- [ stack ] 00007fffe89fe000 8 8 0 rx-- [ anon ] ffffffffff600000 4 0 0 rx-- [ anon ] ---------------- ------ ------ ------ total kB 688584 82564 9592 

Awk is then used to retrieve this value.

 $ pmap -x <pid> | awk '/total/ { print $4 "K" }' 

The pmap values ​​are in kilobytes. If we wanted in megabytes, we could do something like this.

 $ pmap -x <pid> | awk '/total/ { print $4 / 1024 "M" }' 
+2
Sep 17 '13 at 16:36
source share

Why all these complex answers with different shell scripts? Use htop, it automatically resizes and you can choose what information you want to show, and it works in the terminal, so it does not require a desktop. Example: htop -d8

+2
Jun 10 '15 at 3:25
source share

If you do not have an ongoing or lengthy process to track, you can use /usr/bin/time .

This is not the same as Bash time (as you will see).

For example,

 # /usr/bin/time -f "%M" echo 2028 

This is the "Maximum Resident Set Size for a Process During Its Life, in Kilobytes" (cited on the man page). That is, like RES in top , etc.

You can get much more with /usr/bin/time .

 # /usr/bin/time -v echo Command being timed: "echo" User time (seconds): 0.00 System time (seconds): 0.00 Percent of CPU this job got: 0% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 1988 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 77 Voluntary context switches: 1 Involuntary context switches: 0 Swaps: 0 File system inputs: 0 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0 
+2
Mar 22 '17 at 23:02
source share

Use

  • ps u `pidof $ TASKS_LIST` or ps u -C $ TASK
  • ps xu --sort% mem
  • ps h -o pmem -C $ TASK

Example:

 ps-of() { ps u `pidof "$@"` } $ ps-of firefox USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND const 18464 5.9 9.4 1190224 372496 ? Sl 11:28 0:33 /usr/lib/firefox/firefox $ alias ps-mem="ps xu --sort %mem | sed -e :a -e '1p;\$q;N;6,\$D;ba'" $ ps-mem USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND const 3656 0.0 0.4 565728 18648 ? Sl Nov21 0:56 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon const 11361 0.3 0.5 1054156 20372 ? Sl Nov25 43:50 /usr/bin/python /usr/bin/ubuntuone-control-panel-qt const 3402 0.0 0.5 1415848 23328 ? Sl Nov21 1:16 nautilus -n const 3577 2.3 2.0 1534020 79844 ? Sl Nov21 410:02 konsole const 18464 6.6 12.7 1317832 501580 ? Sl 11:28 1:34 /usr/lib/firefox/firefox $ ps h -o pmem -C firefox 12.7 
+1
03 Dec '13 at 9:53 on
source share



All Articles