DEV Community

Puru
Puru

Posted on

6

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.

[zram0]
zram-fraction = 1
max-zram-size=none

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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (2)

Collapse
 
mfat profile image
Mehdi mFat

Doing this means half of your phisizal memory is reserved for zram. Only 8gn remains available for system. Is it really a good idea?

Collapse
 
mble profile image
Maciej Błędkowski

Thanks <3

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay