What is zRAM in Linux?
In Linux, zRAM is a kernel module that creates a compressed block device in RAM. Instead of paging out inactive memory pages to a slow physical swap partition on a hard drive or SSD, the Linux kernel compresses those pages and keeps them in the zRAM block device.
This drastically improves system responsiveness on devices with limited memory (such as older laptops, Raspberry Pi boards, and lightweight virtual machines) because reading and writing to compressed RAM is exponentially faster than reading and writing to physical disk storage. It also extends the lifespan of SSDs and SD cards by significantly reducing swap write operations.
Step 1: Check if zRAM is Already Active
Before configuring zRAM, you should check if your Linux distribution already has it enabled (modern distributions like Fedora and Pop!_OS enable it by default). Open your terminal and run:
zramctl
If the command returns no output, zRAM is not currently configured. If it returns a table showing a device (like /dev/zram0), the module is already active.
Step 2: Install zRAM Tools
The easiest and safest way to configure zRAM on Debian/Ubuntu-based distributions (including Linux Mint and Raspberry Pi OS) is by using the zram-tools package. This utility handles the kernel module loading and systemd service creation automatically.
Run the following command to install it:
sudo apt update && sudo apt install zram-tools
Once installed, the system will automatically create a default zRAM swap drive. You can verify this by running zramctl again.
Step 3: Configure the zRAM Size and Compression Algorithm
By default, zram-tools allocates a conservative amount of memory. To maximize performance, you should configure the percentage of RAM it is allowed to use and set a modern compression algorithm.
Open the configuration file in a text editor like nano:
sudo nano /etc/default/zramswap
Uncomment and modify the following lines in the file:
1. Choose the Compression Algorithm
Look for the ALGO variable. Change it to zstd, which offers an excellent balance of high compression ratio and extremely fast decompression speed:
ALGO=zstd
2. Set the RAM Allocation
Look for the PERCENT variable. This dictates how much of your total physical RAM can be allocated to the compressed block. A standard recommendation is 50%:
PERCENT=50
Note: Setting this to 50 does not mean it immediately consumes 50% of your RAM. It simply sets a maximum ceiling. The zRAM block dynamically resizes based on how much compressed swap data it actually holds.
Save the file by pressing Ctrl + O, Enter, and exit nano using Ctrl + X.
Step 4: Restart the Service and Verify
To apply the new configuration, restart the zRAM systemd service:
sudo systemctl restart zramswap
Now, verify the configuration by checking your swap usage:
swapon --show
You should see /dev/zram0 listed with a high priority (usually 100), meaning the Linux kernel will prioritize swapping to the fast zRAM block before it ever attempts to use your slow physical disk swap.
When Should You Use zRAM?
zRAM is highly recommended for systems with 8GB of RAM or less. It is virtually essential for 2GB and 4GB Raspberry Pi boards. However, if your system has 32GB or 64GB of RAM and rarely enters swap space, enabling zRAM will not provide a noticeable performance benefit and will unnecessarily consume CPU cycles for compression.