Linux memory management and how to see actual RAM usage

Jan 22nd, 2012 | By | Category: Linux / Freebsd

Where the hell did all my RAM go?

So as some of you may or may not know linux allocates memory a little differently than windows and it may be a little disconcerting at first to rely on ‘top’ for your memory usage breakdown as it starts to look like there is a big leak happening somewhere.  This isn’t the case and I’ll take a quick paragraph to explain how it works.

At the end of the day the way that RAM works is that if it isn’t being used it’s being wasted and linux being as amazing as it is has a strict policy about wasting as little as possible so it uses the ram extensively to cache nearly anything it can get it’s hands on.  What ends up happening is that after a program is run the output or information from that program are cached so that every subsequent time it’s run the information can just be pulled out of the cache which is stored in RAM instead, giving it about 1000x increase in read speed for future executions.   Pretty cool eh?  Yea, well unless you don’t know this, setup a new production server in linux, turn it on for the first time and sit there watching top report your free memory going down and down and down and down until it reaches a read out like this:

Mem:   8197228k total,  6219696k used,  1977532k free when just 2 hours ago the same server running on FreeBSD was reporting 90% of it’s ram being free.

The thing to note of course is how big the Cache is.. In my case here it’s about 4.8GB large, meaning there’s all sorts of data stored in there that apache is reloading over and over and over again that is lightning fast.  Already I’m loving this new Debian server 🙂

So how do I see my real memory usage?

#free -m

This nifty little command will show you the memory usage with and without caching taken into account resulting in something like this:

total used free shared buffers cached
Mem: 8005 6078 1926 0 487 4675
-/+ buffers/cache: 915 7089
Swap: 11442 4 11438

Telling me that I actually am only using 915MB of ram, not 6GB of it and I’ve got over 7GB of my available 8GB of memory to spare!  At this point you can breath a sigh of relief and give thanks that you are running an OS that is taking full advantage of all this juicy RAM that you have, which would otherwise just be sitting there taking up space 🙂

 

 

 

 

Tags: , , , , , ,

Leave a Comment