I think I may have a memory leak in my LAMP application (memory gets used up, swap starts getting used, etc.). If I could see how much memory the various processes are using
You can use pmap to report memory usage.
Synopsis:
pmap [ -x | -d ] [ -q ] pids...
The tool you want is ps. To get information about what java programs are doing:
ps -F -C java
To get information about http:
ps -F -C httpd
If your program is ending before you get a chance to run these, open another terminal and run:
while true; do ps -F -C myCoolCode ; sleep 0.5s ; done
First get the pid:
ps ax | grep [process name]
And then:
top -p PID
You can watch various processes in the same time:
top -p PID1 -p PID2
You can use pmap
+ awk
.
Most likely, we're interested in the RSS
memory which is the 3rd column in the last line of the example pmap
output 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 r-x-- [ anon ]
ffffffffff600000 4 0 0 r-x-- [ anon ]
---------------- ------ ------ ------
total kB 688584 82564 9592
Awk is then used to extract that value.
$ pmap -x <pid> | awk '/total/ { print $4 "K" }'
The pmap
values are in kilobytes. If we wanted it in megabytes, we could do something like this.
$ pmap -x <pid> | awk '/total/ { print $4 / 1024 "M" }'
Getting right memory usage is trickier than one may think. The best way I could find is:
echo 0 $(awk '/TYPE/ {print "+", $2}' /proc/`pidof PROCESS`/smaps) | bc
Where "PROCESS" is the name of the process you want to inspect and "TYPE" is one of:
Rss
: resident memory usage, all memory the process uses, including all memory this process shares with other processes. It does not include swap;Shared
: memory that this process shares with other processes;Private
: private memory used by this process, you can look for memory leaks here;Swap
: swap memory used by the process;Pss
: Proportional Set Size, a good overall memory indicator. It is the Rss adjusted for sharing: if a process has 1MiB private and 20MiB shared between other 10 processes, Pss is 1 + 20/10 = 3MiBOther valid values are Size
(i.e. virtual size, which is almost meaningless) and Referenced
(the amount of memory currently marked as referenced or accessed).
You can use watch
or some other bash-script-fu to keep an eye on those values for processes that you want to monitor.
For more informations about smaps
: http://www.kernel.org/doc/Documentation/filesystems/proc.txt.
Use ps
to find the process id for the application, then use top -p1010
(substitute 1010 for the real process id).
The RES column is the used physical memory and the VIRT column is the used virtual memory - including libraries and swapped memory.
More info can be found using "man top"