zram on Debian/Ubuntu for memory overcommitment

In recent Linux releases, it’s available a tiny module called zram, that permits us to create RAM based block devices (named /dev/zramX), which will be kept in memory as compressed data. These ram-based block devices allow very fast I/O, and compression provides a reasonable amounts of memory saving.

We can use it as a drop-in replacement for the well-known tmpfs (used for speeding up compilation tasks or for /tmp), or better as a primary swap device, that will lead to virtually increase memory capacity, at the expense of a slightly increased CPU usage to compress/decompress the swapped data.

Nowadays RAM is very cheap, so why bother with compression? Because there are some situations where you can’t upgrade memory (netbooks) or you want to over-commit real resources (virtualization hosts).

For Ubuntu Precise and later:

Starting with Ubuntu Precise, there is an official upstart script for Ubuntu by Adam Conrad to configure zram in the main repository:

sudo apt-get install zram-config

For other distributions or older Ubuntu:

Googlin’ around to find a nice way to configure zram devices as swap, I found a very nice upstart script that will create a bunch of ramz devices depending on the number of CPU cores available, with a total size of the available memory: https://raw.github.com/gionn/etc/master/init.d/zram

Copy the script to the init.d folder, mark it as executable and enable autostart on boot:

sudo wget https://raw.github.com/gionn/etc/master/init.d/zram -O /etc/init.d/zram
sudo chmod +x /etc/init.d/zram
sudo update-rc.d zram defaults

Try it manually executing it for the first time with:

/etc/init.d/zram start

Depending on the kernel version you are running, you may need to adjust the module parameter name to num_devices on line 26 to:

modprobe zram num_devices=$num_cpus

or keep as is for newer kernels:

modprobe zram zram_num_devices=$num_cpus

Checking if it’s working

If everything went smooth, you will find a few notices on dmesg:

zram: module is from the staging directory, the quality is unknown, you have been warned.
zram: Creating 4 devices ...
Adding 1497864k swap on /dev/zram0.  Priority:100 extents:1 across:1497864k SS
Adding 1497864k swap on /dev/zram1.  Priority:100 extents:1 across:1497864k SS
Adding 1497864k swap on /dev/zram2.  Priority:100 extents:1 across:1497864k SS
Adding 1497864k swap on /dev/zram3.  Priority:100 extents:1 across:1497864k SS

meaning that the zram device have been created and enabled as swap devices with highest priority.

You can discover the increased swap space available with free -m:

             total       used       free     shared    buffers     cached
Mem:          5851       5696        154          0         85       4310
-/+ buffers/cache:       1300       4550
Swap:         5851          0       5850

Happy zramming!

Read Reddit comments

🇬🇧 🇺🇸 If you found value in my content, consider supporting me by treating me to a coffee, beer, or pizza. Your contributions help fuel more quality content creation.

🇮🇹 Se hai apprezzato i miei contenuti, considera di supportarmi offrendomi un caffè, una birra o una pizza. I tuoi contributi aiutano a creare contenuti di qualità.

🇬🇧 🇺🇸 If you have found inaccuracies or wish to improve this article, please use the comments section below (after clicking on Load Comments).

🇮🇹 Se hai trovato imprecisioni o vuoi migliorare questo articolo, utilizza la sezione commenti qui sotto (dopo aver cliccato Load Comments)