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.
Check Your Current ZRAM State
Most people use ZRAM on their Ubuntu, Mint, or other compatible distribution 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 zram-config
Its default values set ZRAM at half your computer’s actual RAM, split into multiple partitions, one for each core of your CPU. You can check this out with:
cat /proc/swaps
You can see a typical swap file (the “/var/cache/swap/swapfile” entry) coexisting with eight ZRAM volumes in our screenshot. My system had 8GB of RAM. If you add those ZRAM volumes together, the sum is 4GB, 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.
You can use the zramctl
command to check the algorithm currently in use by ZRAM. Type zramctl
in a terminal and press Enter to see your ZRAM swaps.
Zramctl should already be available on your distribution. If it’s not, since it’s part of the util-linux package, you can bring it on board with:
sudo apt install util-linux
To control how ZRAM works, though, you’ll have to tweak its main script. Let’s start by changing the amount of RAM it uses.
Change ZRAM Size
To change the ZRAM settings, you’ll have to edit its exec script in the bin folder. Type the following command in the terminal:
sudo nano /usr/bin/init-zram-swapping
To change the amount of RAM used by ZRAM, you’ll have to edit the following line:
mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024 ))
This line defines how much RAM ZRAM will use based on some simple calculations:
totalmem
is the amount of RAM installed on your PC/2
divides totalmem by two, translating to “half the total amount of RAM”/ ${NRDEVICES}
divides that number by the number of cores on your PC* 1024
multiplies the result of the above by 1024
You can’t, and shouldn’t, change anything in point 1 and 3, but you can play with the values in 2 and 4 to tweak the amount of RAM used by ZRAM.
- By increasing
/ 2
to/ 4
, your total amount of RAM would be divided by four instead of two. Thus, ZRAM would use only one-quarter of your RAM instead of half. We’d advise against dedicating your whole RAM to ZRAM, so it is best to leave this value at/ 2
or increase it if you want ZRAM to use less memory.
- Similarly, by keeping
/ 2
but decreasing1024
to512
, you’d achieve the same result since you’d be halving the available RAM for each device. If you’d like to have ZRAM use more RAM, you could increase this value to1536
instead.
Change ZRAM Compression Algorithm
You can change ZRAM’s compression algorithm from the same spot.
First, check the compression algorithm it currently uses:
cat /sys/block/zram0/comp_algorithm
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. Thus, you don’t have to check all of them – in the command above, we’re only checking out the first one.
We were already using the ZSTD algorithm, which you can see enclosed in brackets in the screenshot above. That’s considered the best solution now between compression rates, speed, and demand in resources, so leave it as it is. However, if you’re using the defaults, you’ll have either LZO or LZO-RLE enabled. To change it, return to the init-zram-swapping exec script we edited before. There, find the following line:
echo $mem > /sys/block/zram${DEVNUMBER}/disksize
Copy it and then paste it directly below so that you have two instances of the same command. This line assigns the result of the calculations we saw before as the size of each ZRAM volume. However, we’ll use it to define the compression algorithm instead.
- Replace
$mem
in the first instance at the beginning of the line withzstd
. - Replace
disksize
at the end of the line withcomp_algorithm
.
The tweaked line should look like this:
echo zstd > /sys/block/zram${DEVNUMBER}/comp_algorithm
Our tweaked line echoes zstd
as the value in the parameter comp_algorithm
instead.
If you want to use a different algorithm, change zstd
in the command above with the algorithm’s name.
Save the tweaked file, restart your computer, and check the results.
Make sure to check your system’s performance under load when it’s actively using your tweaked swaps. 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.
Now that you know how to configure ZRAM in Ubuntu, if you are looking to build a more powerful PC, here are the things to take note of when building a PC for Linux.
Our latest tutorials delivered straight to your inbox