What process is using my 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.

Читайте также:  Android port to 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.

Читайте также:  Linux both nvidia and amd

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 

Источник

How do you find out which program is using too much memory?

Ubuntu has been crashing on me recently. I think its because it runs out of memory so I ran the free -m command and found that my memory usage was really high. So then, i ran top to find the culprit, but the displayed processes were using less than 1.5% of memory. How do I know which program is making ubuntu crash/run out of memory? Below is the output:

shafee@shafee-pc:~$ free -m total used free shared buffers cached Mem: 3824 3714 110 0 978 1954 -/+ buffers/cache: 780 3044 Swap: 99 0 99 shafee@shafee-pc:~$ top top - 02:12:14 up 1:24, 2 users, load average: 0.16, 0.42, 1.49 Tasks: 182 total, 1 running, 181 sleeping, 0 stopped, 0 zombie Cpu(s): 2.9%us, 1.9%sy, 0.3%ni, 79.3%id, 15.5%wa, 0.0%hi, 0.2%si, 0.0%st Mem: 3916708k total, 3803848k used, 112860k free, 1002308k buffers Swap: 102396k total, 0k used, 102396k free, 2001852k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4200 root 20 0 289m 53m 38m S 2 1.4 1:06.45 Xorg 5590 shafee 20 0 19348 1368 956 R 2 0.0 0:00.01 top 1 root 20 0 24124 2136 1264 S 0 0.1 0:02.05 init 2 root 20 0 0 0 0 S 0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0 0.0 0:24.23 ksoftirqd/0 6 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/0 7 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/1 9 root 20 0 0 0 0 S 0 0.0 0:00.11 ksoftirqd/1 11 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/2 13 root 20 0 0 0 0 S 0 0.0 0:03.89 ksoftirqd/2 14 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/3 16 root 20 0 0 0 0 S 0 0.0 0:00.16 ksoftirqd/3 17 root 0 -20 0 0 0 S 0 0.0 0:00.00 cpuset 18 root 0 -20 0 0 0 S 0 0.0 0:00.00 khelper 19 root 0 -20 0 0 0 S 0 0.0 0:00.00 netns 21 root 20 0 0 0 0 S 0 0.0 0:00.01 sync_supers shafee@shafee-pc:~$ 

6 Answers 6

You are reading the output of free incorrectly. The Linux Kernel does a lot of its own memory management, in turn allocating more than it actually needs - so your true amount of "Free Memory" is 3044 located in the "Free" column of the +/- Buffers/cache line, making only 780 MB actually being consumed.

By default top will sort based on CPU consumption. You can press Shift + M to sort by percentage of memory consumed - giving you a better grasp of what software is using the memory allotted to the kernel.

free -m total used free shared buffers cached Mem: 7873 3916 3956 0 231 1117 -/+ buffers/cache: 2567 5305 Swap: 12401 0 12401 

And in top with memory sorted:

top - 17:05:18 up 2 days, 1:40, 4 users, load average: 0.21, 0.14, 0.11 Tasks: 237 total, 1 running, 234 sleeping, 0 stopped, 2 zombie Cpu(s): 1.6%us, 0.8%sy, 0.1%ni, 96.7%id, 0.8%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 8062420k total, 4013632k used, 4048788k free, 237204k buffers Swap: 12699644k total, 292k used, 12699352k free, 1144752k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1632 root 20 0 884m 240m 6532 S 0 3.1 1:20.17 java 3911 marco 20 0 1011m 165m 22m S 0 2.1 9:20.62 chrome 3852 marco 20 0 770m 162m 45m S 0 2.1 14:59.59 chrome 1091 root 20 0 491m 160m 118m S 3 2.0 29:19.44 Xorg 1747 marco 20 0 659m 108m 34m S 1 1.4 18:43.92 compiz 3964 marco 20 0 1113m 99m 21m S 0 1.3 18:51.88 chrome 1759 marco 20 0 668m 94m 21m S 0 1.2 2:27.42 nautilus 3046 marco 20 0 788m 86m 26m S 0 1.1 1:22.96 evolution 1793 marco 20 0 647m 85m 18m S 0 1.1 0:12.74 shutter 1791 marco 20 0 404m 85m 13m S 0 1.1 5:19.51 bitcoin 2938 marco 20 0 809m 78m 31m S 0 1.0 1:01.07 empathy 9630 marco 20 0 265m 73m 19m S 1 0.9 12:41.52 skype 9618 marco 20 0 914m 64m 21m S 0 0.8 1:14.04 chrome 1777 marco 20 0 432m 64m 14m S 0 0.8 1:45.96 pastie 

Finally to help you diagnose what the actual software is, try passing the -c flag to top: top -c as that will give you the full path, name, and parameters of the command running.

Источник

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