Running a system with no swap space – ZRam!

If you’re like me and use an SSD as your primary boot drive, you will not want to be installing a swap partition to this drive. SSD’s have limited write cycles, and excess wear caused by swap space is not desirable in terms of lifetime.

So now you have no swap space. What happens when your system runs out of RAM? Boom. Lockup. What can you do to prevent this?

In Windows, you can put a swap file on another drive using the Advanced System Settings dialogue. But what if you don’t have another drive? Well in Linux (and Mac OSX), you can regain space in system RAM by compressing old data (data that would get swapped out to disk). To enable this functionality in Ubuntu Linux, type the following command:

sudo apt-get install zram-config

Once it is installed, reboot and you should notice in htop or another program that you have swap space about half the size of your RAM:

Screenshot - 130315 - 07:44:47

If you type ls /dev/zram* you should see as many zram files as there are cores in your computer:

/dev/zram0  /dev/zram1  /dev/zram2  /dev/zram3  /dev/zram4  /dev/zram5

This is for performance: compressing RAM takes a lot of CPU power, so invoking multi-threading speeds up this process so you won’t see as much of a performance hit as there is when swapping to disk occurs. The amount of effective swap space you get is actually dependant on the compressability of the data stored in RAM – some data compresses better than other data, so you might not get the full half-size amount of compressed RAM.

With this in mind, you may still run out of swap space and experience crashing and lockup, so its advisable to also keep a swap file on a separate disk. ZRam is configured to be used first, then when that is full data gets swapped to disk. I have been using ZRam on a Ubuntu Server installation for years now and I have never experienced crashing from running out of RAM, so things must be working well!

I hope this was interesting, happy ZRamming!

Setting up an Insurgency FastDL server using Ubuntu Server
Directly connecting two Raspberry Pi's with serial UART problems

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.