Gray Matter


Linux Swapping Mechanisms

RT Cunningham | October 15, 2020 (UTC) | Linux

swappingThere are three swapping mechanisms available to Linux distributions: Swap, zswap and zram. I’m using zram with Linux Mint on my laptop computer these days. For my purposes, it works far better than the other alternatives. Yes, I have experience with most swapping systems, including the Windows paging file.

Published opinions say that when you have enough memory, You don’t need to swap. The only time that’s true is when you never use 100 percent of your memory. One out-of-control process can eat up 100 percent quickly.

Swap - The Default Swapping Mechanism

The default swapping mechanism in Linux is just swap (or plain swap). It’s either set up as a swap partition or as a swap file, depending on how it’s installed by your Linux distribution. Modern internal storage drives are large enough that worrying about that space is completely unnecessary.

A section on paging at Wikipedia explains it far better than I can.

Zswap - Included with Modern Linux Distributions

I can’t speak for all distributions, of course. The Debian/Ubuntu-based distributions I’ve used in the last few years have it already installed. It just needs to be activated. The documentation page at explains it well enough.

In my experience, zswap didn’t work much better than swap. When a rogue process caused my computer to use 80-85 percent of its memory, swapping slowed everything down to minutes instead of seconds.

I haven’t used plain swap in so long, I can’t tell you when swapping started to kick in. With zswap, and with swappiness set to 10, the system started swapping before it reached 60 percent.

Zram - An Installable Alternative

If you search for and read zram information and opinions at various sources, it wouldn’t seem to be mature enough to work with. Well, it was included in the mainline Linux kernel at version 3.14 in March 2014. As of the original date of this article (in case I rewrite it later on), it’s been more than six years. I should think it’s pretty mature by now.

When I installed Linux Mint as a Windows and Linux dual-booting system, I immediately enabled zswap. Zswap works in conjunction with a swap file or partition. Based on how I used Linux, I would have to eventually turn swap on and off to free up the main memory.

When I removed every swapping mechanism and installed zram, things changed for the better. Ever since I’ve had zram installed, I’ve yet to see any swap being used at all under normal circumstances. Once I started using Windows in a virtual environment, I started seeing swap space being used, but it was in the low megabytes.

I now have 16 gigabytes of memory in my laptop. My memory usage never exceeds 4 unless I start my Windows virtual machine. Regardless of how much memory I use, there are always a few megabytes being transferred to swap. I’ve never used more than 11 out of the 16 since installing the memory. Setting aside a couple of gigabytes for zram won’t affect my workflow in any way.

You can read more about zram at the documentation page.

Controlling Zram on Ubuntu, Linux Mint and Other Derivatives

Installing zram (sudo apt install zram-config) also installs the zramctl program. I don’t know how zram is controlled on other distributions. I edited /usr/bin/init-zram-swapping to change the divisor to 16 instead of 2 (my CPU cores) so that my swap space is just under a gigabyte (it should be obvious that the full RAM amount is never available).

Of the two laptop computers I still own, only this one is beefed up with an extra SATA drive and the maximum amount of memory it can hold. When I return to the Philippines, I intend to do the same with the older laptop computer I have waiting for me there.

Photo Attribution: Larry White from Pixabay
Edited and updated. Originally published at one of my other websites in August 2019.

Share: Facebook | Twitter

These Posts May Also Interest You: