Let’s face it, some of us don’t upgrade that often. Or, at least, as Linux users, we like to re-purpose our older laptops/computers. A lot of distributions run comfortably on systems with low amounts of RAM (Random Access Memory). Browsers, however, are not that forgiving, requiring more and more memory as the Internet evolves.
While a system with the LXDE desktop environment on it may take as little as 150MB of RAM after it boots up, Chromium, Chrome or Firefox will quickly eat up hundreds of megabytes and even multiple gigabytes of RAM as we open a few tabs.
And that’s when it happens! Hard-disk starts to make that grinding sound. Music starts to stutter. When we switch to another application, it takes tens of seconds until it appears on the screen. Even the mouse cursor may begin to lag. This is the effect of disk thrashing.
What Is Disk Thrashing?
First, we need to understand swap. When we run out of free RAM, application data that the operating system didn’t access in a long time gets moved to the swap area. This is often on a physical storage device. By doing this, the operating system can free up some memory space that a program needs now. This way, a tab in our browser that we didn’t access in the last hour may be swapped to the hard disk. Now the GIMP image editor can run. When we close it and switch back to that old tab, the operating system swaps it back to memory (reads it from disk and copies it back to random access memory).
This mechanism works well when swapping small amounts of data. But when, let’s say, only 2GB of RAM is available and all the programs we have open need 4GB, the operating system has to swap constantly. Moving data from memory to disk, from disk to memory, and back again, constantly, is very slow, especially with harddisks. In such a situation, the storage device is unable to fulfill requests fast enough. Long story short, when disk thrashing occurs, things get very slow.
SSD Drives Are Great for Swap
There’s a myth still circulating on the Internet: “Never set up swap on SSD. It will kill your device!” That was true a few years ago, but not anymore. In fact, setting up swap on an SSD drive leads to great results. If the computer includes an SSD device that has good random read/write performance (at least 300MB), set up a swap partition on it and ignore the rest of this article. When the operating system has to swap 1GB of data, it will take less than five seconds, while on a hard disk it would take more than a minute.
How to Significantly Reduce Disk Thrashing on Linux
Fortunately, most of the data stored in RAM is very compressible. This means that if we take 200MB of data stored in memory and compress it, we can reduce the size to 100MB. Consequently, it means that the system will have to write much less data to disk when swapping.
Imagine that instead of waiting thirty seconds to switch to a program you have to wait fifteen. It’s a palpable improvement. But, in fact, the speed-up is much more significant (may have to wait less than ten seconds) because the tool we will use is more sophisticated than that. It intelligently sends to disk only the bits and pieces that cannot be compressed, keeping the rest in a reserved portion of memory. If you want to read more about its internals, check out the official Linux kernel page about zswap.
A simple version of the benefits we get by using zswap is that instead of horrible slowdowns when we run out of memory, we get tolerable, mild slowdowns that last for seconds instead of tens of seconds or even minutes.
How to Enable zswap On Debian-Based Distributions (Ubuntu, Linux Mint, etc.)
Open a terminal application and run this command to edit the Grub bootloader template configuration file:
With your cursor keys, navigate to the line that begins with “GRUB_CMDLINE_LINUX_DEFAULT”. Within the quotes, add the following text:
For example, a line that was
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" becomes
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1".
To save the file, press Ctrl + X, followed by “y,” and then Enter.
Generate the new Grub configuration files according to the template we previously edited:
Reboot and the zswap module will be enabled automatically.
How to Enable zswap on Fedora and OpenSUSE
The procedure here is similar to the one in the previous section but with a few twists. Edit the Grub template configuration file.
zswap.enabled=1 within the quotes, on the line that starts with
Press Ctrl + X, followed by “y,” and then Enter to save the file.
Depending on whether your computer boots from a BIOS or UEFI system, the Grub configuration file is generated in a different path. To find where it currently resides, enter the following command:
sudo find /boot/ -name grub.cfg
Use the path that is output here in the next command. For example, in our case the path found was “/boot/grub2/grub.cfg,” so we’ll add it at the end of the next command after “-o”:
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Reboot the computer. After you reboot, check if the module is active:
If the output is “Y,” then the module is working correctly.
How to Enable zswap on Arch Linux
Install the systemd-swap package:
sudo pacman -Sy systemd-swap
Make this package work its magic at boot time:
sudo systemctl enable systemd-swap
Start it now, without rebooting:
sudo systemctl start systemd-swap
To check if everything worked as expected, verify if the zswap module is enabled:
If the output is “Y,” it means the module is active.
How to Enable zswap on Different Distributions
Generally speaking, enabling zswap at boot time is as easy as adding
zswap.enabled=1 to your kernel parameters. Most distributions use Grub2 as a bootloader by default. If you are using a different bootloader, find the configuration file where it stores the kernel parameters to pass at boot time and add
zswap.enabled=1 as an additional parameter.
Another parameter you can play with to find if it improves performance for your specific use case, is
zswap.max_pool_percent=20. You can adjust it by changing the number at the end: for example,
Add it to the same kernel parameters where you add
zswap.enabled=1. It’s recommended you don’t go above 50% since more than that can have detrimental effects on systems with low amounts of RAM.
If you frequently ran out of RAM on your old system and experienced the annoying effects of disk thrashing, you’ll immediately notice and appreciate the difference from now on. Instead of horrible slowdowns, you’ll experience mild, temporary lag that lasts for a few seconds (instead of minutes) when the operating system has to swap a lot of data to disk. You’ll be able to continue work in situations where before you couldn’t use your computer at all during the time it swapped.
Our latest tutorials delivered straight to your inbox