Stop your Linux system from lagging when RAM fills up. This repo enables compressed in-RAM swap using zram — the same memory compression approach Apple uses in macOS. One-command install, no reboot required.
When your Linux system runs out of RAM, it starts swapping to disk. Even on an SSD, disk swap is orders of magnitude slower than RAM access. The result:
- System freezes and stuttering
- Applications become unresponsive
- Multi-tasking grinds to a halt
- The dreaded "waiting for swap" lag
If you have 8–16 GB of RAM and run a browser with multiple tabs, an IDE, Docker, or any memory-hungry workflow, you've felt this pain.
Zram creates a compressed swap device entirely in RAM. Instead of writing memory pages to your slow disk, zram compresses them and keeps them in memory. With the zstd algorithm, compression ratios of 2:1 to 4:1 are typical — so 8 GB of RAM can effectively hold 16–32 GB of data.
This is the same core concept Apple uses in macOS — and it's been available in the Linux kernel since 2014.
Apple introduced memory compression in macOS Mavericks (2013). Here's how it works:
- When RAM fills up, macOS compresses inactive pages in memory instead of swapping them to disk
- Apple uses the WKdm compression algorithm, optimized for memory pages
- This is a key reason Macs with 8 GB RAM feel fast — the OS effectively has much more usable memory
- Apple's implementation runs on a dedicated compression thread, making it nearly transparent
Linux has had the same capability via zram since kernel 3.14 (2014). The difference is that Linux doesn't enable it by default on most distros. This repo fixes that.
Sources:
- Apple – OS X Mavericks Core Technologies Overview
- AppleInsider – Memory Compression in OS X
- Greg Gallant – Deep Dive into macOS Memory Compression
Without zram (default): With zram (this repo):
┌──────────────┐ ┌──────────────┐
│ RAM │ │ RAM │
│ (16 GB) │ │ (16 GB) │
│ │ │ │
│ [App Data] │ │ [App Data] │
│ [App Data] │ │ [App Data] │
│ [App Data] │ │ [Compressed] │◄── zram swap
│ │ │ [Compressed] │ (zstd, ~3:1)
└──────┬───────┘ └──────┬───────┘
│ RAM full → swap to disk │ RAM full → compress in RAM
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Disk Swap │ │ Disk Swap │
│ (slow! 🐌) │ │ (rarely hit) │
│ ~500 MB/s │ │ (fallback) │
└──────────────┘ └──────────────┘
│ │
Lag & freezes Smooth & responsive
git clone https://github.com/karem505/linux-zram-optimization.git
cd linux-zram-optimization
sudo bash install.shThat's it. No reboot required. Your system is now using compressed RAM swap.
If you prefer to set things up yourself:
# Ubuntu / Debian
sudo apt install systemd-zram-generator
# Fedora
sudo dnf install zram-generator
# Arch
sudo pacman -S zram-generatorCreate /etc/systemd/zram-generator.conf:
[zram0]
zram-size = ram / 2
compression-algorithm = zstd
swap-priority = 100Create /etc/sysctl.d/99-swappiness.conf:
vm.swappiness=10
Apply immediately:
sudo sysctl -p /etc/sysctl.d/99-swappiness.confCreate /etc/modules-load.d/20-zram-generator.conf:
#Load zram.ko at boot
zram
sudo modprobe zram
sudo systemctl daemon-reload
sudo systemctl restart systemd-zram-setup@zram0.service| Setting | Value | Why |
|---|---|---|
zram-size = ram / 2 |
50% of physical RAM | Balances compression space vs. available RAM. With ~3:1 compression, 8 GB RAM effectively gets ~12 GB usable. |
compression-algorithm = zstd |
Zstandard | Best balance of compression ratio and speed. Faster than gzip, better ratios than lzo. |
swap-priority = 100 |
Highest priority | Ensures zram is always used before any disk swap. |
vm.swappiness = 10 |
Low swappiness | Tells the kernel to prefer keeping data in RAM and only swap when necessary. Default is 60. |
| Algorithm | Compression Ratio | Speed | CPU Usage |
|---|---|---|---|
| lzo | ~2:1 | Very fast | Very low |
| lz4 | ~2.1:1 | Very fast | Very low |
| zstd | ~2.8:1 | Fast | Low |
| gzip | ~3.2:1 | Slow | High |
zstd gives the best ratio without noticeable CPU overhead on modern hardware.
After installing, verify everything is working:
# Check active swap devices (should show /dev/zram0)
swapon --show
# Expected output:
# NAME TYPE SIZE USED PRIO
# /dev/zram0 partition 7.6G 0B 100
# Check zram device details
cat /sys/block/zram0/comp_algorithm
# Output: lzo lzo-rle lz4 lz4hc [zstd] deflate 842
zramctl
# Shows zram device with algorithm, disk size, and compression stats
# Check swappiness
sysctl vm.swappiness
# Output: vm.swappiness = 10On a system with 15 GB RAM running Ubuntu:
Before (disk swap only):
- System stutters when opening 15+ browser tabs + IDE
swapon --showshows slow disk swap being hammered- Noticeable input lag during memory pressure
After (zram enabled):
- Smooth multitasking with 20+ browser tabs + IDE + Docker
- Compressed swap stays in RAM, disk swap rarely touched
- No perceptible lag during memory pressure
To completely revert to default swap behavior:
sudo bash uninstall.shOr manually:
sudo swapoff /dev/zram0
sudo rm /etc/systemd/zram-generator.conf
sudo rm /etc/sysctl.d/99-swappiness.conf
sudo rm /etc/modules-load.d/20-zram-generator.conf
sudo sysctl -w vm.swappiness=60
sudo systemctl daemon-reloadThis setup works on any systemd-based Linux distribution:
| Distro | Package | Status |
|---|---|---|
| Ubuntu 22.04+ | systemd-zram-generator |
Tested |
| Ubuntu 24.04 | systemd-zram-generator |
Tested |
| Debian 12+ | systemd-zram-generator |
Compatible |
| Fedora 38+ | zram-generator |
Compatible |
| Arch Linux | zram-generator |
Compatible |
| openSUSE | zram-generator |
Compatible |
Requirements:
- Linux kernel 3.14+ (for zram with zstd support, kernel 4.18+)
- systemd 248+ (for zram-generator)
Q: Will this use more CPU? A: Negligibly. Zstd compression/decompression is extremely fast on modern CPUs. The CPU cost of compressing a memory page is far less than the time saved by not doing disk I/O.
Q: Can I use this alongside an existing swap file/partition?
A: Yes. Set zram's swap-priority higher (e.g., 100) than your disk swap, and the kernel will prefer zram. Your disk swap becomes a fallback.
Q: How much RAM should I allocate to zram?
A: 50% of RAM (ram / 2) is a good default. With ~3:1 compression, that gives you an additional ~1.5x effective memory. You can increase to ram for more aggressive compression.
Q: Is this safe? Can it cause data loss? A: Zram is swap — it only holds data that would otherwise go to disk swap. If your system loses power, swap data is lost either way (disk or zram). Your files on disk are unaffected.
Q: Why not just add more RAM? A: You should, if you can. Zram is for getting the most out of the RAM you have. It's especially useful on laptops, VMs, and cloud instances where RAM is fixed or expensive.
Q: Does this work on WSL2? A: WSL2 uses a Linux kernel, so zram works. However, WSL2 memory management is controlled by Windows, so results may vary.
| Feature | macOS | Linux + zram |
|---|---|---|
| Compression algorithm | WKdm | zstd (configurable) |
| Enabled by default | Yes | No (this repo enables it) |
| Compression location | RAM | RAM |
| Typical compression ratio | ~2:1 | ~2.8:1 (zstd) |
| Disk swap fallback | Yes | Yes |
| Configuration | None needed | Minimal (3 files) |
Found an issue or have a suggestion? Open an issue or submit a pull request.
Keywords: linux memory compression, zram swap, ubuntu zram setup, linux ram optimization, reduce linux lag, apple memory compression linux, compressed swap linux, zstd zram, systemd zram generator, linux swap optimization, ubuntu performance tuning, linux memory management