Linux pro tip: learn to read `top` and `free` correctly

People look at tools like free and top to see how much RAM their system is using, but they almost always fail to read the output properly. Learn to interpret this properly so you don’t freak out whenever you see something like this:

At first glance it appears I only have 310 megabytes of RAM free. Wrong! The devil is in the details:

If you want to know how much RAM your computer really has free, this is a much better indicator; it’s the amount of RAM which is free if file caches are not counted (notice the “-/+ buffers/cache” row heading on the left side).

Oh noes!! Where is all my RAM?

Computers use memory for both running programs (like Firefox, Rhythmbox, ls, cp, etc), as well as for caching recently-used files. When you unzip linux-3.2.tar.bz2, for example, the file is read into RAM and every single file inside the archive passes through RAM as it is extracted (generally speaking, CPUs can only access data which is in RAM!). The same goes for when you compile Android from source, or when your late-night cron jobs run through the file system cleaning log files and indexing things.

Caches save you boat loads time when accessing files which you’ve recently accessed. It’s by design that the kernel does this, and it’s a good thing.

Waaa! I want `top` to show me all my RAM!

Calm down, br0, it’s not the end of the world. If you so desire, you can instruct the kernel to drop caches immediately:

root@ndechu:~# echo 3 > /proc/sys/vm/drop_caches

Now you see numbers which are probably more like what you were expecting in the first place:

If you're interested, there are various knobs you can tweak to modify the kernel's memory management, caching, swapping, etc; vfs_cache_pressure and swappiness are two good places to start. Also, I just stumbled upon this hilarious site which should help quite a bit: Help! Linux Ate My RAM!.