Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
أجهزة ومعدات

How to Configure ZRAM on Ubuntu Linux


While ZRAM is a fantastic solution to trade some CPU horsepower to gain more RAM, how can you configure it to dedicate more or less RAM to ZRAM? How can you change the compression algorithm? In what scenarios are such changes worthwhile? Read on to find out how to configure ZRAM on Ubuntu.

Good to know: If you are not sure whether ZRAM, ZCache or ZSwap is suitable for you, we have the answer for you.

Check Your Current ZRAM State

Most people use ZRAM on their Ubuntu and Debian-based Linux distros through the zram-config script. That’s because it allows you to take advantage of ZRAM in the most straightforward way possible. You can install it with the command:

sudo apt install util-linux zram-config

Next, check whether ZRAM is already enabled in your Ubuntu machine. You can do that by running the zramctl utility:

Ensure that Zswap is currently disabled for your system:

sudo -s
echo 0 > /sys/module/zswap/parameters/enabled

Zswap is an alternative RAM cache solution that, similar to ZRAM, comes by default with every Linux distro. However, unlike ZRAM, it relies on physical swap partitions which means that it can be slow on older disks.

Reboot your computer to create the initial ZRAM block device in your Ubuntu system.

Tip: Learn how you can extend the life of your SSD drives by enabling TRIM in Ubuntu.

Knowing the Right ZRAM Setup

With your machine back up and running, check whether it is now reporting a ZRAM partition:

You can see a typical swapfile coexisting with a single ZRAM block device in our screenshot. On this, my system had 4GB of RAM and 2GB of ZRAM, which is half of our RAM amount. However, depending on your workload, you may want to change that size.

  • If you’re working with multimedia and video, those aren’t very compressible, so it’s probably best if you reduce your ZRAM’s size and increase your actual swap’s size.
  • If you’re working with large databases or other types of data that are highly compressible, it’s the opposite. In such scenarios, you can increase your computer’s responsiveness by increasing ZRAM’s size.

Another tweak worth looking into has to do with the compression algorithm used by ZRAM. By default, it uses LZO or LZO-RLE, which is light on the CPU. On modern CPUs, though, the newer ZSTD algorithm usually offers better compression rates, and its somewhat higher toll on the CPU isn’t felt. It’s worth using this algorithm instead of the older LZO variants if your PC is less than a decade old.

Change ZRAM Size

To change the ZRAM settings, you’ll have to edit its exec script in the “/usr/bin” folder. Type the following command in the terminal:

sudo nano /usr/bin/init-zram-swapping

Scroll through the “init-zram-swapping” script and find the line that starts with mem.

This defines how much space your kernel will allocate for the ZRAM block device. By default, it reserves half of your system’s physical memory. You can reduce this to a quarter by replacing “1024” with “512.”

A terminal highlighting the lower memory offset for the mem variable.

You can also increase the amount of memory that ZRAM takes by setting the mem value to “1536.” This will allocate three-fourths of your physical memory to ZRAM.

A terminal highlighting a higher memory offset for the mem variable.

Change ZRAM Compression Algorithm

You can also change ZRAM’s compression algorithm from the same spot. To do that, open a new terminal session, then list all the available compression algorithms for ZRAM:

cat /sys/block/zram0/comp_algorithm
A terminal showing all the available compression algorithms for the ZRAM module.

You can mix and match algorithms in advanced custom ZRAM configurations. However, if you are using the default values, all ZRAM partitions will be using the same algorithm.

Return to the “init-zram-swapping” exec script we edited before. There, find the following line:

echo $mem > /sys/block/zram0/disksize

Copy then paste it directly below so that you have two instances of the same command.

A terminal highlighting the duplicate echo UNIX pipe for the ZRAM block device.

Replace the $mem in the first instance at the beginning of the line with “zstd.”

A terminal highlighting the zstd string for the first echo UNIX pipe.

Change the end of the line from “disksize” to “comp_algorithm.”

A terminal highlighting the comp_algorithm string for the first echo UNIX pipe.

Note: If you want to use a different algorithm, change zstd in the command above with one of the values from cat /sys/block/zram0/comp_algorithm.

Save your tweaked “init-zram-swapping” file, then restart your computer.

Check whether your new ZRAM setting is working by running the zramctl command.

A terminal showing the ZRAM block device using a larger memory base and a different algorithm.

Make sure to check your system’s performance under load when it actively uses your tweaked swap. If you feel it chugging or stuttering, you may have overdone it and need to dial down the values you chose. If, on the other hand, it’s flying, you can probably increase them some more.

Configuring your system’s ZRAM is just the first step in optimizing your Ubuntu system. Learn how you can extract the most performance out of your machine by compiling your own Linux kernel on Ubuntu. Meanwhile, if you are looking to build a more powerful PC, here are the things to take note of when building a PC for Linux.

Image credit: Liam Briese via Unsplash. All alterations and screenshots by Ramces Red.

Ramces Red

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى