What is ZRAM and Why Do You Need It?
ZRAM (formerly known as compcache) is a Linux kernel technology that creates a compressed block in RAM and uses it as a swap device. Instead of writing data to a slow hard disk or SSD, the system compresses unused memory pages and stores them in RAM. This is particularly useful for:
- Old hardware with small amounts of RAM (2-4 GB).
- Diskless systems (e.g., live systems, embedded devices).
- Virtual machines with limited resources.
- Servers where you need to minimize disk I/O operations.
ZRAM does not completely replace disk swap in hibernation scenarios, but it can become the primary or sole swap location for most workloads, significantly speeding up system responsiveness.
Checking Current Configuration
Before setting up, ensure ZRAM isn't already active in your system and check current swap devices:
swapon --show
If the output is empty or only contains /dev/sdX (disk), you can proceed. Also, check if the kernel module is loaded:
lsmod | grep zram
If the module is not loaded, you can load it manually (sudo modprobe zram), but with proper systemd configuration, this will happen automatically.
Method 1: Configuration via zram-tools (Recommended for Debian/Ubuntu)
The zram-tools package provides a ready-made script and systemd service for managing ZRAM. This is the simplest path.
Installation
For Debian, Ubuntu, and derivatives:
sudo apt update
sudo apt install zram-tools
Configuration
The main configuration file is /etc/default/zramswap. Open it in a text editor:
sudo nano /etc/default/zramswap
Find and modify the parameters:
PERCENT=50— percentage of total RAM that ZRAM will occupy. For 4 GB of RAM, this is 2 GB. You can also specify an absolute size:DEV_SIZE=2G.ALGO=lzo(orzstd) — compression algorithm.zstdprovides better compression but requires more CPU.lzois faster.SWAP_RATING=100— swap device priority (higher means more actively used).
Example configuration for 2 GB of ZRAM with the zstd algorithm:
# /etc/default/zramswap
PERCENT=0
DEV_SIZE=2G
ALGO=zstd
SWAP_RATING=100
Activation
After editing the file, restart the service:
sudo systemctl restart zramswap
Or enable it for autostart and run it immediately:
sudo systemctl enable --now zramswap
Check the result:
free -h
In the Swap line, you should see a size corresponding to DEV_SIZE.
Method 2: Manual Configuration via systemd (Universal)
This method requires no additional packages and works on any modern Linux with systemd.
Creating a systemd Unit
Create the unit file:
sudo nano /etc/systemd/system/zram-setup.service
Insert the following content (adjust MemoryMax and CompressionAlgorithm as needed):
[Unit]
Description=Setup ZRAM
After=local-fs.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStartPre=/sbin/modprobe zram num_devices=1
ExecStart=/bin/sh -c 'echo zstd > /sys/block/zram0/comp_algorithm'
ExecStart=/bin/sh -c 'echo 2G > /sys/block/zram0/disksize'
ExecStart=/sbin/mkswap /dev/zram0
ExecStart=/sbin/swapon /dev/zram0 -p 100
[Install]
WantedBy=multi-user.target
num_devices=1— creates onezram0device.echo zstd > ...— sets the compression algorithm (lzo,lz4,zstd).echo 2G > ...— device size (can be in MB or GB).swapon -p 100— sets a high priority.
Activation
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now zram-setup.service
Check the status:
sudo systemctl status zram-setup.service
Kernel Parameter Tuning
For more efficient ZRAM operation, you can tune virtual memory parameters in /etc/sysctl.conf:
sudo nano /etc/sysctl.conf
Add the following lines:
# Increase system tendency to use swap (including ZRAM)
vm.swappiness = 100
# Reduce inode/dentry cache pressure (can help during RAM shortage)
vm.vfs_cache_pressure = 50
# Optimization for ZRAM (if using compression)
vm.page-cluster = 3
Apply settings without rebooting:
sudo sysctl -p
Method 3: Using zram-generator (for Fedora, RHEL 8+, Modern Distributions)
Some distributions ship zram-generator — a tool that generates systemd units based on a config.
Installation
On Fedora/CentOS/RHEL 8+:
sudo dnf install zram-generator
Configuration
Edit /etc/systemd/zram-generator.conf:
# /etc/systemd/zram-generator.conf
[zram0]
zram-fraction = 0.5
compression-algorithm = zstd
swap-priority = 100
zram-fraction = 0.5— 50% of RAM.- You can use
max-zram-size = 2Ginstead of a fraction.
Reboot the system or run:
sudo systemctl start /dev/zram0
Monitoring and Verification
After setup, verify that ZRAM is active and in use:
# Show all swap devices
swapon --show
# Show memory usage in a readable format
free -h
# Detailed statistics for zram0 (values in bytes)
cat /sys/block/zram0/mm_stat
Fields in mm_stat (can be converted to human-readable via awk):
orig_data_size— original data size before compression.compr_data_size— size after compression.mem_used_total— total ZRAM memory used (including metadata).same_pages— number of pages that did not compress (already compressed or zero pages).
Example compression ratio calculation:
orig=$(cat /sys/block/zram0/mm_stat | awk '{print $1}')
compr=$(cat /sys/block/zram0/mm_stat | awk '{print $2}')
echo "scale=2; $orig / $compr" | bc
A ratio greater than 1 indicates compression is working.
Common Issues and Solutions
ZRAM does not activate after reboot
- Cause: The service is not enabled or the configuration file contains errors.
- Solution: Check
sudo systemctl status zramswap(or your unit). View logs:sudo journalctl -u zram-setup.service. Ensure the config specifies a valid size.
System "freezes" during intensive memory usage
- Cause: ZRAM size is too small or the compression algorithm is suboptimal. Under memory pressure, the system actively compresses/swaps, loading the CPU.
- Solution: Increase ZRAM size (e.g., from 1 GB to 2 GB for 4 GB RAM). Try the
lzoalgorithm instead ofzstdif the CPU is weak.
ZRAM does not appear in free
- Cause: The device is not initialized or not formatted as swap.
- Solution: Manually run the commands from Method 2, especially
mkswapandswapon. Ensure thezrammodule is loaded:lsmod | grep zram.
Conflict with another swap device
- Cause: By default, the system uses the swap device with the highest priority. If you have both disk and ZRAM, ZRAM with priority 100 will be used first.
- Solution: This is normal behavior. If you want to use only ZRAM, disable disk swap:
sudo swapoff /dev/sdXand comment out the corresponding line in/etc/fstab.
Conclusion: What ZRAM Gives You
Configuring ZRAM is one of the most effective ways to increase system responsiveness without upgrading hardware. This is especially noticeable on laptops and PCs with 2-4 GB of RAM, where regular disk swap is insufficient and disk operations slow everything down.
After setup, you will get:
- Faster application switching.
- Fewer "stutters" when opening multiple browser tabs.
- Reduced load on SSD/HDD (fewer swap writes).
- Extended lifespan for older devices.
Remember, ZRAM is compressible RAM, not a replacement for disk space. It is not suitable for data storage, but as a buffer for inactive memory, it is ideal.