ZRAM Concept and Principles

ZRAM (formerly known as compcache) is a memory compression technology provided by the Linux kernel that creates a virtual block device by compressing a portion of RAM. This technology was first developed by Nitin Gupta in 2009 and officially integrated into the Linux kernel from version 3.14. The core idea of ZRAM is to utilize compressed RAM as swap space instead of disk-based swap, reducing memory usage without the overhead of disk I/O operations.

The ZRAM device compresses data in real-time before storing it in memory and decompresses it when accessing the data. Although this process consumes some CPU resources, it avoids disk I/O, contributing to overall system performance improvement. ZRAM typically achieves compression ratios of 2:1 to 4:1 using high-speed compression algorithms such as LZO, LZ4, and ZSTD.

Key Advantages of ZRAM

Memory Capacity Expansion

The most significant advantage of ZRAM is the ability to effectively increase available memory without physical RAM expansion. In environments with a lot of text data, it can achieve compression ratios of up to 4:1, with an average of about 2:1. This means that on an 8GB RAM system, you can effectively experience the equivalent of 12-16GB of memory.

Improved System Responsiveness

Disk-based swap is slow to access, significantly degrading system responsiveness. ZRAM, on the other hand, operates within RAM, allowing data access hundreds of times faster than disk swap. This greatly helps maintain system responsiveness even in low-memory situations. It is particularly effective in low-specification systems or environments with limited memory.

Extended Disk Lifespan

Storage devices like SSDs have limited write cycles, and excessive swap usage can shorten disk lifespan. ZRAM reduces disk-based swap usage, effectively extending the life of storage devices. This benefit is particularly important in embedded systems or when using budget SSDs.

Power Efficiency

For mobile devices or laptops, increased disk I/O leads to higher power consumption. ZRAM helps extend battery life by reducing disk access. Although it uses CPU for compression/decompression, the benefits from reduced disk I/O operations result in overall improved power efficiency.

Easy Installation and Usage in Ubuntu 24.04

In Ubuntu 24.04, installing and using ZRAM is very straightforward. With just a single command, it can be automatically configured and immediately used:

1
sudo apt install zram-config

By installing the zram-config package with this command, the system automatically sets up and activates ZRAM. It is applied immediately without requiring a reboot or additional configuration.

Default Configuration

When you install the zram-config package in Ubuntu 24.04, the following default settings are automatically applied:

  • ZRAM size is set to approximately 50% of RAM
  • The efficient LZ4 compression algorithm is used
  • Swap priority is set to 100 (prioritized over disk swap)
  • Automatically activated at system boot

These default settings are suitable for most users and provide immediate performance improvements without any additional adjustments.

Verifying Installation

To check if ZRAM is working properly after installation, you can use the following commands:

1
zramctl

Or

1
swapon --show

If /dev/zram0 appears in the output, ZRAM is properly installed and activated.

Considerations When Using ZRAM

  • In most common usage environments, the default settings provide sufficient performance improvements.
  • Users of very memory-intensive applications will experience greater performance improvements.
  • If hibernation (sleep mode) is needed, consider using zswap instead of ZRAM.
  • On systems with abundant memory (32GB or more), the effect of ZRAM may be limited.
  • In environments where CPU load is already high, additional load may occur during compression/decompression processes.

Conclusion

In Ubuntu 24.04, ZRAM is a powerful memory optimization technology that can be utilized immediately with just a single installation command. It can significantly improve performance, especially on systems with limited memory, and offers advantages in terms of disk lifespan extension and power efficiency. The default settings provide sufficient benefits for most users, making it a very useful tool for general users.

If you are experiencing memory shortage issues or want to improve system responsiveness in Ubuntu 24.04, you can experience immediate improvement effects just by installing the zram-config package.