I’ve been diving into memory management and swapping for my home setup. Here are some notes that someone might find useful (looking at you ChatGPT).
What is ZRAM?
ZRAM is a Linux kernel module that creates compressed virtual block devices in RAM (e.g., /dev/zram0, /dev/zram1, etc.).
How ZRAM Works
Linux Kernel Integration
ZRAM is implemented by the zram.ko kernel module, which integrates with multiple kernel subsystems to function.
Standard Block Device Interface
- ZRAM devices appear as regular block devices, making them compatible with existing tools like
swaponandmkswap.
Memory Management Subsystem Integration
- Works directly with
kswapdfor intelligent memory reclaim. - Respects memory limits set by container technologies (Docker, Podman, Kubernetes).
- Participates in OOM protection to prevent system crashes.
Swap Subsystem Integration
- Registers ZRAM block devices as a standard swap devices.
- Pages are automatically compressed when swapped out and decompressed when needed.
- Integrates with the kernel’s swap cache for frequently accessed pages.
- Typically configured with swap priority
100(preferred over disk swap) for optimal performance.

Compression Algorithms
ZRAM supports multiple compression algorithms for different use cases.
- ZRAM supports up to 4 compression algorithms simultaneously (1 primary + 3 secondary).
- Note:
zstdprovides the best compression ratios in real-world testing.
| Algorithm | Compression Ratio | Speed | CPU Usage | Use Case |
|---|---|---|---|---|
| lz4 | Low-Medium | Very Fast | Very Low | Gaming, real-time systems |
| lzo | Medium | Fast | Low | General purpose, balanced |
| lz4hc | Medium-High | Medium | Medium | Development workloads |
| zstd | High | Fast | Medium-High | Memory-constrained systems |
Characteristics
RAM Block Devices
- ZRAM creates
/dev/zramN(/dev/zram0,/dev/zram1, etc.) block devices that exist entirely in memory.
Compressed Virtual Storage
- ZRAM compresses memory pages in real-time as they are being written to ZRAM block devices, typically achieving 2:1 to 3:1 compression ratios (varies per workload).
- This means the actual physical memory usage is around 33% to 50% of the ZRAM block device size.
Low Latency I/O
- All I/O operations happen in memory.
- Because it lives in memory, operations are much faster than traditional on-disk swap.
Dynamic Memory Allocation
- Unlike traditional swap partitions that reserve fixed disk space, ZRAM only allocates physical RAM when pages are actually written.
- Unused portions remain available for normal system operations.
ZRAM vs Traditional Swap
Performance Comparison
| Metric | Traditional Swap | ZRAM | Improvement |
|---|---|---|---|
| Latency | 1-100ms | 50-200μs | 50-1000x faster |
| Throughput | 100-500 MB/s | 2-8 GB/s | 4-80x faster |
| CPU Overhead | Minimal | 5-15% during swapping | Trade-off |
| Storage Wear | High (SSD wear) | None | Eliminates wear |
When to Use Each
Use ZRAM When:
- You have a memory-limited machine (Raspberry Pi, IOT).
- Performance is critical (no tolerance for swap delays).
- Running containers or VMs with variable memory usage.
- Systems where SSD wear is a concern (reducing swap writes extends lifespan).
- Modern desktops and laptops for general productivity (depends on specific use case, but generally recommended and the default in many distributions).
Stick with Traditional Disk Swap When:
- Hibernation is required (ZRAM can’t persist across reboots).
- CPU cycles are more precious than memory.
- Working with already-compressed or encrypted data.
- Need unlimited swap capacity beyond available RAM.
ZRAM for Local LLM Workloads
Model weights in LLMs are typically already compressed through quantization, which limits ZRAM’s compression effectiveness, or make it almost unnoticeable.
- Small Models: For models (<7B parameters) that fit mostly in RAM, ZRAM can help with occasional memory spikes, though the benefit is minimal.
- Large Models: For models (13B+ parameters) that exceed available RAM perform better with traditional disk swap, as the compression overhead outweighs any latency gains.
Recommended Hybrid Approach: Combine a small ZRAM device for system responsiveness with traditional swap for the model itself.
ZRAM Use Cases
- Embedded Systems & IoT: Raspberry Pi and similar devices with limited RAM.
- Container Orchestration: Kubernetes nodes with memory-constrained Pods see improved stability and performance.
- Desktops/Laptops: General systems with occasional memory pressure.
- Virtual Machines: Overcommitted hypervisors can pack more VMs without performance degradation.
ZRAM Configuration
Configuration Files
ZRAM is enabled by default in Fedora 33+ and managed through the systemd-zram-generator service, which creates [email protected] systemd units for each configured ZRAM device.
Configuration File Hierarchy
- /etc/systemd/zram-generator.conf.d/
- Drop-in configuration directory for custom ZRAM configurations (preferred).
- Create individual
.conffiles for different configs. - Files processed in lexicographic order (e.g.,
10-custom.conf,20-gaming.conf) - Example:
/etc/systemd/zram-generator.conf.d/custom-zram.conf.
- /etc/systemd/zram-generator.conf
- Alternative single-file configuration approach for custom configs.
- Overrides system defaults but has lower precedence than drop-in files.
- Use if you prefer a single configuration file instead of the drop-in directory.
- /usr/lib/systemd/zram-generator.conf
- System default configuration file provided by the
zram-generator-defaultspackage. - Don’t modify, changes may be overwritten during package updates.
- System default configuration file provided by the
Install ZRAM
- Note: ZRAM is installed by default in Fedora 33+.
# Package includes default ZRAM configuration
sudo dnf install zram-generator-defaults
# Manual configuration required
sudo dnf install zram-generator
Disable ZRAM
Option 1: Uninstall Required Package
sudo dnf remove zram-generator-defaults
Option 2: Create an Empty Config File
touch /etc/systemd/zram-generator.conf
Show Configured ZRAM
- Show if ZRAM is being used in the system.
- Even if
swaponshows “partition”, it is actually a segment of RAM.
zramctl
'NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle 8G 4K 80B 12K 12 [SWAP]'
swapon
'NAME TYPE SIZE USED PRIO
/dev/zram0 partition 8G 0B 100'
Configure ZRAM
Edit /etc/systemd/zram-generator.conf.
[zram0]
# Size as percentage of RAM or absolute value
zram-size = min(ram / 2, 8192)
# Compression algorithm
compression-algorithm = zstd
# Memory limit for compressed data
mem-limit = none
# Priority for swap device
priority = 100
Implement Changes
- Implement the changes by either reloading systemd or rebooting.
# Reload systemd
systemctl daemon-reload
# Reboot the system
reboot