RAM is getting expensive, so squeeze the most from it • The Register


Linux has two ways to do memory compression – zram and zswap – but you rarely hear about the second. The Register compares and contrasts them.

The Linux kernel’s zram tool has been in the news this week. Phoronix reports that the new patch could make some zram operations 50 percent faster.

Thanks to the increasingly desperate flailing by the hucksters selling automatic plagiarism machines, RAM has got much more expensive and the price rises are expected to continue. Apple has even quietly dropped the 512 GB RAM model of its Mac Studio. This is not a good time to buy computers with lots of RAM, or to add more RAM to existing machines.

Better to try to make the most of what you’ve already got. So we thought this might be a good time to look at what zram is and does, and explore its less well-known companion zswap.

Both work with Linux’s existing swapping mechanism. Swapping (called paging in Windows) is a way for the kernel to handle running low on available RAM. It chooses pages of memory that aren’t in use right now and copies them to disk, then those blocks can be marked as free and reused for something else. This could be a whole process and its data, which gets paused and swapped out from RAM onto disk. The hope is that some other program will finish, which frees up its RAM, and the process can be swapped in again. Unfortunately, in real life, it’s more likely that the poor stressed user switches to the inactive program, which means something else has to get swapped out to make room for it.

This matters because it means that to use either zram or zswap, you need some form of swap.

Swapping is a basic mechanism that comes along as part of the deal with any OS that has virtual memory. That broadly means any OS that can use a hardware memory-management unit. For the PC industry, this means anything since Linux (1991) and Windows NT (1993). From reading far too many posts in far too many techie message boards, we get the impression that a lot of people these days think “My computer has enough RAM. I don’t need swap, so I turned it off.”

This is wrong. Don’t do it. Always configure some swap, just in case.

It is like concluding that you are happy with your body weight, and so put a wire waistband in all your trousers (underpants, skirts, whatever you prefer). Sure, on a normal day they will fit, but you might go to a really good dinner and eat too much, or you might get sick and not eat and lose a bit of weight, and suddenly they will all painfully cut into you, or they’ll fall down. A little bit of elastic, even if only in your underwear, makes life much more comfortable.

Having some swap is an elastic waistband for your computer. In normal use, the machine might never need it, but if something goes a bit wrong one day, it’s much better to have it than, metaphorically speaking, have your clothes split and fall off. One day you will be glad there was a little slack or a little give.

So, first, let’s talk briefly about swap space.

What kind of swap?

By default, there are basically two options here: a swap file, or a swap partition. Ever since version 17.04, Ubuntu has used a swap file by default. When the Linux kernel was young, this was slower, but there’s been next to no difference in a decade or more. The only real snag is that you need more room in the root partition.

From old habit, The Reg FOSS desk always sets up a dedicated swap partition. One advantage is that if you have more than one physical drive, you can put the swap partition on a different drive, which spreads the load on the disks a little. Back when SSDs were expensive and wore out, we kitted out machines with a small SSD and a big HDD, and put the OS on the SSD and the swap on spinning rust, but that isn’t really necessary any more. Just bung an extra partition on the end of one of your SSDs, and during installation tell the installer that’s for swap.

How much? That’s contentious. Perlod’s guidelines seem reasonable:

Zram

Thinking about where swap is, and how much, is where zram comes in. As is often the case, the Arch wiki explains it well. Zram creates compressed dynamic RAM disks. “Dynamic” meaning they grow only as they fill up, and “compressed” means with LZ4, which is fast. One use for those RAM disks is for swapping.

This sounds insane: swapping from memory to memory? What’s the point? But it is not mad, it works. The gist is that your computer swaps stuff out when it’s short of space. Normally, that means to disk. For instance, a 500 MB program gets turfed out of RAM and stuck in half a gigabyte of disk space, freeing up half a gig of RAM for other stuff.

The problem is, swap is very slow, because disks are very slow compared to RAM. To understand how many thousands of times slower, there’s a handy table of Latency Numbers Every Programmer Should Know on GitHub, and a humanized version that expresses the time differentials in terms more meaningful to us meatsacks.

The idea of zram is: rather than take the time to write it out to disk, why not compress it and keep it in memory? With a fast CPU, it won’t take long, and it will take about half as much space. When you need it back, swapping it in just means decompressing it from RAM to RAM, which is relatively quick.

It works surprisingly well and some distros enable it by default these days, such as Fedora and derivatives.

Memory compression is mainstream tech now: macOS has used memory compression since 10.9, and Windows 10 gained the feature in 2015 in Build 10525.

There are two main use cases for swapping to zram. One is if your machine has enough RAM that you rarely run low. In this scenario, it takes a little of the free memory that the kernel would use for your disk cache anyway, and hey presto, you can have more programs in memory at once.

The other situation is that you might not have much spare memory, but your system drive is slow, or small, or limited in how many writes it can handle. This applies to computers which boot from microSD cards, or similar onboard storage such as an eMMC drive. This particularly applies to the Raspberry Pi, and to many low-end single-board computers and cheap tablets.

Here, zram lets you avoid wearing out the card, and helps prevent you from running out of memory. It lets you over-commit your RAM: load more stuff into memory than you have available memory, because the stuff that isn’t running right now has effectively been zipped so it takes less space.

This only works so far – it can’t compress everything in your memory: the kernel needs some workspace, and room for whatever task is currently running. The good news is that the kernel natively understands the idea of multiple separate areas of swap space, and can prioritize them. So you can have some zram as well as a swap file, and it will move the least recently used stuff from zram into disk swap as needed.

In our testing of Pop!_OS 24.04 so far, we found it configures two-level swap like this: first zram, then an encrypted swap file in the root partition.

To summarize, zram can take the place of swap on disk. Or, if you have the room to spare, you can use it as well as plain old swapping to disk, in which case it acts as a sort of compressed cache for your swap, speeding swap performance slightly. That’s why, earlier in its development, it was called compcache.

Zswap

The main alternative to zram is zswap, which works rather differently.

Zswap isn’t an alternative to, or complement for, having swap on disk. It requires swap on disk, but does its best to reduce how much your OS uses that swap by compressing stuff before it gets to the swap space. For the technical nitty-gritty, its developer described how it works for LWN in 2013: the zswap compressed swap cache.

There’s a short sharp description in the Debian wiki. The good thing about zswap is that using it is basically as simple as turning it on or off. You can even do it on the fly, although we usually enable it on all our machines, right from the bootloader. If your distro boots using GRUB, it’s as simple editing one line in one config file. For distros that use systemd-boot, it’s a bit more complicated – that is systemd all over – but it’s only a couple more steps.

Pop!_OS is one example – but for that distro, compressed swap needs more work. Two things need to be turned off for this to provide any performance benefit. Firstly, Pop!_OS enables zram by default, so you need to disable zram. Then reboot and check it’s gone. (Hint: sudo swapon -s lists all active swap volumes.)

Secondly, Pop defaults to encrypting its swap file. The encryption and decryption reduces performance, and zswap is a performance optimization. So you should disable cryptswap too. Afterwards, for (much) faster boots, remove the /etc/crypttab file too. (We just renamed it to crypttab.old from old paranoid habit.)

Do both, and don’t forget to reboot each time. When you’re sure both zram and cryptsetup are gone, then try enabling zswap.

One or the other, not both

Zram can work alongside a normal swap file, but not alongside zswap. Both are trying to do the same thing, and any performance improvement either could give will be eliminated by them duplicating one another’s work and wasting the very resource you’re trying to use more efficiently. Use zram, or zswap, but don’t even try both at once.

We’ve tried both, and we do use zram on things like our Raspberry Pis and on custom bootable Linux keys. However, for general desktop and laptop use, we find zswap works better – and others find the same.

We know a lot of people also advocate encrypting your machines’ drives, especially laptops. We feel that XKCD 538 applies here. Turn it off, especially swap. If possible, configure a dedicated partition – then enable zswap, and leave it well alone.

Bootnote

There is a third alternative as well, called Zcache. This is a bolder and more general effort than either zram or zswap, but despite decades of work, it is still quite experimental and it’s not just a feature that you can turn on or off. For that reason, we decided to leave it out in favor of the two things you can have right now, off the metaphorical shelf. ®



Source link