94

When I check free in one of Prod server it showing 70% of memory is being used:

             total       used       free     shared    buffers     cached
Mem:     164923172  141171860   23751312          0    4555616   20648048
-/+ buffers/cache:  115968196   48954976
Swap:      8388600          0    8388600

But I didn’t find what process is using the memory, I tried the top command and it is showing process using memory only 1.1 and 5.4 %

How can I find which process is using the memory?

Below are the top command results:

15085 couchbas  25   0 2784m 2.4g  40m S 183.7  1.5 299597:00 beam.smp
28248 tibco     18   0  124m 100m 3440 S 20.9  0.1   2721:45 tibemsd
15334 couchbas  15   0 9114m 8.6g 3288 S  9.0  5.4  12996:28 memcached
15335 couchbas  18   0  6024  600  468 S  2.0  0.0   1704:54 sigar_port
15319 couchbas  15   0  775m 2516  944 S  0.7  0.0 269:13.41 i386-linux-godu
12167 tibco     16   0 11284 1464  784 R  0.3  0.0   0:00.04 top
12701 root      15   0  451m 427m 2140 S  0.3  0.3  18:25.02 controller
13163 root      11  -5     0    0    0 S  0.3  0.0 289:58.58 vxglm_thread
4
  • 1
  • Thanks for quick response, Is there any way to check the what all opened files and which file is eating memeory.
    – user90640
    Nov 7, 2014 at 6:44
  • 1
    Try to sort the top output - when running, press Shift + m
    – Eenoku
    Nov 7, 2014 at 7:37
  • If you want a better visual result you can you htop or stacer.
    – Nitish770
    Apr 21, 2021 at 5:05

6 Answers 6

128

This will show you top 10 process that using the most memory:

ps aux --sort=-%mem | head

Using top: when you open top, pressing m will sort processes based on memory usage.

But this will not solve your problem, in Linux everything is either file or process. So the files you opened will eating the memory too. So this will not help.

lsof will give you all opened files with the size of the file or the file offset in bytes.

2
  • Re: "pressing m", on CentOS 7 I had to use "pressing shift m" Dec 11, 2023 at 14:26
  • Also, ps aux --sort=-rss | head or ps aux --sort=-vsz | head.
    – Artfaith
    Mar 18 at 10:20
11

Your ram isn't actually used. Linux just caches the stuff on your disk by default, and puts what it caches in the "used" section of ram.

If you load applications that actually use that RAM, it will be their for use.

See http://www.linuxatemyram.com/

2
9

You can pipe ps and sort to get the process using the most memory. It will also allow you specify other process details you wish to see with it:

ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -n -r | less

PS: The above assumes that your memory column is first "sort -k 1 -n -r"

1
  • Please also share how to exit Nov 5, 2021 at 9:19
6

For an interactive analysis, you can use htop, then hit function key F6 to choose the sort-order, e.g. PERCENT_MEM (memory usage in percents), M_RESIDENT (resident memory size, the real memory that is used), M_SIZE (virtual memory size) or M_SHARE (size of shared memory). The first two are the relevant numbers.

The green lines are threads. Hit (upper case) K and H to hide them.

4

You can use command slabtop to view what slab objects are used by kernel.
Also you can check output of /proc/meminfo

Look for these 2 lines in the output

Slab:              68136 kB
SReclaimable:      21208 kB 

Above example shows 68MB of slab cache being used by kernel and 21MB can be reclaimed. To reclaim you can use technique described here https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html-single/Performance_Tuning_Guide/#s-memory-tunables

echo 2 > /proc/sys/vm/drop_caches  
3

Below command can be used to get the top process by memory usage percentage

# ps -o pid,user,%mem,command ax | sort -b -k3 -r
 PID USER     %MEM COMMAND
 1234 mysql    25.0 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
 5678 apache    1.3 /usr/sbin/httpd

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .