Archive

Posts Tagged ‘sysadmin’

Understanding the “free” command in Linux

July 1st, 2009 Comments off

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.