Table of Contents

DATE CHECKED THIS PAGE WAS VALID: 13/09/2023

Ubuntu and Debian are fairly similar - zramswap lets you get more ram use/tricks system into thinking there is more by compressing parts of your RAM.

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 in my case but typically you will have a swap partition equal to what you set zramswap to be. I have experimented with other values but this works well from what I can tell. the compression is around 2-3 on average so you want to have enough swap to move out 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 a similar size works pretty well in testing. In my example it 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. Around these values 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. Essentially I am able to comfortably use more than 32GB RAM on a system with less and not have massive performance issues. This is the goal:

Actual Setup

Either way, lets setup zramswap. You will remember that in selecting a compression algorithm, for our BTRFS disks we chose to compress with lzo (some people choose zstd for even better compression and faster reads but I prefer slightly faster writes to the disk and less memory used for operations). 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 (see end for some exceptions). 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 changing 'sw' to pri=2:

sw is not really needed the syntax of the file simply expects something to be there so you can change that value to something valid.

You have to reboot for the change to take effect.

Once rebooted swapon -s will show that the priority is correct (2 for the disk swap and 100 for the zram).

Now everything is correct you can load up a couple of memory intensive VM's and check that your system performs normally.

In this test Ubuntu VM I have fired up three 3.9GB test VM's to see if it works ok (the max I could allocate is 3900MB to each - you cant allocate to 1 single VM more ram than you have), and despite the system only having 4GB actual RAM allocated to it. It seems to perform just fine:

As you can see the zram was completely used up and this is what allows the performance to maintain while the Ram is completely full.

You can experiment yourself and find what works for you :)

Notes

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

Some suggested values: 1GB,2GB or 4GB systems : zramswap the same size as RAM but zstd compression and a swapfile the same size as zramswap value eg: 4GB RAM example - zramswap set to 4GB also and Swap partition of 4GB also. Thus you see 4GB RAM in htop and 8GB swap (4gb being zramswap). 1GB,2GB and 4GB systems tend to have too little RAM (eg: raspberry pi etc) and setting a large value and accepting a slower compression at cost of CPU can improve performance in most cases. (See swappiness page on systems for low ram - you may wish to set swappiness to 101).

8GB, 16GB systems : zramswap half the size of RAM, swapfile half the size of RAM (equal to zramswap value). lz4 for all RAM values 8GB or more (faster, not RAM constrained machines).

32GB RAM and beyond : zramswap 25% of RAM, swapfile same value.

Reason for 32GB and more systems using this is that zramswap tends to be less speedy than just using RAM alone so if you already have a ton of RAM its benefit starts to tail off as you have more and more available RAM. In some cases you might find a use case for it but mostly when you start getting to the stage where you have ample RAM, its benefit is of lower value so you may as well not set large values anymore, or use it at all.

Some exceptions might be a 64GB system with a large database and increasing RAM could be too costly so you are forced to increase RAM some other way. This situation it might be best to use zstd and a 75% value of ram for zramswap. This could be a balance until you can afford to pay for more RAM or upgrade your VPS.

Note if you are running any sort of database do not go beyond 85% of your ram under any circumstances and regardless of how much or little ram you have. Dont do it.

Etc. Test your own values. Mileage might vary.