Tips for speeding up your Linux system
Tweak Talk
If you are looking for ways to speed up your Linux, consider this collection of curated performance tweaks.
Linux is renowned as a high-performance operating system, and it runs on nearly all of the world's most powerful supercomputers. It also runs very well on regular desktops and workstations, but sometimes people ask for more. Whether you're faced with a low-end hardware setup or a loaded production system with high I/O, there is always room for tweaks and optimizations. Linux is an ideal OS for tinkering, and you have many options for eliminating performance bottlenecks, fixing non-optimal settings, and making the system more fluid and responsive. The goal of this article is to point you to some best practices for tweaking a typical home or office Linux-powered machine, while avoiding some of the outdated or less efficient advice.
The Curse of Low Memory
"Buy more RAM" – that's a frequent response to "I've got only 2GBs." However, sometimes it is not possible to install more memory bars into a computer. An average Linux desktop runs butter-smooth with 8GB, very nicely with 4GB (with some limitations to multitasking), and quite poorly with 2GB or less. Some palliative techniques that bring relief include using zram and zswap. These are the two methods of compressing memory to take down (or even completely avoid) swapping memory pages to the hard drive. Thanks to compression, the system has more free RAM, and with the lower swapping, the filesystem also speeds up. The trade-off is a higher CPU load due to constant compressing and decompressing, but its impact is usually smaller than the lagging caused by a running out RAM.
Zram is a compressed RAM-based swap device designed for systems with no physical swap partitions. It is a Linux kernel module (included since kernel 3.14) that creates a very fast virtual block device backed by RAM and sets it as a top-priority swap "partition." All you need to do is install the supplementary package for the zram systemd service and enable it. In Ubuntu, use the following commands:
$ sudo apt install zram-config $ sudo systemctl start zram-config
Now there are extra /dev/zramX
virtual devices, one per each of your CPU cores. It is easy to see the new swap setup with the $ sudo zramcltl
command (Figure 1). The size of the devices is determined automatically based on your system's specs, although you can still change it. The following example sets /dev/zram0
to 1GB:
# swapoff /dev/zram0 && zramctl --size 1024M /dev/zram0 # mkswap /dev/zram0 && swapon /dev/zram0
Zswap is different in that it is designed for systems with a normal swap partition. If there is a swap partition, zswap provides a compressed cache that sits between RAM and the swap device. When it is time to offload memory pages to disk, zswap compresses the pages and stores them in the dedicated RAM cache. This technique is often referred to as a writeback cache, and it significantly reduces the disk I/O.
Zswap is a kernel feature (version 3.15 onwards) that you can enable and configure via the GRUB2 bootloader parameters – in the most basic case, it is enough to add zswap.enabled=1
. I'll explicitly define the compression algorithm as LZ4, tell zswap to allocate 30 percent of RAM, and set the allocator to z3fold for better memory management:
zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=30 zswap.zpool=z3fold
It is theoretically possible to use both zram and zswap at the same time, but this option is not known to improve performance more than the improvement you get from choosing one of the tools. When deciding on a compressing method for your system, keep in mind that zram works best in situations where you want to avoid HDD or SSD wear out (e.g., on small systems with flash storage). Zswap is a more general-purpose tweak.
More on Swapping
Depending on the amount of available RAM, it may be worth adjusting parameters such as swappiness and disk cache pressure. Both take settings from /etc/sysctl.d/99-sysctl.conf
. There is a popular mistaken belief that swappiness represents a threshold for RAM usage, and when the amount of used RAM hits that threshold, swapping starts. In fact, the vm.swappines
setting is a ratio (0-100) that controls the aggressiveness of swapping, particularly the memory pages reclamation by the kernel. Changing swappiness only makes sense for systems with slow or aging mechanical drives in order to reduce the swapping activity to some degree. For instance, changing the value from 60 (default) to 40 is a good practice for making a slow machine perform better, especially if it has at least 2GB of RAM.
Disk cache pressure (vm.vfs_cache_pressure
) controls the tendency of the kernel to reclaim the memory that is used for caching of directory and inode objects. Setting the value to 0 leads to out-of-memory lockdowns, whereas increasing it over the default value of 100 makes the Linux kernel reclaim VFS inodes too actively and slows down the system. The optimal value is perhaps 50; at least my test showed that setting the value to 50 makes many filesystem operations (like finding files) noticeably faster.
Tinkering with the Kernel?
There are lots of third-party patches for Linux that require manually applying and rebuilding the whole source tree of the kernel, which could be a difficult and time-consuming task. The promising, efficient, and easy way to include these options is the XanMod kernel.
The XanMod project provides a patch set and prebuilt kernel packages for Debian, Ubuntu, Arch, and Gentoo. This modified kernel features the optimized CPU scheduler, Budget Fair Queueing (BFQ) I/O scheduler (the tool that controls the disk read/write requests management, which plays a huge role in keeping the OS responsive when the disk is under heavy load), and other tweaks for better preemptive multitasking and lower latencies. When compared to the stock Linux kernel in such tests as encoding something with FFmpeg, resizing and rotating images in Gimp, and similar CPU-heavy tasks, the XanMod 5.4 kernel was 8-10 percent faster, which was a solid margin. Install XanMod in Ubuntu as follows:
$ echo 'deb http://deb.xanmod.org releases main' | sudo tee /etc/apt/sources.list.d/xanmod-kernel.list && wget -qO - https://dl.xanmod.org/gpg.key | sudo apt-key add $ sudo apt update && sudo apt install linux-xanmod
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.