Linux RAM compression to extend notebook life
The Linux community was surprised when Canonical increased the minimum memory (RAM) requirement for Ubuntu Desktop from 4 GB to 6 GB. I usually ignore such changes because, when looking at the Linux distribution packages, nothing seems to change dramatically.
But after upgrading from Ubuntu 24.04 to Ubuntu 26.04 on an 11-year-old Lenovo X250 laptop, it became impossible to use it productively. The whole computer would freeze for a few seconds and then continue running. Sometimes the Linux Out-of-Memory (OOM) killer also terminated the entire graphical GNOME session. I thought the laptop had a hardware issue, but it was running fine on Ubuntu 24.04. So it had to be related to RAM usage. I saw that many gigabytes were stored in the swap file even when only a few browser tabs were open. My assumption is that the freezes happened when the Linux kernel restored data from the swap file back into RAM.
The computer has only 8 GB of RAM and a 16 GB swap file, but the SSD uses SATA, where the maximum data rate is about 500 MB/s. So the freezes could have been caused by that.
So what can be done? Increasing the computer’s RAM is not easy because it has only one memory slot and officially supports only 8 GB. According to forum posts, there is a specific 16 GB RAM module that should work, but even buying it used on eBay is really expensive (about 100 EUR) and not worth it for an 11-year-old system.
I know from some embedded systems that Linux supports RAM compression and that it is used there. But can it also be used on desktop Linux? Yes, it can. There are some blog articles that explain it, for example this one: https://blog.guillaumea.fr/post/zram-memory-optimization-ubuntu-server/.
After reading some articles, I realized how simple it is. Just installing zram-tools with sudo apt-get install zram-tools does the job. Technically, zram-tools adds a RAM-backed block device and stores compressed swap data on it.
You can see it with the swapon command by looking at the /dev/zram0 device:
$ swapon
NAME TYPE SIZE USED PRIO
/swapfile file 16G 0B -1
/dev/dm-2 partition 980M 0B -1
/dev/zram0 partition 3,5G 997,4M 100Furthermore, you can check the RAM compression ratio with zramctl.
$ zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4 3,5G 966,1M 349,2M 357,9M [SWAP]By default, zram-tools uses 50% of the RAM. This can be configured in the file /etc/default/zramswap. I left the configuration untouched.
After working with RAM compression for a few weeks, the computer freezes are gone and the laptop can be used again for daily work. I do not know why the RAM usage behavior changed so much from Ubuntu 24.04 to Ubuntu 26.04, but RAM compression extended the laptop’s useful life by a few years.