Root-Server Tutorial

ROOT-SERVER TUTORIAL

How to Use zRam for Compressing Data into RAM

Learn how to use zRam for compressing data into RAM.

Author:

Marcel Pabst

Last updated:

08/11/2021

Introduction

Especially on systems with little RAM, it can happen from time to time that the available memory becomes scarce and the system starts to use swap memory. As swap memory is typically located on the hard drive, it is relatively slow compared to RAM and thus may impact the system's performance.

A remedy is provided by a technology integrated in the Linux kernel called zRam. It creates a compressing block device directly in the computer's RAM. The kernel first occupies the available RAM and then tries to compress parts of it into zRam. It effectively only moves data within the RAM, which is magnitudes faster than swapping to disk. This technique, therefore, allows more data to be accommodated in memory. Practically, this results in the system not having to swap to a slower hard disk as quickly. The price is a slightly increased processor load which usually has a much smaller impact than swapping to disk.

The installation is quite simple but can make a big impact on the performance of your server if memory is tight.

Requirements

This tutorial should be adaptable to most Linux Distros since zRam is a kernel function. Yet the activation of the feature/installation of the package might be different. I want to keep the tutorial related to Ubuntu and Debian to keep it short and meaningful. So all you need is a vServer or root server from netcup with Ubuntu or Debian installed and a user with root access.

Step 1 - Install zram-tools

sudo apt install zram-tools

Step 2 - Adjust the configuration

Edit the configuration file with your favorite text editor, e.g. sudo editor /etc/default/zramswap:

## EXAMPLE Configuration
## Adjust to your needs
#
## Specifies amount of zRam devices to create.
## By default, zRamswap-start will use all available cores.
CORES=4

## Specifies the amount of RAM that should be used for zRam
## based on a percentage the total amount of available memory
##PERCENTAGE=10

## Specifies a static amount of RAM that should be used for
## the zRam devices, this is in MiB
ALLOCATION=4096

## Specifies the priority for the swap devices, see swapon(2)
## for more details.
##PRIORITY=100

In this example configuration, we assume that we have a system with 4 cores, all of which are to be used for (de)compression.

We can then tell zRam how much of our RAM should be used. This can either be a percentage of the total amount of available memory (PERCENTAGE) or a fixed value (ALLOCATION). In the example, we decided to use a fixed amount of 4096 MiB of RAM.

PRIORITY sets the priority for the swap device. You normally want to use zRam as the first swap device used, so we keep this at 100 (which is the highest priority).

CORES and ALLOCATION now determine the number of block devices and their respective size (you will see what this means in a moment).

Step 3 - Reboot

There are various methods to reboot your system. Here is one that works from your terminal:

sudo reboot

Step 4 - Check your configuration

Now you should be able to see your zRam SWAP by typing sudo swapon -s, which will show output like this:

Filename                                Type            Size    Used    Priority
/swapfile                               file            4194300 0       -2
/dev/zram0                              partition       1048572 30472   100
/dev/zram1                              partition       1048572 30216   100
/dev/zram2                              partition       1048572 30652   100
/dev/zram3                              partition       1048572 30772   100

We have a "standard" swap file /swapfile that is located on our actual hard drive (this is the one we only want to use if we have no other option) and we have four zRam partitions /dev/zram0 to /dev/zram3 (they equal our configured cores) with the size of ALLOCATION / CORES (4096 MiB / 4). You can also see how much zRam is currently used and the priorities of the swap devices.

Conclusion

Congratulations! You have successfully installed and configured zRam to make better use of your system's resources.

If you want to learn more about how zRam (swap) is used on your server, you should take a look at /etc/sysctl.conf, where you can configure your swappiness, cache pressure etc.

## SYSCTL ZRAM EXAMPLE Configuration
vm.vfs_cache_pressure=500
vm.swappiness=100
vm.dirty_background_ratio=1
vm.dirty_ratio=50

The configuration above works well on systems with low memory. We start swapping early (as we want to use our zRam as early as possible) and have set a high cache pressure to reduce memory consumption in the long run. For a full explanation of the settings take a look here: https://www.kernel.org/doc/html/latest/admin-guide/sysctl/vm.html

License

MIT Licence

Copyright (c) 2021 netcup

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributor's Certificate of Origin

By making a contribution to this project, I certify that:

  1. The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or

  2. The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or

  3. The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

  4. I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.

Published 08/11/2021 by Marcel Pabst

Submit your tutorial

Get 60€ netcup vouchers for every published tutorial.