Use zram for swap

Last updated on 2021-11-07 Tagged under  # debian  # linux

Instead of creating a separate swap partition or using a swapfile, its possible to create a swap device in RAM itself with the Linux kernel module zram. Even though its been around for years, I'm learning about it for the first time and its pretty cool!

Zram creates a compressed block device in memory, and the RAM assigned to it is only used for swapping as-needed (until then, the RAM is still available for use by applications). Swap data stored is compressed thereby allowing more data to be stored in RAM. Performing read/write operations in memory is both a speed boost and reduces wear on SSD drives, at the cost of more CPU-intensive activity.

I'm currently giving it a go on my Thinkpad X230 with 8GB RAM running Debian 11 Bullseye, with 50 percent of memory assigned to the zram0 device. No other swap devices are in use.

Let's go!

1. Switch off any active swaps ...

$ sudo swapoff --all && free

2. Check if any swap devices exist in fstab ...

$ grep swap /etc/fstab

If so, comment out the swap entries to disable.

3. Install zram-tools ...

$ sudo apt install zram-tools

4. Shutdown any zram devices that might have been auto-started ...

$ sudo zramswap stop

5. Set desired options in /etc/default/zramswap. An example ...


6. Start ...

$ sudo zramswap start
$ sudo zramswap status
/dev/zram0 lz4           3.8G   4K   64B    4K       4 [SWAP]

Going forward, the zram0 swap device will be auto-created at next boot.


» Later: Getting started with Arch Linux

« Earlier: New life for an old laptop as a Linux home server