Saved for posterity. Possibly there is an easier way.
$# root@mt:/var/log# bash memreport
Name: bash: 10588
Name: bash: 10600
Name: bash: 20548
Name: cron: 22420
Name: getty: 5928
Name: init: 8352
Name: ntpd: 38332
Name: rsyslogd: 54144
Name: sort: 59840
Name: sshd: 49168
Name: sshd: 70452
Name: udevd: 16864
Name: udevd: 16864
Name: udevd: 16868
$# cat memreport
#!/bin/bash
for i in /proc/[123456789]*/status; do
size=$(grep VmSize $i | awk '{print $2}')
(( size > 0 )) && echo -e "$(grep Name: $i): \t$size"
done | sort -n;
Free is a command line interface (CLI) tool available on most Linux and Unix based systems. From the man page…
… displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel.”
Running free, you get output similar to the following (by default the numbers are in kilobytes)…
1
2
3
4
5
| :~$ free
total used free shared buffers cached
Mem: 3992680 3033116 959564 0 272632 1754476
-/+ buffers/cache: 1006008 2986672
Swap: 979924 0 979924 |
The numbers are essentially counters representing how the RAM on your Linux system is allocated in bytes. By itself the above is not very readable, but the command comes with a few handy options to make it easier. You can pass it the ‘-m’ parameter to translates the counters to MB, the ‘b’ option does the same in bytes.
1
2
3
4
5
| :~$ free -m
total used free shared buffers cached
Mem: 3899 2963 935 0 266 1713
-/+ buffers/cache: 984 2914
Swap: 956 0 956 |
The first line shows overall usage of the total memory usage on your system. Including any file-system and other caches reserved by the kernel. Since RAM is faster the OS will always try to take advantage of that resource by using it to its fullest. But in practice most of that can be released quite quickly for applications that need more space.
So how can we see how much memory us really being used by the applications on your server? The ‘-/+ buffers’ line shows how much of the actual RAM is in use by applications while the last line shows how much of your slower disk based virtual memory is in use.
Sometimes a PC will start to run relatively slowly, for example when it is running a busy web server. By checking free you will likely see the used column of the last two lines starting to approach the total value. Which is bad for performance and you run the risk of the kernel killing low priority processes to release memory for new tasks.
There are lots of great guides available about optimising memory usage, especially in a server environment. One that I have found handy is on the Rimuhosting.com site. There is also an incredibly extensive but more general page at Linux Troubleshooting with some handy tips. Lots more can be found on Google.