Tuning ZRAM in Fedora for Better Performance and Get Rid of OOM Crashes

Every user know that it’s important to properly optimize memory usage to prevent system crashes, be it that you use Linux, MacOS or Windows. This is especially the case when running memory-intensive tasks like compiling software, operating multiple and heavy virtual machines, or even with too many browser tabs open.

In this blog post, I want to share with you a tweak I’ve setup to avoid Out Of Memory (OOM) issues without having to increase physical RAM or SWAP space, using Fedora’s SWAP on zram feature.

This method has been working excellently for me without any significant slowdown or crashes in the last six weeks. It should work just as well on Fedora 39, although I am currently running it on 38, as two Gnome extensions I use are not yet fully compatible with Gnome 45.

More info: Fedora Wiki
Somehow, I believe the changes proposed in this wiki to bump the default zram fraction never made it to release.

Let’s get to it!

Calibrating Allocation with ZRAM

Before we dive into the process, let’s check our current allocation. You can use the zramctl command to get this data. This command controls the compressed RAM block device. By executing it, you can view the information about zram block devices.

We want to see how much compression goes on with the current setup and usage. As we’ll see later, this is important because we want to maximize the percentage of data in RAM that’s compressed while still having enough space for incompressible data.

In the below example, pay attention to the DATA and COMPR columns that show the efficiency of data compression. In this case, 4.1G of data in RAM results in only 1.1G once compressed in zram - a very significant gain!

> $ zramctl                                                                                                                   
NAME       ALGORITHM DISKSIZE  DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle       7.6G  4.1G    1G  1.1G       8 [SWAP]

You can also use free command with the -mh parameters to get memory details in a more human readable format.

> $ free -mh                                                                                                         
               total 
Mem:            15Gi 
Swap:          7.6Gi 

Customizing ZRAM Configuration

Fedora, by default, uses SWAP on zram, making the process much easier since we only need to tweak it and not install from scratch.

We will increase the zram ratio, which effectively increase the amount of RAM. I have not found it necessary to add a writeback device for data that can’t be compressed effectively.

Hereby, we’re going to modify the settings in /etc/systemd/zram-generator.conf file using nano editor.

nano /etc/systemd/zram-generator.conf

This is the configuration I use for a laptop with 16G of RAM:

[zram0]
zram-fraction=0.75
max-zram-size=12288

Through the balance of trial and error, I’ve found zram-fraction=0.75 (75%) to provide an excellent balance between giving a substantial memory boost and leaving enough space for data that can’t be compressed.

Restarting the Service

After setting up the appropriate configuration, we need to restart the service to apply our new settings.

> $ systemctl restart systemd-zram-setup@zram0.service

Checking the Result

Now, you can check your memory status again. We see the total SWAP space has increased.

> $ free -mh                                                                                                                       
               total        used        free      shared  buff/cache   available
Mem:            15Gi       4.0Gi       1.7Gi       2.5Gi       9.5Gi       8.3Gi
Swap:           11Gi          0B        11Gi

Following these steps should prepare you to handle heavier workloads without OOM issues on your Fedora system.

I hope this helps and stay tuned for more tips and tricks!