Most popular pages

Tips, tweaks and FAQ

Increase performance by turning on zswap


Back to the home page


Contents of this page:

You can improve the performance of the system memory (RAM) of Linux Mint 22.x with the following tweaks:

The absolute number one: turn on zswap

1. You'll often achieve a considerable speed gain by turning on zswap. This is especially so on older machines, but on modern machines there will also be a speed gain, even though it'll be smaller. It's therefore advisable to turn it on in all cases. Below, I'll explain how to do that.

A. First some background information:

Swapping is one of the biggest threats to performance. This hasn't changed over the years and will stay that way, as long as RAM will continue to be faster than permanent storage devices. That's why zswap exists: to mitigate this undesirable effect of swapping, by a reduction of I/O activity. Or in normal human language: zswap will make the system write less to the hard disk.

The kernel feature called zswap, provides a compressed RAM cache for swap pages. Pages which would otherwise be swapped out to the disk, are instead compressed and stored into a memory pool in the RAM.

Once the pool is full or the RAM is exhausted, the least recently used page is decompressed and written to the swap on the hard disk, as if it had not been intercepted at all. After that page has been dumped into the disk swap, the compressed version in the RAM pool will be deleted, thus freeing up the space it took in the RAM.

By default, zswap is switched off. When switched on, it uses up to 20 percent of the RAM memory by default. In my how-to, you'll also learn how to set that maximum at 30 percent, which virtually "increases" your system memory a lot (don't increase that above 40 percent, because higher maximum percentages might not leave enough RAM for uncompressed use).

The price you pay for turning on zswap, is twofold:

- Your processor (CPU) will be taxed more, because it'll have to compress and decompress more. With a single-core CPU of low speed there might be a noticeable performance hit, as compressing/decompressing data requires a faster CPU, preferably a multi-core one.

- When the system has filled the RAM swap, it'll start swapping on the hard drive as well. With a burden on the available RAM: the chunk of memory that has been sacrificed for the RAM swap.

That's why I advise, if your computer has a relatively weak CPU, to turn on zswap only in combination with a swappiness that has been reduced to 45 (see item 2 on this page).

Note: If your hard disk is an SSD, there's an extra reason to use zswap. That's because too many disk write actions, like frequent disk swapping, reduce the lifespan of an SSD.

B. Now the how-to:

Switching on zswap involves heavy modification of the Grub bootloader settings. It's therefore sensible to make a copy of the current Grub settings first, aptly called grubcopy.

This makes it easy to roll back the changes you're about to make (in the unlikely case that you should ever wish to undo them). So proceed like this:

a. Launch a terminal window.
(You can launch a terminal window like this: *Click*)

b. Copy/paste the following blue command line into the terminal:

sudo cp -v /etc/default/grub /etc/default/grubcopy

Press Enter. Type your password when prompted. In Ubuntu this remains entirely invisible, not even dots will show when you type it, that's normal. In Mint this has changed: you'll see asterisks when you type. Press Enter again.

So far, so good. You can switch on zswap as follows (with superior fast compression, by means of lz4 and zsmalloc):

c. First you're going to add some boot parameters to the settings file of bootloader Grub. For that, copy/paste the following blue command line into the terminal. That's one long line, don't chop it up! And don't try to type it all, because it's far too easy to make a mistake:

sudo sed -i 's/splash/splash zswap.enabled=1 zswap.max_pool_percent=30 zswap.zpool=zsmalloc zswap.compressor=lz4/' /etc/default/grub

Press Enter.

d. Copy/paste the following command line into the terminal, in order to update Grub with the new settings:

sudo update-grub

Press Enter.

(if you type the command: note the dash between update and grub)

e. The zsmalloc module isn't included in the initramfs at boot time, so it can't be loaded automatically. Change that like this:

Copy/paste the following command line into the terminal, in order to add the new module to the settings file of the initramfs:

echo "zsmalloc" | sudo tee -a /etc/initramfs-tools/modules

Press Enter.

f. Then in the terminal, to update the initramfs of all installed kernels with the new settings (use copy/paste to transfer the command to the terminal!):

sudo update-initramfs -uk all

Press Enter. This might take a long time to complete!

g. Reboot.

h. Now you can check whether the change has been implemented with this command (use copy/paste):

sudo dmesg | grep zswap

The output should report in its final line:
zswap: loaded using pool lz4/zsmalloc

i. The percentage of your RAM that zswap maximally uses, can be checked with the following command:

cat /sys/module/zswap/parameters/max_pool_percent

It should report 30. As said already: don't increase that above 40, because higher maximum percentages might have negative consequences.

Why zswap is better than zRam (and never use both of them simultaneously!)

1.1. There's also an alternative RAM swap feature called zRam, which is not as good as zswap. The reasons that zswap is generally better than its alternative zRam, are twofold:

a. zswap taxes the CPU less than zRam. Sometimes much less: especially with desktop use the difference can be significant.

b. zswap is less invasive than zRam: it utilizes the existing swap infrastructure in Linux Mint and Ubuntu.

You can find more background information concerning zswap versus zRam here and here. Web pages gone? Download an offline copy of both articles here.

Note: Don't install zRam when you use zswap, because it's downright counterproductive in combination with zswap!

Both zswap and zRam provide a compressed cache and would, when used simultaneously, wind up using more system memory than each individually. Simultaneous use makes therefore no sense and would even be detrimental.

When zswap is enabled: decrease swap use when your CPU is weak

2. On the hard disk there's a separate file or partition for virtual memory, called the swap. When Mint uses the swap too much, the computer slows down. This still plays a role when zswap has been turned on (see item 1 on this page).

A. First some background information:

Mint's inclination to use the swap, is determined by a value called "swappiness". The lower the value, the longer it takes before Mint starts using the swap.

On a scale of 0-200, the default value is 60. When your CPU is relatively weak, that's a bit too high for normal desktop use; the optimal compromise for a weak CPU (these things are always a trade-off) is probably something like 45. Decreasing the default value somewhat on a desktop computer with a weak CPU, has no negative side effects.

On a computer with a powerful CPU, the default swappiness of 60 is best. So don't change it for such a computer.

Note (1): In general, it's nowadays not a good idea anymore to decrease swappiness as a stand-alone measure. Don't change the default swappiness when you haven't enabled zswap.

Note (2): Don't fully disable the disk swap, no matter how much RAM your computer has! Not only because zswap needs it, but also because disabling the disk swap is a bad idea in general: it's counterproductive to do so and might degrade the performance of your system.

B. Now the how-to:

Now the how-to for setting the swappiness to a more reasonable level for a computer with a weak CPU and an enabled zswap:

a. First check your current swappiness value. Type in the terminal (use copy/paste in order to prevent typos):

cat /proc/sys/vm/swappiness

Press Enter.

The result will probably be 60.

b. To change the swappiness into a more sensible setting for a weak CPU, use copy/paste to transfer the following line into the terminal (this is one long line, definitely use copy/paste in order to avoid typos):

echo "vm.swappiness=45" | sudo tee /etc/sysctl.d/7-swappiness.conf

Press Enter. Type your password when prompted.

c. Reboot your computer.

d. After the reboot, check the new swappiness setting:

- Launch a terminal window.
(You can launch a terminal window like this: *Click*)

- Type (use copy/paste):

cat /proc/sys/vm/swappiness

Press Enter.

Now it should be 45.

Regrets? Undoing swappiness reduction is easy

2.1. Having regrets about the swappiness reduction? Undoing it is simple with this terminal command:

sudo rm -v /etc/sysctl.d/7-swappiness.conf

Reboot and the swappiness should be default again.

Increasing the size of the available swap on the hard disk

3. In Linux Mint and Ubuntu, the virtual memory on the hard disk is a swap file instead of a dedicated swap partition (except when LVM is used, which I don't recommend anyway). This swap file is, unsurprisingly, called swapfile.

By default its size is 2 GiB. Which is fine for most use cases, especially so if you've enabled zswap (see item 1 on this page). Still, a few people may need a bigger swap file. If you're one of them, read on.

You can change the size of this swap file in the following way:

a. Launch a terminal window.
(You can launch a terminal window like this: *Click*)

b. First you're going to check the presence and the size of the current swap file. Copy/paste the following blue command line into the terminal, for which no root permissions are needed:

swapon --show

Press Enter.

It'll probably tell you that there's indeed already a swap file and that its size is currently set at 2 G (2 GiB).

c. Now you need to disable the current swap file. Copy/paste the following blue command line into the terminal:

sudo swapoff /swapfile

Press Enter. Type your password when prompted. In Ubuntu this remains entirely invisible, not even dots will show when you type it, that's normal. In Mint this has changed: you'll see asterisks when you type. Press Enter again.

d. Next, you can delete the current swapfile. Copy/paste the following blue command line into the terminal:

sudo rm -v /swapfile

Press Enter.

e. Next step: create a new swap file of the desired size. For this, you're going to use the application dd to make sure a file without "holes" is created. Using the correct parameters requires a bit of calculation.

An example makes it easy: let's suppose you want to make an 8 GiB swap file. For that, you'll need to calculate the number of MiBs involved: 8 x 1024 MiB = 8192 MiB.

Copy/paste the following blue command line into the terminal, in order to create an 8 GiB swap file:

sudo dd if=/dev/zero of=/swapfile bs=1M count=8192

Press Enter. This might take some time to complete, so wait patiently until it's finished!

f. Assign the swap file read/write permissions for root only (not strictly needed, but it tightens security), with this terminal command (use copy/paste):

sudo chmod -v 600 /swapfile

Press Enter.

g. Format the file as swap, with this terminal command (use copy/paste):

sudo mkswap --verbose /swapfile

Press Enter.

h. The swap file will be activated automatically on the next reboot. However, it's also possible to activate it right away, with this terminal command (use copy/paste):

sudo swapon /swapfile

Press Enter.

i. You can check the size of the active new swap file with the following terminal command, for which no root permissions are needed:

swapon --show

Press Enter. If you've applied the 8 GiB in the example, this should now report that its size is 8 G.

(This is a simplified version of the excellent how-to written by vanadium on Askubuntu)

Other safe speed tweaks

4. Other safe speed tweaks can be found on this page.


Want more?

Do you want more tips and tweaks? There's a lot more of them on this website!

For example:

Clean your Linux Mint safely

Avoid 10 fatal mistakes


To the content of this website applies a Creative Commons license.

Back to the home page

Disclaimer