ZRAM and how I deal with the memory usage in my Linux system


Introduction

I started using Linux in 2018 on a laptop with 4GB of RAM and a dual-core CPU.

At first, I was happy with how my system performed. But over time, I noticed it becoming slower, especially as memory usage increased. Occasionally, the system would freeze for a few seconds, and I had to wait for it to respond again.

I thought enabling swap might help improve performance. At the time, I didn’t know about ZRAM, so I simply enabled traditional disk-based swap.

Thanks to the SSD, swap access was relatively fast—but still, the system didn’t feel as responsive as before, especially when I had multiple applications open.

For a long time, I believed swap was the only solution to extend memory. Then I discovered ZRAM, and I began learning how it works and how to make use of it.

What is ZRAM?

ZRAM is a Linux kernel module that creates compressed, RAM-based block devices. These virtual devices can be used either as swap space or as general-purpose RAM disks.

Yes, you read that right—swap space stored in RAM instead of on disk.

At this point, you might be thinking: “Isn’t swap supposed to expand available memory? Why would we use our already limited RAM for it?”

That’s where ZRAM’s magic lies. By compressing data before storing it in RAM, ZRAM allows you to store more data in the same amount of memory. This makes your system feel like it has more RAM—without touching your disk.

How does ZRAM swap work?

Unlike traditional swap, which stores data on disk, ZRAM compresses and stores swap data directly in RAM.

This means you can fit more data into your available memory. For example, with 4GB of physical RAM, ZRAM might effectively give you the equivalent of 6GB—depending on the compressibility of your data.

Since RAM is significantly faster than any disk, this results in better system responsiveness, especially under memory pressure.

Of course, compression and decompression use CPU resources. On low-end CPUs, this overhead might cause a performance hit. But on modern processors, it’s usually negligible—and the benefit of having “more RAM” tends to outweigh the cost.

ZRAM supports several compression algorithms, such as LZO, LZ4, ZSTD, and others. Personally, I prefer ZSTD because it strikes a good balance between speed and compression ratio. It’s also the default in openSUSE Tumbleweed.

Enabling ZRAM

For openSUSE Tumbleweed, enable ZRAM is easy. Just run the following command:

sudo zypper install systemd-zram-service && sudo zramswapon

For Debian/Ubuntu, you can install the zram-config package:

sudo apt install zram-config
sudo systemctl enable zram-config
sudo systemctl start zram-config

Because of ZRAM swap (data compression), data should be pushed into ZRAM as soon as possible. Swappiness should be 180.

sudo sysctl vm.swappiness=180

With zstd, vm.page-cluster should be 0 because of decompression.

sudo sysctl vm.page-cluster=0

Conclusion

I’ve been using ZRAM for a while now, and I can confidently say it has significantly improved my system’s performance. At the very least, it allows me to open dozens of browser tabs and run multiple applications without noticeable slowdowns.

That said, ZRAM is not a replacement for physical RAM. Instead, it’s a smart way to make better use of the RAM you already have—especially under memory pressure.

If your system has plenty of RAM, ZRAM might not be necessary. In fact, the CPU overhead for compression and decompression could slightly degrade performance in such cases.

But if you’re working with a low-end system or limited RAM, ZRAM can be a game-changer. It provides a noticeable performance boost with minimal setup.

Thanks for reading! I hope you found this post helpful.

If you have any questions or suggestions, feel free to leave a comment below.

Next time, we’ll look at how some popular operating systems handle memory management and swap strategies.

References:

  • https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html
  • https://dev.to/archerallstars/lets-fine-tune-your-zram-aka-free-ram-in-opensuse-with-zstandard-aka-zstd-4eap



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Generate password with command line in Linux or macOS
  • Hub and Spoke VPN, how it solve my working problem
  • P2P - UDP Hole Punching
  • Mình đã chuyển sang Fcitx5 thay vì IBus Bamboo và IBus Unikey