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:
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.
Well, you could use the top command, or maybe some ps grepping like this:
S 100052145576280077916232548 futex_ ? 00:01:00 chromium-browse
S 100055721984180085916184994 poll_s ? 00:03:27 chromium-browse
S 10005574557208001927660531 poll_s ? 00:00:02 chromium-browse
S 10005576108002247667221 wait_f ? 00:00:00 chromium-browse
S 10003101119843800415124284530 poll_s ? 00:05:14 firefox-bin
R 1000312113101158800618876349396 - ? 01:31:23 plugin-containe
S 10003217855721380062780171077 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!)
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!
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:
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.