Where did all my memory go on my 512MB VPS?

I have a little experience setting up linux servers, anything from a simple samba server to a full web and mail server. But it’s been a while so when I set up my linode vps I came across this gotcha.  Judging by search queries like “Where did all my ram go on my 512MB VPS?” (hence the title of this post) it is a common one for those just starting out.

The command used to check free memory on your VPS or any linux system is:

free -m

but the output is a little cryptic.  Here are my results:

 

results from free -m

 

I did a double-take, since I had taken pains to optimize my VPS as much as possible.  But it seems to be showing only 8MB is free!! And when you have a small VPS (mine is 512MB Ram) then it seems like it is time to panic.

So I tweaked things here and there, mostly with my nginx configuration and all to no avail.  I gained maybe 10MB back, if that.  When I started with linode it was only 360MB Ram but I never ran out of memory, and I hadn’t upgraded or added anything to the system.  And then it hit me, duh!  I read the output wrong.  I had forgotten because a. it was a long time since I used free, and b. I normally worked with servers with 2GB+ memory (and maybe c. I was very very tired that night)

The figures to concentrate on are actually the second line.  Here it is showing I am really using:

 

The important numbers

 

 

But why?  Well linux/the system borrows free memory for disk caches and other buffers.  Otherwise it would just go to waste, but if your programs need it, it gives it back instantly so practically “I” or rather my programs are using 186MB not the 503MB of the first line and if “I” needed it there was 326MB available to use.  So you see 360MB of my original plan was more than enough.  🙂

I wonder if I will forget again and do a double-take in a few years time :p