Running multiple browser tabs, virtual machines, or memory-hungry applications on a Linux system with limited RAM often leads to sluggish performance or system freezes. Traditional swap partitions or swapfiles on hard drives and SSDs are much slower than RAM, causing noticeable delays when the system runs out of physical memory. Zram offers a practical solution by compressing swap data directly in RAM, allowing systems to store more data in memory and significantly reduce slow disk access.
Enabling and Configuring Zram on Linux
Step 1: Install the Zram management package for your distribution. On Debian, Ubuntu, and derivatives, the zram-tools
package streamlines setup and configuration. Open a terminal and run:
sudo apt update
sudo apt install zram-tools
This package automatically sets up a Zram device and provides the zramctl
utility for monitoring and management.
Step 2: Adjust the Zram configuration to suit your system’s workload and available RAM. Edit the configuration file at /etc/default/zramswap
:
sudo nano /etc/default/zramswap
Set the compression algorithm and the percentage of RAM to allocate for Zram swap. For example, using the efficient zstd
algorithm and allocating 20% of total RAM:
ALGO=zstd
PERCENT=20
On systems with less than 8 GB of RAM, you may benefit from increasing PERCENT
to 50 for more swap headroom. Save and close the file.
Step 3: Reload the Zram service to apply your changes:
sudo systemctl restart zramswap
Verify Zram is active with:
zramctl
or
swapon --show
The output should display a /dev/zram0
device, indicating that compressed swap in RAM is now available.
Optimizing Memory Behavior for Zram Efficiency
By default, Linux may still use disk-based swap or manage cached data in ways that do not take full advantage of Zram. Fine-tuning system parameters can improve responsiveness and prevent unnecessary slowdowns.
Step 4: Adjust kernel memory management settings by creating or editing a custom sysctl configuration file:
sudo nano /etc/sysctl.d/99-zram-tweaks.conf
Add the following settings to balance swap usage, cache retention, and disk write behavior:
vm.swappiness=50
vm.vfs_cache_pressure=50
vm.dirty_background_ratio=5
vm.dirty_ratio=10
vm.swappiness
controls how aggressively the kernel uses swap. A value of 50 encourages use of Zram swap without overusing it. Lower values delay swapping, while higher values use swap sooner.vm.vfs_cache_pressure
sets how quickly inode and dentry caches are reclaimed. A value of 50 keeps more file metadata in RAM for faster file access.vm.dirty_background_ratio
andvm.dirty_ratio
determine when the system starts writing unsaved data to disk. Lower values reduce the risk of sudden large disk writes that can cause slowdowns, especially on HDDs.
Apply these changes immediately:
sudo sysctl --system
For systems with very limited RAM or slow storage, consider lowering vm.swappiness
to 10–20 to prioritize RAM usage, or raising it to 100–180 to use Zram swap more aggressively. Adjust vm.vfs_cache_pressure
upwards (e.g., 100–200) if you need to reclaim RAM from caches more quickly under heavy load.
Disabling Traditional Disk Swap (Optional)
On systems where disk-based swap causes noticeable slowdowns or excessive SSD wear, you may want to disable traditional swap entirely and rely on Zram. First, comment out or remove the swap entry in /etc/fstab
and turn off the swapfile:
sudo swapoff /swapfile
After disabling disk swap, ensure Zram swap is prioritized and sufficient for your typical workload. If you frequently exhaust both RAM and Zram, consider keeping a small disk swap as a fallback to prevent out-of-memory errors.
Choosing the Right Compression Algorithm and Zram Size
Zram supports several compression algorithms, each with different trade-offs between speed and compression ratio. zstd
offers high compression with good speed on most modern CPUs, making it a strong default choice. You can view supported algorithms with:
cat /sys/block/zram0/comp_algorithm
To change the algorithm, update the ALGO
setting in your configuration file as shown above.
When choosing Zram size, keep in mind that the configured size is the maximum uncompressed data Zram can hold. The actual RAM usage will be lower due to compression, but you should not allocate all system memory to Zram. For most systems under 8 GB RAM, allocating 20–50% is effective. Larger allocations may reduce available RAM for active processes and caches, potentially slowing down other tasks.
Comparing Zram and Zswap
Zram and Zswap are both Linux kernel features that compress memory, but their approaches differ. Zram creates a compressed swap device entirely in RAM, reducing or even eliminating the need for disk-based swap. Zswap, on the other hand, acts as a compressed cache for swap pages before they are written to disk, dynamically using RAM as needed and offloading to disk only when the cache is full.
- Zram is best suited for desktops, laptops, and devices where disk swap is slow or undesirable, and where you want to maximize in-memory swap capacity.
- Zswap is often preferred on servers or systems with fast SSDs, where disk swap is still viable but reducing write frequency and swap size is beneficial.
Both methods can be effective, but they should not be enabled together unless you have a specific advanced configuration in mind. For most low-memory systems, Zram provides the most immediate and visible improvement in responsiveness and application switching speed.
Monitoring Zram Usage and Performance
After enabling Zram, monitor its effectiveness with:
zramctl --output-all
This command shows the size of the Zram device, the amount of uncompressed data stored, the compressed size, and the compression ratio achieved. Comparing these numbers helps you understand how much extra data your system can store in RAM before resorting to disk swap. If you find that Zram rarely fills up or that your system never swaps, you may be able to reduce its size and reclaim more RAM for other uses.
For ongoing performance, observe system responsiveness during heavy multitasking. Applications should switch more quickly, and the system should avoid freezing or stuttering when memory is exhausted. If you continue to experience slowdowns, consider fine-tuning your Zram allocation, sysctl settings, or upgrading physical RAM if possible.
Allocating compressed swap in RAM with Zram streamlines Linux performance on low-memory systems, letting you open more applications and browser tabs without slowdowns from disk swapping. Fine-tune your configuration for your hardware and workload to get the most noticeable boost in day-to-day use.
Member discussion