Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

19 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Raspberry Pi ZRAM Optimizer

License: MIT Platform: Raspberry Pi Shell: Bash Architecture: ARM

Production-ready ZRAM optimization for Raspberry Pi devices Reduce SD card wear, improve performance, and optimize memory usage with compressed swap in RAM


๐Ÿ“– Table of Contents


โœจ Features

Feature Description
๐Ÿ”ง One-Command Setup Install and configure ZRAM with a single command โ€” no complex configuration needed
๐Ÿ’พ SD Card Protection Eliminates swap-on-microSD to prevent premature wear and extend device lifespan
๐Ÿš€ Performance Boost Compressed swap in RAM reduces I/O wait and improves system responsiveness
๐ŸŽฏ Raspberry Pi Optimized Specifically tuned for Pi devices with 512MB+ RAM (3B+, 4, 5, Zero 2 W)
๐Ÿ”’ Production Ready Idempotent scripts with clean error handling โ€” safe to run multiple times
โšก Systemd Integration Automatic startup and management via systemd services
๐Ÿงฉ Minimal Dependencies Uses only core Linux utilities โ€” no extra packages required

๐ŸŽฏ What is ZRAM?

ZRAM (formerly compcache) is a Linux kernel feature that creates a compressed block device in RAM. Instead of writing swap data to your SD card (which causes wear and is slow), ZRAM keeps everything in compressed memory.

๐Ÿ’ก Why Use ZRAM?

Problem Solution
SD Card Wear Swap writes to SD cards reduce their lifespan. ZRAM eliminates this.
Slow Swap SD card swap is ~100x slower than RAM. ZRAM compression is ~10x faster.
Memory Pressure When RAM runs low, the system slows down. ZRAM provides extra virtual memory.
System Stability Under heavy load, systems without swap can crash. ZRAM prevents this.

๐Ÿ“ˆ How It Works

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  Raspberry Pi                    โ”‚
โ”‚                                                  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚  Physical   โ”‚     โ”‚    ZRAM Device       โ”‚    โ”‚
โ”‚  โ”‚    RAM      โ”‚     โ”‚  (Compressed Swap)   โ”‚    โ”‚
โ”‚  โ”‚             โ”‚     โ”‚                      โ”‚    โ”‚
โ”‚  โ”‚  1GB Total  โ”‚     โ”‚  ~500MB Compressed   โ”‚    โ”‚
โ”‚  โ”‚             โ”‚     โ”‚  ~1.5GB Effective    โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚         โ”‚                    โ”‚                   โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                   โ”‚
โ”‚                  โ”‚                               โ”‚
โ”‚            โ”Œโ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”                        โ”‚
โ”‚            โ”‚  Kernel   โ”‚                        โ”‚
โ”‚            โ”‚  Memory   โ”‚                        โ”‚
โ”‚            โ”‚ Manager   โ”‚                        โ”‚
โ”‚            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“‹ Requirements

Component Requirement Notes
๐Ÿ’ป Operating System Linux with kernel โ‰ฅ3.14 ZRAM support included in modern kernels
๐Ÿ—๏ธ Architecture ARM (Raspberry Pi) Optimized for ARM devices
๐Ÿง  RAM โ‰ฅ512 MiB 1 GiB or more recommended
๐Ÿ” Permissions Root access Required for ZRAM setup (sudo)
๐Ÿ’พ Storage Any Linux filesystem microSD, USB drive, or NVMe

๐ŸŽฎ Tested Devices

Device RAM Status
Raspberry Pi 3B+ 1 GiB โœ… Fully supported
Raspberry Pi 4 2-8 GiB โœ… Fully supported
Raspberry Pi 5 4-8 GiB โœ… Fully supported
Raspberry Pi Zero 2 W 512 MiB โœ… Fully supported
Raspberry Pi 3B 1 GiB โœ… Fully supported

๐Ÿš€ Quick Start

1๏ธโƒฃ Clone the Repository

git clone https://github.com/4riful/raspberrypi-zram-optimizer.git
cd raspberrypi-zram-optimizer

2๏ธโƒฃ Run the Setup Script

sudo ./scripts/zram-setup.sh start

3๏ธโƒฃ Verify Installation

# Check ZRAM devices
zramctl

# View active swaps
swapon --show

# Memory overview
free -h

4๏ธโƒฃ Enable Auto-start at Boot (Optional)

# Copy script to system path
sudo cp scripts/zram-setup.sh /usr/local/bin/zram-setup.sh
sudo chmod +x /usr/local/bin/zram-setup.sh

# Copy and enable systemd service
sudo cp systemd/zram-setup.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable zram-setup.service

โš™๏ธ Configuration

All settings are optional. The defaults work well for most Raspberry Pi models.

๐Ÿ”ง Environment Variables

Variable Default Description Options
ZRAM_RATIO 50 Percentage of RAM to use as ZRAM 25-100
ZRAM_COMP_ALGO lz4 Compression algorithm lz4, lzo, zstd
ZRAM_PRIORITY 100 Swap priority (higher = preferred) 0-32767
VM_SWAPPINESS 100 Kernel swap aggressiveness 0-100

๐Ÿ’ก Usage Examples

# Default setup (50% of RAM)
sudo ./scripts/zram-setup.sh start

# Use 60% of RAM for ZRAM
sudo ZRAM_RATIO=60 ./scripts/zram-setup.sh start

# Use zstd compression (higher ratio, more CPU)
sudo ZRAM_COMP_ALGO=zstd ./scripts/zram-setup.sh start

# Conservative setup (25% of RAM)
sudo ZRAM_RATIO=25 ./scripts/zram-setup.sh start

# Aggressive setup (75% of RAM)
sudo ZRAM_RATIO=75 ./scripts/zram-setup.sh start

๐ŸŽš๏ธ Compression Algorithms

Algorithm Speed Compression Ratio CPU Usage Best For
lz4 โšก Fastest Good (2:1) Low Default, general use
lzo ๐Ÿš€ Fast Good (2:1) Low Legacy systems
zstd ๐Ÿข Slower Best (3:1) High Maximum compression

๐Ÿ”ง Commands

Command Description Example
start Create and enable ZRAM swap sudo ./scripts/zram-setup.sh start
stop Disable and remove ZRAM swap sudo ./scripts/zram-setup.sh stop
status Show ZRAM and memory status sudo ./scripts/zram-setup.sh status

๐Ÿ“ Command Examples

# Start ZRAM
sudo ./scripts/zram-setup.sh start

# Check status
sudo ./scripts/zram-setup.sh status

# Stop ZRAM (cleanup)
sudo ./scripts/zram-setup.sh stop

๐Ÿ“Š Monitoring

๐Ÿ” Real-time Monitoring

# Watch ZRAM stats live
watch -n1 zramctl

# Monitor memory usage
watch -n1 free -h

# Check swap activity
watch -n1 swapon --show

๐Ÿ“ˆ System Information

# View ZRAM device details
cat /sys/block/zram0/disksize

# Check compression ratio
cat /sys/block/zram0/compr_data_size

# View kernel messages
dmesg | grep -i zram

# Monitor system resources
htop

๐ŸŒก๏ธ Raspberry Pi Specific

# Check CPU temperature
vcgencmd measure_temp

# Monitor CPU frequency
vcgencmd measure_clock arm

# Check throttle status
vcgencmd get_throttled

๐Ÿ› ๏ธ Troubleshooting

โŒ Common Issues

Problem Cause Solution
Permission denied Not running as root Run with sudo
ZRAM module not found Kernel doesn't support ZRAM Check kernel version โ‰ฅ3.14
Swap not activating Script failed silently Run dmesg | grep zram to check errors
High CPU usage Compression algorithm too heavy Switch to lz4 (default)
System slowdown ZRAM too large Reduce ZRAM_RATIO to 25-50%

๐Ÿ” Debug Steps

# 1. Check if ZRAM module is loaded
lsmod | grep zram

# 2. Check kernel messages
dmesg | grep -i zram

# 3. Verify ZRAM device exists
ls -la /dev/zram*

# 4. Check current ZRAM configuration
zramctl

# 5. Review system logs
journalctl -u zram-setup.service

๐Ÿ†˜ Getting Help


๐Ÿ“ Project Structure

raspberrypi-zram-optimizer/
โ”œโ”€โ”€ ๐Ÿ“„ README.md              # This file
โ”œโ”€โ”€ ๐Ÿ“„ LICENSE                # MIT License
โ”œโ”€โ”€ ๐Ÿ“ scripts/
โ”‚   โ””โ”€โ”€ ๐Ÿ”ง zram-setup.sh     # Main ZRAM setup script
โ”œโ”€โ”€ ๐Ÿ“ systemd/
โ”‚   โ””โ”€โ”€ โš™๏ธ zram-setup.service # Systemd service file
โ””โ”€โ”€ ๐Ÿ“ config/
    โ””โ”€โ”€ ๐Ÿ“‹ sysctl.conf        # Kernel parameters (optional)

๐Ÿ“„ File Descriptions

File Purpose
scripts/zram-setup.sh Core script that sets up and manages ZRAM
systemd/zram-setup.service Systemd service for automatic startup
config/sysctl.conf Optional kernel parameter tuning

๐Ÿ”’ Security Considerations

  • ๐Ÿ” Root Access Required: ZRAM setup requires privileged access for system configuration
  • ๐Ÿ›ก๏ธ No Backing Files: Avoid using microSD for ZRAM backing (default disabled)
  • ๐Ÿ“Š Logging: All operations are logged for audit purposes
  • ๐Ÿ”’ System Integration: Service runs at boot with appropriate permissions

๐Ÿค Contributing

We welcome contributions! Here's how you can help:

  1. ๐Ÿ› Report Bugs: Open an issue with detailed reproduction steps
  2. ๐Ÿ’ก Suggest Features: Share your ideas for improvements
  3. ๐Ÿ“ Improve Documentation: Help make our docs clearer
  4. ๐Ÿ”ง Submit Code: Fix bugs or add new features
  5. ๐Ÿงช Test: Try on different hardware and report results

๐Ÿ“‹ Development Setup

# Clone the repository
git clone https://github.com/4riful/raspberrypi-zram-optimizer.git
cd raspberrypi-zram-optimizer

# Create a new branch
git checkout -b feature/your-feature

# Make changes and test
# ...

# Commit and push
git commit -m "feat: add your feature"
git push origin feature/your-feature

๐Ÿ“„ License

This project is licensed under the MIT License โ€” see the LICENSE file for details.

MIT License

Copyright (c) 2026 4riful

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software...

๐Ÿ™ Acknowledgments

  • ๐Ÿง Linux Kernel Team: For ZRAM implementation
  • ๐Ÿ“ Raspberry Pi Foundation: For the amazing hardware platform
  • ๐ŸŒ Open Source Community: For inspiration and feedback
  • ๐Ÿ‘ฅ Contributors: Thanks to all contributors who helped improve this project

๐Ÿ“ž Support

Channel Link
๐Ÿ› Bug Reports GitHub Issues
๐Ÿ’ฌ Discussions GitHub Discussions
๐Ÿ“š Documentation Project Wiki
โญ Star the Repo GitHub Stars

Made with โค๏ธ for the Raspberry Pi community

Raspberry Pi ZRAM Optimizer

If this project helped you, please give it a โญ star on GitHub!

About

Optimize your Raspberry Pi's memory management using ZRAM for better performance and stability under memory pressure.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages