Skip to content

karem505/linux-zram-optimization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux Zram Optimization — Apple-Style Memory Compression for Linux

Ubuntu Debian Fedora Arch License: MIT

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.


The Problem

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.

The Solution

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 Uses This Exact Approach

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:

How It Works

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

Quick Install

git clone https://github.com/karem505/linux-zram-optimization.git
cd linux-zram-optimization
sudo bash install.sh

That's it. No reboot required. Your system is now using compressed RAM swap.

Manual Setup

If you prefer to set things up yourself:

1. Install systemd-zram-generator

# Ubuntu / Debian
sudo apt install systemd-zram-generator

# Fedora
sudo dnf install zram-generator

# Arch
sudo pacman -S zram-generator

2. Configure the zram device

Create /etc/systemd/zram-generator.conf:

[zram0]
zram-size = ram / 2
compression-algorithm = zstd
swap-priority = 100

3. Lower swappiness

Create /etc/sysctl.d/99-swappiness.conf:

vm.swappiness=10

Apply immediately:

sudo sysctl -p /etc/sysctl.d/99-swappiness.conf

4. Ensure zram module loads at boot

Create /etc/modules-load.d/20-zram-generator.conf:

#Load zram.ko at boot
zram

5. Activate

sudo modprobe zram
sudo systemctl daemon-reload
sudo systemctl restart systemd-zram-setup@zram0.service

Configuration Details

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.

Why zstd?

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.

Verification

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 = 10

Results

On a system with 15 GB RAM running Ubuntu:

Before (disk swap only):

  • System stutters when opening 15+ browser tabs + IDE
  • swapon --show shows 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

Uninstall

To completely revert to default swap behavior:

sudo bash uninstall.sh

Or 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-reload

Compatibility

This 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)

FAQ

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.

How This Compares to macOS

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)

Contributing

Found an issue or have a suggestion? Open an issue or submit a pull request.

License

MIT


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

About

Apple-style in-RAM memory compression for Linux using zram + zstd. One-command installer for Ubuntu, Debian, Fedora, and Arch.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages