Linux:Using ZRam

I recently discovered zram in Linux, and it felt like a mystical, futuristic experience. Upon discovering it and learning how to use it, I immediately went looking for uses.

What is Zram?

Zram is a feature of the Linux kernel (modprobe zram) that allows the user to create "compressed RAM-based block devices".

If that doesn't make much sense, it is very similar to RAM drives, but compressed. I learned about using RAM drives a few years ago (mount -t tmpfs ...) and have been using them for all kinds of things on my systems to reduce IO load on my storage, while maximizing performance. With RAM drives you section off a portion of your RAM to use as "storage" [with a read/write speed of ~6 GB/s]. ZRam is very similar, except that it compresses inline, making even more "storage" available at the cost of less RAM, but slightly higher CPU.

How can we use zram?

A package compiling layer (like /var/pkgmk)

Previously, I did this (in fstab, but here's the command)

mount -t tmpfs -o size=10G none /var/pkgmk

That creates a 10 GB RAM device, mounted at /var/pkgmk, where I compile packages from source on my system (any Crux users out there?).

NOW however, I do this:

dev=$(zramctl -f)  # Find the first available zram device
zramctl "${dev}" -s 16384 -t $(( $(nproc) / 2 ))
mkfs.ext4 "${dev}"
mount "${dev}" /var/pkgmk

It's a bit more complicated to get set up (I have the above in a script), but the end result is a 16 GB RAM drive, which takes significantly less ram, using up to half of my CPU's cores to compress. I tend to see about 70% compression when compiling source code, so while the drive allows for 16 GB of capacity (the full amount in my laptop in fact), at its maximum it only takes about 5 GB; usually much less.

zswap (swapon... swapoff... swapon... swapoff)

I haven't used swap on my system in a long time. With computers coming with more and more RAM, I haven't needed it in a long time. However, I recently ran into some compiling tasks that, combined with my ram drive for storing compiling source code, consumes more RAM than my laptop has.

Enter zswap

Zswap is basically the same thing as a zram block device, except instead of being formatted with a real filesystem, it is formatted for swap usage. To set one of these up, I do this (again, this is distilled from the script I wrote):

dev=$(zramctl -f)  # Find the first available zram device
zramctl "${dev}" -s 10240 -t $(( $(nproc) / 2 ))
mkswap "${dev}"
swapon "${dev}"

That will create a new zram block device that is 10 GB in size, using up to half of my CPU's cores for compression. This zram device is then formatted for swap usage (mkswap), and enabled (swapon).

Conclusion

These commands obviously need to be put into a nice shell script for easy setup and teardown. I have my in an init script, supporting start, stop, and status commands to make things less cumbersome. You can see the full init script I wrote for zswap setup over here.

Do you have any other ideas for how to use zram? If so, feel free to email me any ideas you might come up with. I'd love to hear them!

Last edited: 2021-07-14 23:36:39 UTC