PROWAREtech

articles » current » information-technology » linux » setup-and-configure » enable-ram-compression-on-ubuntu

How to Enable RAM Compression on Linux

How to install and configure memory compression, a.k.a. ZRAM, on Ubuntu 24.04.

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 zram device. The data is compressed before being stored in this device, reducing the amount of physical memory required.
  • Compressed Swap Space: zram devices 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 configure zram as a general-purpose compressed storage device if needed.

Benefits of zram

  1. 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.
  2. Memory Efficiency: By compressing pages in RAM, zram can effectively "expand" memory, since compressed pages require less physical memory than their uncompressed counterparts.
  3. Reduced Wear on SSDs: By keeping swap in memory rather than on disk, zram reduces the amount of wear on SSDs, which have a limited number of write cycles.

Common Use Cases for zram

  • Low-Memory Systems: zram is popular on devices with limited RAM, such as embedded systems, IoT devices, and low-cost laptops.
  • Desktop Systems: Many Linux distributions configure zram by default to improve performance and reduce the need for disk-based swap.
  • Containers and Virtual Machines: zram is also beneficial in virtualized environments where RAM is often limited.

Trade-Offs

  • CPU Usage: Because zram compresses and decompresses data, it requires CPU resources. This may slightly impact CPU performance, although on modern systems the impact is generally minimal.
  • Memory Overhead: zram itself 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

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT