Memory management with free

Basic monitoring should be done on a regular basis. This keeps you informed as to the general condition of your server and may warn of impending problems.

This article concentrates on memory management using the command 'free'.


Memory management is actually very easy to do. What you do about any problem is a different matter and we'll look at monitoring what is actually using the memory and how much it's using in a later article.

Free

To start with, log into your server and give the 'free' command:

free

The output consists of four lines:

Well, for a start, that's all a bit confusing as the answer is in Kb. I don't know about you but dividing the reported figures by 1024 is a bit of a pain.

Free -m

Let's make it more readable by adding the '-m' option which will displays the output in megabytes:

free -m

That's better:

One of the aliases in my .bash_profile is:

alias free="free -m"

So I only have to enter 'free' to get a readable output.

Buffers and Cache

The first line gives the column headers: total, used, free and so on.

A common mistake is to read the second line and have a panic attack as that line reports:

.                total       used       free     shared    buffers     cached
Mem:         254        249          5          0             15             181

However, the figure of 249MB out of 254MB includes cached and buffered memory.

Unless you are particularly interested in the buffers and cached figures, ignore this line.

Actual Usage

Concentrate on the third line:

.                total       used       free     shared    buffers     cached
Mem:         254        249          5          0         15        181
-/+ buffers/cache:    51         202

This tells us what memory is being used and is not available for another process.

So it reports 51MB of 254MB being used by various tasks and applications. I have 202MB to play with and for my application to grow into.

Not bad, but then I don't have anything installed on this test slice - this was just after a base install and update.

Swap

The final line tells me how much swap space I am using:

.                total       used       free     shared    buffers     cached
Mem:         254        249          5          0         15        181
-/+ buffers/cache:    51         202
Swap:        511          0          511

As you can see, I have 511MB of swap available and have used none of it.

If you are using swap space then you need to investigate what is using so much memory that it had to resort to hard drive swapping to work. Often it's a case of your application 'outgrowing' your Slice and you may need to upgrade it to include more RAM.

The odd bit of swapping is fine as it is, after all, there to be used. However, consistent swapping or very high levels of swapping does mean your Slice is struggling and it will make your web application slow down.

Regular monitoring

Actually, that's a good example as to why regular monitoring of your Slice is a good idea - your site may be up and running but it may be struggling and becoming slower and slower.

It's a lot better to catch any issues before they result in downtime and a non-operational site.

That's it - quick and easy. Next time we'll look at system monitoring with 'top' which will pinpoint what processes are actually using the memory and, more importantly, how much each process is using.

PickledOnion.

Article Comments:

Milan Andric commented Wed Oct 03 20:47:05 UTC 2007 ago:

Here's an example monitor script to let you know when swap is in use.

http://parsed.org/tip/388/

Paul commented Wed Feb 13 01:44:22 UTC 2008 ago:

Milan, thanks for the script link. It works perfectly on my ubuntu slice, without mods, once I apt-getted mailx.

Kirill Maximov commented Tue Mar 25 20:00:27 UTC 2008 ago:

I really like monit utility to monitor my server, including site load, process memory consumption, process and service availability control and much more stuff.

Jeff O'Hara commented Thu May 22 17:10:44 UTC 2008 ago:

Don't forget the ever useful top command to watch processes in realtime.

-Jeff

G commented Wed May 28 05:34:23 UTC 2008 ago:

Don't forget the even prettier htop command to watch processes like top but with colour. :D http://htop.sourceforge.net/

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)