What process use memory linux

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

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.

Читайте также:  Linux после установки черный экран

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.

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 

Источник

Linux: find out what process is using all the RAM?

Before actually asking, just to be clear: yes, I know about disk cache, and no, it is not my case 🙂 Sorry, for this preamble 🙂 I'm using CentOS 5. Every application in the system is swapping heavily, and the system is very slow. When I do free -m , here is what I got:

 total used free shared buffers cached Mem: 3952 3929 22 0 1 18 -/+ buffers/cache: 3909 42 Swap: 16383 46 16337 

So, I actually have only 42 Mb to use! As far as I understand, -/+ buffers/cache actually doesn't count the disk cache, so I indeed only have 42 Mb, right? I thought, I might be wrong, so I tried to switch off the disk caching and it had no effect - the picture remained the same. So, I decided to find out who is using all my RAM, and I used top for that. But, apparently, it reports that no process is using my RAM. The only process in my top is MySQL, but it is using 0.1% of RAM and 400Mb of swap. Same picture when I try to run other services or applications - all go in swap, top shows that MEM is not used (0.1% maximum for any process).

top - 15:09:00 up 2:09, 2 users, load average: 0.02, 0.16, 0.11 Tasks: 112 total, 1 running, 111 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 4046868k total, 4001368k used, 45500k free, 748k buffers Swap: 16777208k total, 68840k used, 16708368k free, 16632k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ SWAP COMMAND 3214 ntp 15 0 23412 5044 3916 S 0.0 0.1 0:00.00 17m ntpd 2319 root 5 -10 12648 4460 3184 S 0.0 0.1 0:00.00 8188 iscsid 2168 root RT 0 22120 3692 2848 S 0.0 0.1 0:00.00 17m multipathd 5113 mysql 18 0 474m 2356 856 S 0.0 0.1 0:00.11 472m mysqld 4106 root 34 19 251m 1944 1360 S 0.0 0.0 0:00.11 249m yum-updatesd 4109 root 15 0 90152 1904 1772 S 0.0 0.0 0:00.18 86m sshd 5175 root 15 0 90156 1896 1772 S 0.0 0.0 0:00.02 86m sshd 

Restart doesn't help, and, by they way is very slow, which I wouldn't normally expect on this machine (4 cores, 4Gb RAM, RAID1). So, with that - I'm pretty sure that this is not a disk cache, who is using the RAM, because normally it should have been reduced and let other processes to use RAM, rather then go to swap. So, finally, the question is - if someone has any ideas how to find out what process is actually using the memory so heavily?

Источник

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