Back to articles
DevOps & Systems

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...

DPTCloud Engineering5 min read
Optimizing Swap with ZRAM on Linux VPS: Turn 2GB Physical RAM into 4GB Virtual RAM Without Slowing Performance

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:

  1. Run stress --vm 1 --vm-bytes 1G --timeout 30s to test memory usage.
  2. Run free -h to check virtual memory.
  3. Run zramctl -l to verify ZRAM status.
  4. Compare response times of services like Nginx or MySQL before and after enabling ZRAM.

Troubleshooting

  • If ZRAM is not working, check if the zram module is loaded using lsmod | grep zram.
  • If no new swap appears, check swapon --show and ensure /dev/zram0 is 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 lzo or lz4.

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.

DPTCloud Tier III Infrastructure

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.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478