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:

$ ps -ely | grep -E 'firefox|chromium|plugin'
S  1000  5214  5576  2  80   0 77916 232548 futex_ ?       00:01:00 chromium-browse
S  1000  5572  1984  1  80   0 85916 184994 poll_s ?       00:03:27 chromium-browse
S  1000  5574  5572  0  80   0 19276 60531 poll_s ?        00:00:02 chromium-browse
S  1000  5576     1  0  80   0 22476 67221 wait_f ?        00:00:00 chromium-browse
S  1000 31011  1984  3  80   0 415124 284530 poll_s ?      00:05:14 firefox-bin
R  1000 31211 31011 58  80   0 618876 349396 -    ?        01:31:23 plugin-containe
S  1000 32178  5572 13  80   0 62780 171077 poll_s ?       00:08:28 chromium-browse

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:

cat /proc/sys/vm/swappiness

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:

sudo sysctl vm.swappiness=10

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:

gksudo gedit /etc/sysctl.conf

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.