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:
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:
1 2 3
Try it manually executing it for the first time with:
Depending on the kernel version you are running, you may need to adjust the module parameter name to num_devices on line 26 to:
or keep as is for newer kernels:
Checking if it’s working
If everything went smooth, you will find a few notices on
1 2 3 4 5 6
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
1 2 3 4
Read Reddit comments