ZRam: Compress Your RAM
ZRam is a Linux kernel module that creates a compressed block device in RAM. Think of it as a way to "squeeze" more data into your physical memory. Instead of swapping out to a slow disk when RAM gets full, Linux can swap into this compressed ZRam device, which is significantly faster.
This is especially great for systems with limited RAM, as it trades a little bit of CPU for a lot more usable memory space.
Installation
Debian makes this incredibly easy with the zram-tools package. It handles all the setup scripts for us.
We will install the package:
Configuration
By default, the package sets up a basic configuration, but let's tweak it to get the best performance. We want to use the zstd compression algorithm (which offers a great balance of speed and compression ratio) and dedicate a healthy chunk of RAM to it.
We will edit the configuration file /etc/default/zramswap:
Once we've saved our changes, we need to restart the service to apply them:
Verification
Let's check if our ZRam device is up and running. The zramctl tool gives us a nice summary. You should see output similar to this, showing the algorithm (zstd) and the disk size:
We can also verify that it's being heavily utilized as a high-priority swap device: