Ubuntu 22.04 LTS to leverage zswap to run on Raspberry Pi 4 with 2GB RAM

Canonical used to recommend Raspberry Pi 4 with at least 4GB RAM to run Ubuntu Desktop, but Ubuntu 22.04 LTS should run more smoothly on the Raspberry Pi 4 2GB as the company has enabled zswap by default to allow the Linux operating system to run better on systems with less memory.

Canonical explains that zswap is essentially a compression tool.

When a process is about to be moved to the swap file, zswap compresses it and checks whether the new, smaller size still needs to be moved or if it can stay in your RAM. It is much quicker to decompress a ‘zswapped’ page than it is to access the swap file so this is a great way of getting more bang for your buck from systems with smaller amounts of RAM.

Raspberry Pi 4 2GB Ubuntu 22.04

The good news is that you don’t even need to wait for Ubuntu 22.04 LTS to come out, since it can be enabled manually in Raspberry Pi 4 as follows:


Or simply edit /boot/firmware/cmdline.txt file, and add the following string to the existing command:


After a reboot, that should even help the performance of Raspberry Pi 4 boards with 4GB and 8GB RAM, but with the 2GB model, some extra steps are needed to enable z3fold allocator & lz4 compression. The commands to run are explained in detail in Ubuntu blog, and in the end, you’ll have loaded a few more modules and your cmdline.txt should have three extra parameters:


There will be nothing to do in Ubuntu 22.04 LTS as those will be enabled by default.

We previously wrote about enabling ZRAM in Ubuntu to improve the performance and reliability of systems low on RAM. So what’s the difference between zram and zswap exactly? I understand zram is like a compressed swap that resides in RAM, while zswap uses both storage and RAM, and only stores the compressed data into the swap file on the storage device, if there’s not enough RAM to hold the data.

That question was also asked on Stackoverflow so we have a more detailed answer (edited for brevity):

zram
Status: Available since Linux 3.14 (March 2014)
Implementation: compressed block device, memory is dynamically allocated as data is stored
Usage: Configure zram block device as a swap device to eliminate need for physical swap defice or swap file

Benefits:

  • Eliminates need for physical swap device.
  • A zram block device can be used for other applications other than swap, anything you might use a block device for conceivably.

Drawbacks:

  • Once a page is stored in zram it will remain there until paged in or invalidated. … zram can not do evict pages out to physical disk.

zswap
Status: Available since Linux 3.11 (September 2013)
Implementation: compressed in-kernel cache for swap pages. In-kernel cache is compressed, the compression algorithm is pluggable using the CryptoAPI and the storage for pages is dynamically allocated. Older pages can be evicted to disk making this a sort of write-behind cache.
Usage: Cache swap pages destined for regular swap devices (or swap files).

Benefits:

  • Integration with swap code (using Frontswap API) allows zswap to choose to store only pages that compress well and handle memory allocation failures, in those cases pages are sent to the backing swap device.
  • Oldest pages in the cache are pushed out to backing swap device to make room for newer pages.

Drawbacks:

  • Needs a physical swap device (or swapfile).

Thanks to Geokon for the tip.

Share this:
FacebookTwitterHacker NewsSlashdotRedditLinkedInPinterestFlipboardMeWeLineEmailShare

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK Pi 4C Plus

10 Replies to “Ubuntu 22.04 LTS to leverage zswap to run on Raspberry Pi 4 with 2GB RAM”

    1. What did ‘Armbian’ provide since 2014?

      Armbian used an 128MB ’emergency swap file’ with vm.swappiness=0 setting until few years ago. After months or even a year of testing zram became default in 2018 and the swap file was gone. And only recently (Dec 2020) the zram config script had been adjusted to not conflict with zswap which always was up to the kernel and user in question to be used or not.

      1. I checked upon first mentioning. “Proper” implementation in 2017/18, more proper in 2020 but still not perfect for everyone … since someone recently reported “he has to have full control over his memory, so he is disabling zram 1st”

        1. > someone recently reported “he has to have full control over his memory, so he is disabling zram 1st”

          There are also people who still run a monthly defrag on their SSDs.

          Anyway it seems you still confuse zram with zswap…

  1. > Or simply edit /boot/firmware/cmdline.txt file, and add the line

    That’s not a good idea to add lines to cmdline.txt since ThreadX (that’s the OS parsing the secondary OS’s kernel params) can’t cope with multiple lines. ‘zswap.enabled=1’ needs to be appended separated by spaces to the one existing line.

      1. BTW: I would believe the initramfs stuff is still important (at least on x86 it’s needed to get things working as desired). Maybe you can test since the Internet is full of wrong tutorials that get copy&pasted over and over again…

  2. > I understand zram is like a compressed swap that resides in RAM

    It can also use a ‘backing device’ since some kernel version and could always be combined with regular swap files/partitions which has its own drawbacks (see comments in the linked ‘enabling ZRAM in Ubuntu‘ thread).

    In general zram is great if swap on storage should be avoided which applies especially to SBC running off flash memory with horribly low random I/O performance and low endurance (AKA ‘average SD card’).

  3. Bit depressing that ubuntu can’t run in 2GB of RAM without this. I’m not one of those save every K on machines with 32 or 64GB of RAM people but how much is ubuntu really doing to not be able to fit into 2GB of RAM + a bit of swap. Is it all of the overlayed squashfs crap?

    That said apt-get on a bare minimum Debian install causes OOMs on a 128MB machine. Pretty sure that used to work fine a few years ago.

    1. > Bit depressing that ubuntu can’t run in 2GB of RAM without this

      Huh? That works perfectly fine. But here it’s not about Ubuntu in general but about something else: https://ubuntu.com/tutorials/how-to-install-ubuntu-desktop-on-raspberry-pi-4

      They advertise this as a fully functional desktop (IDE, tons of browser tabs open) and oviously configure swap on the rootfs which simply sucks once it happens on slow SD cards. Enabling zswap is a cheap ‘fix’.

Leave a Reply

Your email address will not be published. Required fields are marked *

Khadas VIM4 SBC
Khadas VIM4 SBC