Optimizing Swap with ZRAM on Linux VPS: Turn 2GB Physical RAM into 4GB Virtual RAM Without Slowing Performance
A detailed guide on using ZRAM to optimize swap on Linux VPS, effectively expanding virtual memory from 2GB physical RAM to 4GB or more without reducing system performance. The...
Introduction to ZRAM and Its Benefits on VPS
ZRAM is a Linux kernel feature that creates a compressed virtual storage device in RAM, replacing traditional swap on disk. On VPS systems with limited physical RAM (such as 2GB), using ZRAM to replace conventional swap can effectively expand virtual memory without slowing down data access, since RAM is significantly faster than NVMe or SSD storage.
Optimizing swap with ZRAM allows converting 2GB of physical RAM into virtual memory equivalent to 4GB or more, thanks to high compression ratios (typically 2:1 to 3:1). This is especially useful when running multiple services like Docker, Nginx, or databases on a VPS where virtual memory may be insufficient.
Installing and Configuring ZRAM on Linux
To begin, you need root access on your Linux VPS. The steps below are demonstrated on Debian/Ubuntu systems.
1. Check if ZRAM is Available in the Kernel
zramctl -l
If no output appears, check if the ZRAM module is loaded:
lsmod | grep zram
If the module is not loaded, load it:
modprobe zram
2. Create and Configure a ZRAM Device
We will create a ZRAM device with 50% of physical RAM (1GB) and use the LZO compression algorithm (fast speed, good efficiency):
echo 1G > /sys/block/zram0/disksize
echo lzo > /sys/block/zram0/comp_algorithm
3. Create a File System and Activate Swap
mkswap /dev/zram0
swapon /dev/zram0
4. Configure ZRAM to Start Automatically
To ensure ZRAM loads at boot, add the following line to /etc/fstab:
/dev/zram0 none swap sw 0 0
Testing and Verifying Effectiveness
After configuration, check the ZRAM and swap status:
zramctl -l
swapon --show
To verify compression performance, check the details:
cat /sys/block/zram0/comp_algorithm
zramctl
Also, check total virtual memory using:
free -h
The Swap value will show the expanded capacity thanks to ZRAM. If the expected result is not seen, verify that the system is not using traditional swap and that ZRAM is prioritized.
Alternative Solutions and Notes
On some systems, you may use tools like zram-generator for automatic ZRAM configuration. However, the manual method above allows full control.
ZRAM should not be used as a replacement for physical RAM. It is only recommended to extend virtual memory when needed and should not store sensitive or frequently accessed data due to potential performance impacts from compression.
For VPS systems with limited resources, optimizing swap with ZRAM is an effective solution to run more services without hardware upgrades.
To learn more about high-performance NVMe Cloud VPS for Docker, Nginx, and databases, see NVMe Cloud VPS for Docker, Nginx & Databases.
For larger systems, consider High-Performance Dedicated Servers for Production Clusters.
Performance Comparison: ZRAM vs Traditional Swap
| Criteria | ZRAM | Traditional Swap |
|---|---|---|
| Access Speed | Synchronous with RAM (very fast) | Depends on disk (slower) |
| Compression Efficiency | 2:1 to 3:1 (depends on algorithm) | None |
| CPU Impact | Low (if using LZO) | Negligible |
| Reliability | Depends on physical RAM | Depends on disk |
Performance Testing Procedure
To verify that ZRAM does not reduce system speed, perform the following tests:
- Run
stress --vm 1 --vm-bytes 1G --timeout 30sto test memory usage. - Run
free -hto check virtual memory. - Run
zramctl -lto verify ZRAM status. - Compare response times of services like Nginx or MySQL before and after enabling ZRAM.
Troubleshooting
- If ZRAM is not working, check if the
zrammodule is loaded usinglsmod | grep zram. - If no new swap appears, check
swapon --showand ensure/dev/zram0is activated. - If compression errors occur, verify the compression algorithm with
cat /sys/block/zram0/comp_algorithm. - If performance decreases, try switching the compression algorithm to
lzoorlz4.
References
To learn more about building a private DNS server to block ads system-wide with AdGuard Home on Oracle Free Tier VPS, see Guide to Building a Private DNS Server to Block Ads System-Wide with AdGuard Home on Oracle Free Tier VPS.
For building a personal manga storage and viewing station with Komga or Kavita on VPS, see Guide to Building a Personal Manga Storage and Viewing Station with Komga or Kavita on VPS.
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
