DEV Community

Puru
Puru

Posted on

Dynamically Increase SWAP (ZRAM) Size in Linux

Alt Text


Introduction

Swap memory is useful, but no one likes it when it's slow. Swap in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space.

Fedora particularly has been using swap-on-zram by default for years and comes by default on Fedora Workstation. ZRAM creates a block device in RAM where pages that would otherwise be written to swap (disk/ssd) are instead first compressed, then stored. This allows for much faster I/O of swap, and also the data compression provides a great amount of memory savings.

The zram device, typically /dev/zram0 has a size set at create time during early boot, by zram-generator per its configuration file. The memory used is not preallocated. It's dynamically allocated and deallocated, on-demand. Due to compression, a full /dev/zram0 uses half as much memory as its size.

The system will use RAM normally up until it's full, and then start paging out to swap-on-zram, same as a conventional swap-on-drive.


Dynamically increase swap size on-demand

If you are using Fedora Linux, you should already have zram0 setup and running. You can run the following command to verify:

Alt Text

Now, edit the zram-generator configuration file located at /usr/lib/systemd/zram-generator.conf

Alt Text

And increase the SWAP size as desired, e.g: increase it 1x the RAM size.

Finally restart the service to take the new configuration in effect.

Alt Text

Verify that the swap size has increased by running the following commands:

Alt Text



References

Discussion (1)

Collapse
mbled profile image
Maciej Błędkowski

Thanks <3