PROWAREtech








How to Enable RAM Compression on Linux
What's ZRAM
zram is a Linux kernel feature that creates a compressed block device in RAM, which is typically used as a swap device. Rather than swapping to a traditional hard disk or SSD, zram compresses data and stores it in memory, enabling faster access speeds while saving physical RAM space.
How zram Works
- Compression in RAM: When the system needs more memory than is available, it swaps out inactive pages to a zramdevice. The data is compressed before being stored in this device, reducing the amount of physical memory required.
- Compressed Swap Space: zramdevices are usually set up as swap devices, meaning that they store compressed pages of memory that would otherwise be swapped to disk. However, you can also configurezramas a general-purpose compressed storage device if needed.
Benefits of zram
- Performance: Accessing compressed data in memory is typically faster than accessing data on a traditional swap partition or swap file on an SSD or HDD.
- Memory Efficiency: By compressing pages in RAM, zramcan effectively "expand" memory, since compressed pages require less physical memory than their uncompressed counterparts.
- Reduced Wear on SSDs: By keeping swap in memory rather than on disk, zramreduces the amount of wear on SSDs, which have a limited number of write cycles.
Common Use Cases for zram
- Low-Memory Systems: zramis popular on devices with limited RAM, such as embedded systems, IoT devices, and low-cost laptops.
- Desktop Systems: Many Linux distributions configure zramby default to improve performance and reduce the need for disk-based swap.
- Containers and Virtual Machines: zramis also beneficial in virtualized environments where RAM is often limited.
Trade-Offs
- CPU Usage: Because zramcompresses and decompresses data, it requires CPU resources. This may slightly impact CPU performance, although on modern systems the impact is generally minimal.
- Memory Overhead: zramitself uses some memory for its operation, although this is usually less than the memory savings gained from compression.
Overall, zram provides a practical way to improve memory efficiency, particularly on Linux systems with constrained resources.
Installing and Enabling ZRAM
1. From the terminal, type this:
sudo apt update
sudo apt install zram-tools
2. Edit the configuration file:
sudo nano /etc/default/zramswap
3. Make sure these lines are present without being commented out (via a hashtag in front of them):
ENABLED=true
ALGO=zstd
PERCENTAGE=50
PRIORITY=100
The zstd algorithm offers the greatest compression. The PERCENTAGE lines tells the percentage of RAM to be used by zram.
4. Enable zram with these two commands:
sudo systemctl enable zramswap
sudo systemctl start zramswap
5. Check the zram status:
sudo zramctl