How can I monitor the memory usage?
I recommend combining the previous answers
watch -n 5 free -m
Note that Linux likes to use any extra memory to cache hard drive blocks. So you don't want to look at just the free Mem
. You want to look at the free
column of the -/+ buffers/cache:
row. This shows how much memory is available to applications. So I just ran free -m
and got this:
total used free shared buffers cached
Mem: 3699 2896 802 0 247 1120
-/+ buffers/cache: 1528 2170
Swap: 1905 62 1843
I know that I'm using 1528 MB and have 2170 MB free.
Note: To stop this watch
cycle you can just press Ctrl+C.
I think htop
is the best solution.
sudo apt-get install htop
This way you will notice what programs is using most RAM. and you can easily terminate one if you want to. Here's a screenshot!
If you looking for a nice breakdown of the memory used by each running process, then I might recommend checking out ps_mem.py (found here at pixelbeat.org).
I know in the comments above, you mentioned wanting a one-line snapshot from free, but I figured others might find this useful.
Example output:
user@system:~$ sudo ps_mem.py
[sudo] password for user:
Private + Shared = RAM used Program
4.0 KiB + 7.5 KiB = 11.5 KiB logger
4.0 KiB + 8.0 KiB = 12.0 KiB mysqld_safe
4.0 KiB + 10.0 KiB = 14.0 KiB getty
4.0 KiB + 42.0 KiB = 46.0 KiB saslauthd (5)
48.0 KiB + 13.0 KiB = 61.0 KiB init
56.0 KiB + 27.5 KiB = 83.5 KiB memcached
84.0 KiB + 26.5 KiB = 110.5 KiB cron
120.0 KiB + 50.0 KiB = 170.0 KiB master
204.0 KiB + 107.5 KiB = 311.5 KiB qmgr
396.0 KiB + 94.0 KiB = 490.0 KiB tlsmgr
460.0 KiB + 65.0 KiB = 525.0 KiB rsyslogd
384.0 KiB + 171.0 KiB = 555.0 KiB sudo
476.0 KiB + 83.0 KiB = 559.0 KiB monit
568.0 KiB + 60.0 KiB = 628.0 KiB freshclam
552.0 KiB + 259.5 KiB = 811.5 KiB pickup
1.1 MiB + 80.0 KiB = 1.2 MiB bash
1.4 MiB + 308.5 KiB = 1.7 MiB fail2ban-server
888.0 KiB + 1.0 MiB = 1.9 MiB sshd (3)
1.9 MiB + 32.5 KiB = 1.9 MiB munin-node
13.1 MiB + 86.0 KiB = 13.2 MiB mysqld
147.4 MiB + 36.5 MiB = 183.9 MiB apache2 (7)
---------------------------------
208.1 MiB
=================================
Private + Shared = RAM used Program
The only part I don't like is the fact that the script claims to require root privileges. I haven't had an opportunity yet to see exactly why this is the case.