Why is Linux using all my RAM?

My Linux-running laptop seemed to be a little bit sluggish, so I had a look at the amount of free RAM available – and did a double take:

Linux free RAM confusion
A few apps eating an entire 4GB? Say it ain't so!

This is on a 4GB machine with a web browser (6 tabs) open, email client, code::blocks IDE and totem to play some music. How can that devour my entire 4GB?

Answer: It can’t. Or at least it’s not! The Linux kernel is in fact using free RAM as a disk cache to keep your most often used data in memory and thus speed up the system. As soon as that RAM is actually needed for applications, then it’s instantly made available. This is in theory like the Windows Superfetch service (which is the first thing I disable on any Windows machine I’m forced to co-exist with), only the Linux version actually works well without perpetually thrashing the pants off the hard drive.

What’s really happening is this:

Linux free RAM in reality
It ain't so =D

For the exact workings, please see the excellent (and indeed excellently named) site “Help! Linux ate my RAM!” – http://www.linuxatemyram.com/.

Bit of a PEBKAC moment, there ;-)

How to: Get detailed browser memory usage from within Firefox or Chrome

Well, you could use the top command, or maybe some ps grepping like this:

But that’s not especially friendly to read, so how about simply going to the internal memory usage page at about:memory? (Note: Just put about:memory into the URL location and hit enter – I can’t link to it because it goes to about:blank instead!)

Firefox Memory Usage - Internal Display

Presumably this is a cross-platform method, so available on any modern version of Firefox or Chrome. The details in the above screengrab were taken on Firefox 6 on Linux with two windows containing 8 tabs in total open, and it’s chewing on 316MB of RAM? Ouch. When my wife plays Facebook games I’ve seen Firefox eating over 700MB for a single window with a single tab running FarmTown or Treasure Madness or such, but I guess that’s more down to the bloat of those flash games than the browser… Still, good technique.

Credit where it’s due: Tip found on linuxers.org – good find!

How To: Speed Up Linux by Minimising Swapiness

Swappiness is a setting in the Linux kernel which controls how amenable to paging things in memory out to disk the kernel is (like using virtual memory in Windows), and in Ubuntu it comes with a default value of 60 – which for a box with lots of memory is too high in my humble opinion. The range of values goes from 0 (never use the swap file unless absolutely critical) to 100 (page stuff out to file whenever it feels like).

You can easily check your current swappiness value like this:

And you can change the swappiness of the system on the fly (but it’ll go back to the value in the sysctl.conf file after a reboot) like this:

My laptop has 4GB of RAM, and even with a bunch free, Linux decides to swap stuff out to file quite often with a swappiness setting of 60, which can slow the box to a crawl. To fix this, and permanently insist that all physical RAM is used up before starting any paging at all,, simply change the setting to something like 10 like this:

Then, when the file is open, either add the line vm.swappiness=10 to the bottom of the file, or if it already exists just modify the value, then reboot.

To find out more about the whole swappiness thing, try this article.