DEV Community

Puru
Puru

Posted on

7

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay