Back to the home page

Contents of this page:
- 1. The absolute number one: turn on zswap
- 2. When zswap is enabled: decrease swap use when your CPU is weak
- 3. Increasing the size of the available swap on the hard disk
- 4. Other safe speed tweaks
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 noticeable speed gain by turning on zswap. This is especially so on older and weaker machines, but on modern machines there will probably also be a speed gain, even though it'll be smaller.It's therefore advisable to turn zswap on in all cases. Below, I'll explain how to do that.
Background: Why enabling zswap improves memory performance
1.1. First I'll explain why enabling zswap improves the performance of the system memory (RAM). If you're not interested in that background information, you can simply skip this item and scroll straight down to the how-to described in item 1.2 below. Anyway, here goes:On the hard disk there's a separate file or partition for virtual memory, called the swap. Under normal circumstances, even if your machine has lots of RAM, the swap is useful and even makes your system faster (linked web page gone? Download an offline copy here).
That's because the swap is primarily a mechanism for equality of memory reclamation between processes. Or in other words: it helps to divide the memory evenly over the active processes. Only in the second place it also provides "emergency extra memory" when the system's RAM is groaning under heavy load.
Swap is usually not what makes your application slow; a situation in which processes have to fight each other for memory is what makes your application slow.
However, when Mint uses the swap too much, namely when it's being used as "emergency extra memory" after all, the computer does slow down. Excessive 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 (read: hard disks).
That's why zswap has been invented: to mitigate this undesirable effect of excessive swapping, by a reduction of I/O activity. Or in normal human language: zswap will make the system use the hard disk less.
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 set that above 40 percent, because a higher percentage might not leave enough RAM for uncompressed use).
Furthermore, there's a threshold (lower limit) at which zswap will start accepting new pages again after it becomes full. The Ubuntu and Mint default is 90 percent load.
That's not optimal for computers with little RAM, as it might cause too much "to and fro" zswap activity on those machines. My how-to reduces that threshold to a calmer 80 percent, which is a better one-size-fits-all compromise (it's also the threshold that's suggested upstream at kernel.org).
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 data more often. With a weak CPU there might be a noticeable performance hit, as compressing/decompressing data goes quicker with a reasonably fast CPU, preferably a multi-core one.
- The power consumption of your computer will probably increase a bit, because the CPU has to work harder. This is likely to decrease the battery life of a laptop somewhat.
That's why I advise, if your computer has a 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.
How to enable zswap
1.2. In item 1.1 you've learned how zswap works and why it improves memory performance. Now it's time for the actual how-to. You can switch on zswap as follows (with superior fast compression, by means of lz4 and zsmalloc):a. Switching on zswap involves modifying several system parts. So launch Timeshift and make a manual snapshot of your current system. 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).
b. Launch a terminal window.
(You can launch a terminal window like this: *Click*)
c. 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.shrinker_enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=30 zswap.accept_threshold_percent=80 zswap.zpool=zsmalloc/' /etc/default/grub
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. 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 this command into 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 to transfer this complicated command line into the terminal, don't try to type it):
grep -r . /sys/module/zswap/parameters/
Press Enter.
Why zswap is better than zRam (and never use both of them simultaneously!)
1.2.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 as follows:zswap taxes the CPU less than zRam. Sometimes much less: especially with desktop use the difference can be significant. But there's more at play:
As explained in depth in this fine article, zswap sits as gatekeeper in front of your disk swap, compresses pages in RAM, and automatically tiers "cold" data (data which is unlikely to be needed soon) to disk. It integrates directly with the kernel's memory management and distributes pressure gracefully.
zRam on the other hand, is a compressed RAM block device with a hard capacity limit. When you put swap on it and it fills up, there is no automatic eviction, and the kernel has very little leverage to do anything about the situation.
When the zRam "device" is full, the system either starts to kill processes because of acute memory shortage or falls back to lower-priority swap, causing so-called LRU inversion: your fastest storage tier is clogged with the coldest data, with no way to evict it. That actively forces your working set of "warm" data (data that'll probably be used again soon) onto the slowest storage.
In such a case, zRam isn't just failing to help. It is, instead, actively making things worse than having no compressed swap at all.
And even worse, the longer the system has been running, the more broken things get: warm pages drift to disk, cold pages fossilize in zRam, and the gap between what zRam is holding and what you actually need keeps widening. Not exactly helpful....
zRam only really makes sense for extremely memory-constrained embedded systems, diskless setups, or cases with specific security requirements around keeping private data off persistent storage. And there's even more bad news: swap on zRam is also increasingly becoming unsupported upstream.
You can find more background information about why zswap is usually better than zRam here. Web page gone? Download an offline copy of that article 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. Normal usage of the swap makes your system faster, but when Mint uses the swap too much, the computer slows down.Also when the recommended zswap has been turned on (see item 1.2 on this page), this still plays a role: even a fast RAM swap is slower than plain uncompressed RAM...
That's where swappiness comes in.
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 weak and zswap has been turned on, 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 more 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. Only change the default swappiness when you've enabled zswap and your CPU is weak.
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 will probably 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