Discover how to enable zRAM in Rocky Linux 9 with our step-by-step guide. Optimize your system’s performance and memory management by leveraging this compressed RAM feature for efficient resource utilization. #centlinux #linux #linuxswap
zRAM, formerly called compcache, is a Linux kernel module for creating a compressed block device in RAM, i.e. a RAM disk with on-the-fly disk compression. The block device created with zRAM can then be used for swap (named as zswap) or as general-purpose RAM disk.
The zram module creates RAM-based block devices named /dev/zram<id> (<id> = 0, 1, …). Pages written to these disks are compressed and stored in memory itself. These disks allow very fast I/O and compression provides good amounts of memory savings. Some of the use cases include /tmp storage, use as swap disks, various caches under /var and maybe many more.
Statistics for individual zram devices are exported through sysfs nodes at /sys/block/zram<id>/
Yes, enabling zRAM can improve performance in certain scenarios, particularly on systems with limited physical memory (RAM) or systems that frequently encounter memory pressure. zRAM, also known as compressed RAM or virtual memory compression, works by compressing data stored in RAM, allowing more data to fit into memory and reducing the need for swapping data between RAM and disk storage (swap space).
Here are some ways in which zRAM can improve performance:
It’s important to note that while zRAM can improve performance in many scenarios, the benefits may vary depending on the specific workload and system configuration. Additionally, enabling zRAM may consume some CPU resources for compression and decompression operations, so there may be a trade-off between CPU usage and memory utilization. Overall, zRAM can be a valuable tool for optimizing memory usage and improving system performance, particularly on systems with constrained memory resources.
Zram and zswap are both technologies used in Linux systems to improve memory management by compressing memory pages. However, they serve slightly different purposes:
In summary, while both Zram and Zswap involve compressing memory pages to improve memory management, Zram creates a compressed block device in RAM, while Zswap acts as a compressed cache for swap pages. The choice between them depends on factors such as system resources, workload, and specific performance requirements.
Recommended Online Training: Learn Bash Shell in Linux for Beginners
We are using a minimal Rocky Linux 9 virtual machine with following specifications.
By using a ssh client, login to your Linux server as root user.
Create a file with following content, to enable zRAM module during loading of your Linux Kernel.
# cat > /etc/modules-load.d/zram.conf << EOF > zram > EOF
Create configuration file for zRAM Kernel Module.
In this configuration file, you can set the number of zRAM block devices (num_devices=x) for your Linux server.
# cat > /etc/modprobe.d/zram.conf << EOF > options zram num_devices=1 > EOF
Create a udev rule and specify the size of your zRAM device therein.
# cat > /etc/udev/rules.d/99-zram.rules << EOF > KERNEL=="zram0", ATTR{disksize}="2G",TAG+="systemd" > EOF
Open /etc/fstab file by using vim text editor.
# vi /etc/fstab
Find and comment the following line to disable automount of default swap partition. If you have more than one swap partitions then disable all of them.
#/dev/mapper/rl-swap none swap defaults 0 0
Create a systemd unit for zRAM to enable zswap partition on your Rocky Linux server.
By using vim text editor create a systemd unit.
# vi /etc/systemd/system/zram.service
Add following directives in this file.
[Unit] Description=Swap with zram After=multi-user.target [Service] Type=oneshot RemainAfterExit=true ExecStartPre=/sbin/mkswap /dev/zram0 ExecStart=/sbin/swapon /dev/zram0 ExecStop=/sbin/swapoff /dev/zram0 [Install] WantedBy=multi-user.target
Enable zram.service by executing following command.
# systemctl enable zram.service Created symlink /etc/systemd/system/multi-user.target.wants/zram.service → /etc/systemd/system/zram.service.
Reboot your Linux server to let it start with the new zswap partition.
# reboot
After reboot, check the status of your zram block devices by executing zramctl command.
# zramctl NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT /dev/zram0 lzo-rle 2G 4K 74B 12K 1 [SWAP]
Check the status of zswap memory by using free command at Linux bash prompt.
# free -m total used free shared buff/cache available Mem: 1743 392 1323 5 174 1351 Swap: 2047 0 2047
Currently, the zswap memory is not in used, because we are using a test machine with almost zero load on it. The output may vary on a producion grade server.
You need to write a C language program, to create some imaginery load on your Rocky Linux server.
You can use vim text editor to write your C language program.
# vi ~/memeater.c
Add following lines of code in this file.
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> int main(int argc, char** argv) { int max = -1; int mb = 0; int multiplier = 100; // allocate 1 MB every time unit. Increase this to e.g.100 to allocate 100 MB every time unit. char* buffer; if(argc > 1) max = atoi(argv[1]); while((buffer=malloc(multiplier * 1024*1024)) != NULL && mb != max) { memset(buffer, 1, multiplier * 1024*1024); mb++; printf("Allocated %d MBn", multiplier * mb); sleep(1); // time unit: 1 second } return 0; }
You may need to install C language compiler to compile your program.
Use dnf command to install gcc package.
# dnf install -y gcc
Now, use gcc command to compile your C language program.
# gcc ~/memeater.c -o ~/memeater
Adjust the swappiness of your Linux server to 100% for immediate result.
# sysctl vm.swappiness=100
Now execute memeater command.
# ~/memeater
Open another ssh session and check the usage of zswap partition.
# free -m total used free shared buff/cache available Mem: 1743 1713 30 13 473 30 Swap: 2047 834 1213
Our zswap partition is in use now.
Reboot your Linux server now.
# reboot
Unlock the potential for enhanced system performance and memory management with our guide to enable zRAM in Rocky Linux 9. By implementing this compressed RAM feature, you’ll optimize resource utilization and elevate your computing experience to new heights. Let this tutorial be your key to unleashing the full capabilities of your system.
Learn how to change Apache document root in Linux by following this step-by-step guide. Adjust…
Discover how to change Apache port in Linux easily. Follow our simple guide to modify…
Learn how to create a virtual host in Apache Server with this comprehensive guide. Set…
Discover 10 practical tasks for the RHCSA exam with step-by-step solutions. Boost your Linux skills…
Discover the ultimate Fail2ban configuration guide. Learn how to set up, customize, and optimize Fail2ban…
Explore the ultimate guide to VPS servers and learn everything about Virtual Private Servers, including…
This website uses cookies.