Site Tools


start:zramswap

This is an old revision of the document!


Table of Contents

Ubuntu and Debian are fairly similar.

Before enabling zramswap you should ensure partitioning is logical. For example here is my daily drivers partition layout:

My disk has been cloned in the past so the sizes might seem a little strange, but you don't need to fixate on things being exact when comparing, instead see that the first boot partition is around 1GB, the second root partition is around 512GB and then I have a third partition I store files on that's about 1.2GB. The third partition should be ext4 so that I can store VM images and other files on it but again it was cloned from a legacy system and I never got around to changing it. Then the last partition is a 64GB swap partition.

You do want to make sure your partitioning scheme is nice and logical so if you need to make any changes you can just boot a live Ubuntu CD and run gparted and move and resize partitions as needed (takes a long time to complete resize and move operations).

The swap partition is abnormally large as I have 32GB of RAM and with zram you use 50% of your RAM size for zramswap. I have experimented with other values but any larger reduces performance. the compression is around 3 on average so you want to have enough swap to move out the 16GB when its uncompressed into a normal page file if need be or you can have weird issues. To ensure thats possible a disk swap of about double RAM works pretty well in testing. This allows me to allocate out about 44GB of RAM to virtual machines without any performance issues. Once you go over that you do notice a couple of pauses here and there as the base OS of Debian seems to like about 4GB to itself either some free, and a couple of GB for it to run nicely. Basically I just take my RAM + zram - 4GB so in my case 32+16-4=44 or there about. It is plenty when running a bunch of VM's.

Here is htop with 44GB of RAM allocated out to a bunch (6) of VM machines. As you can see my CPU on this laptop is starting to become the bottleneck now, as running the VM's tend to use a lot of the 12 threads I have. However being able to spin up many machines is very useful:

Either way, lets setup zramswap. You will remember that in selecting a compression algorithm, for our BTRFS disks we chose to compress with lzo. This is a capable compression algorithm that sacrifices some speed in compression and decompression for a slight compression improvement. For files on a disk this works well as the disk is generally the slowest part when collecting data and so retrieving a smaller file then decompressing it is worth the cpu hit.

However for RAM this is not true. Selecting lzo is a poor choice because we need the RAM to be as fast as possible. After all that is where the system has placed files it needs right now. So instead we need to select the fastest decompression algorithm. This is lz4. While our compression ratios might not be as good, the fact that the data is accessible very quickly means we wont have pauses, or slowdowns while things are paged in and out of zramswap. This can happen if the compression algorithm chosen cannot keep up with the demands of the system.

To get started we install zram tools:

sudo apt-get install zram-tools

Now edit the config file:

sudo nano /etc/default/zramswap

Uncomment the line with 'ALGO=' and 'SIZE=' and 'PRIORITY='. The defaults of lz4, and 100 are excellent and work the best. For RAM make it half the size of your installed physical RAM (2GB in my case):

Now enable and start the service:

sudo systemctl enable zramswap.service
sudo systemctl start zramswap.service

You can check you have more ram with htop (install htop if you dont have it:

htop

As you can see your swap increases to be the page size for the partition + what we just added for zramswap. In my case I have just under 10GB (8GB+2GB). The sizes wont be exact but will be near enough. The installer might have calculated a GB slightly differently to us. If this stresses you out you can (as mentioned in the beginning of this guide) boot a live cd and move and resize your partitions to make them perfect. Totally up to you.

If you quit htop you can also check to see that zram is working with 2 other commands:

sudo zramctl
sudo swapon -s

You will notice the output of swapon says the priority of your disk swap (/vda2 for me) and the zram. However the priority is not what is desirable:

What I have noticed is that in this setup, zramswap is sometimes not used first. To correct this lets fix our fstab file we will give it a priority of 2 which is low but still a positive value as opposed to the system giving it a negative value.

sudo nano /etc/fstab

In the screenshot below you see I have added pri=2 to my swap mount options by using the tab key one time after the 'sw' and typing pri=2:

To remount swap without rebooting you can type:

Notes

Notes: If anyone has any other notes or useful commands or things about ZramSwap, add them here.

start/zramswap.1648222137.txt.gz · Last modified: 2022/03/25 15:28 by peter