Linux memory usage test

In Linux, how to tell how much memory processes are using?

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, it might help me resolve my problem. Is there a way for me to see this information in *nix?

Can you update the question to indicate the OS that you were using at the time? The selected answer doesn’t seem to work 11 years later. Ah, nevermind.

13 Answers 13

Getting right memory usage is trickier than one may think. The best way I could find is:

echo 0 $(awk '/TYPE/ ' /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 = 3MiB

Other 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.

This is terrific, however it looks like it returns memory in KB (for Rss and Private anyway). Do you know how to get memory in bytes?

Ages later and probably not relevant anymore, but: actual memory allocation is always a multiple of the physical page size, which on modern systems is always a small multiple of 1024 bytes. So just multiply the size in KB by 1024 for bytes; there is no rounding error. (The kernel has mostly not caught the iB disease: unless there is clear evidence to the contrary, assume K = 1024 not 1000.)

Читайте также:  Editing pdf in linux

What would be the «total» memory consumed for a scenario such as this: gist.github.com/9bbd0ce953143b67c038 ?

You can do the cat+grep+awk+sed with just awk: echo 0 $(sudo awk ‘/TYPE/ ‘ /proc/PID/smaps) | bc

Why not do it all in awk instead of passing to bc ? awk ‘BEGIN < used=0 >; /TYPE/ < used += $2 >END < print used >‘ /proc/PID/smaps will give you the size in KB.

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

$ 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 

Handy function. It’s worth noting that the rss column used for the calculation (Resident Set Size) includes memory from shared libraries, so will throw the numbers off. In my case, the processes were using more memory than the system had available.

this is the answer, dunno why the other was marked as correct, all I get from it is the result «0», this one show exactly what I need . Thank YOU

I modified it to include **virtual memory size in KiB. Also format numbers in fixed width so results are easier to compare. I also truncate the line length to 110 chars but you can pass a 2nd argument to make line longer (shows more of arguments). The solution is: mem() < maxLineLen=$<2:-110>ps -eo rss,vsz,pid,euser,args —sort %mem | grep -v grep | grep -i $1 | cut -c -$maxLineLen | awk ‘‘ >

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»

Regarding «VIRT»: For almost all practical purposes, the size of the virtual image tells you nothing — almost every linux system is configured to allow overcomitting of memory and a lot of apps actually do heavy overcommit.

Here’s a one liner that allows you to specify the name of the process (assumes there is only one process that matches the name): top -p`ps -ef | grep -i $NAME_OF_PROCESS | grep -v grep | gawk ‘‘`

Читайте также:  Линукс удаление файла через командную строку

You can watch various processes in the same time:

You can use pmap to report memory usage.

Nice one, here is an example usage: pmap $(pgrep -f -u username /usr/bin/gnome-shell) | sed -n -e ‘s/ total \+//p’ | numfmt —from=iec 1724678144

In case you don’t have a current or long running process to track, you can use /usr/bin/time .

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

This is «Maximum resident set size of the process during its lifetime, in Kilobytes» (quoted from the man page). That is, the same as RES in top et al.

There are a lot more you can get from /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 

The macos /usr/bin/time is not capable of this level of analysis, but homebrew does provide the correct utility through the gnu-time package. It installs a utility called gtime which does what you talk about.

echo "Memory usage for PID <>:"; for mem in ;do grep $mem /proc//smaps | awk -v mem_type="$mem" ' END ' ;done 

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

I see the RES, but I don’t think I need that. My used Mem and used Swap keeps going up. I need to know what’s making those go up. Ideas?

Resident memory is the memory used by your processes. If none of the processes seems to be using much memory in spite of your total memory usage increasing, the memory could only be used by the kernel. Try sorting after the RES column. Another point maybe too high swappiness when you have heavy disk IO.

Читайте также:  Opengl как установить linux

Thanks. I used this to create this simple bash script that can be used to watch a process and its memory usage:

#!/bin/bash # PROCESSNAME=changethistoyourprocessname MYPID=`pidof $PROCESSNAME` echo "======="; echo PID:$MYPID echo "--------" Rss=`echo 0 $(cat /proc/$MYPID/smaps | grep Rss | awk '' | sed 's#^#+#') | bc;` Shared=`echo 0 $(cat /proc/$MYPID/smaps | grep Shared | awk '' | sed 's#^#+#') | bc;` Private=`echo 0 $(cat /proc/$MYPID/smaps | grep Private | awk '' | sed 's#^#+#') | bc;` Swap=`echo 0 $(cat /proc/$MYPID/smaps | grep Swap | awk '' | sed 's#^#+#') | bc;` Pss=`echo 0 $(cat /proc/$MYPID/smaps | grep Pss | awk '' | 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 "================ mt24">
)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share
)" title="">Improve this answer
answered Jul 28, 2011 at 5:01
2
    likely because the script computes the data with 5 passes through of the smaps file. It should be reasonably easy to have awk do parsing and computation in one pass.
    – Timothée Groleau
    Jun 24, 2013 at 3:47
    @TimothéeGroleau Agree with awk to performance, anyway the script looks cool and someone can learn a bit from it. Maybe Paul Rubenstein wanna update their script :D. Thanks.
    – m3nda
    May 22, 2015 at 7:10
Add a comment|
5

The tool you want is ps. To get information about what java programs are doing:

ps -F -C java

To get information about http:

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 

Источник

Оцените статью
Adblock
detector