DEV Community

Oshan Wisumperuma
Oshan Wisumperuma

Posted on • Updated on

How to create faster smaller swap space

Even though swap is not necessary for the most of use cases, but developers might need swap space when doing a big compilation, browsing on chrome or on slack. but it is a really painful experience using a swap partition on Disk space. ZRAM helps us allocate small storage from RAM as swap space and store 3,4 times of data on that space. its even faster than the SSD swap partitions because this is a ram drive.

Since zstd has an excellent compression ratio, 2GB of zram space will have total swap capacity around 6GB. please note that these commands are only tested on Fedora 32. All the changes will be lost in the next reboot.

$ modprobe zram num_devices=2
$ echo zstd > /sys/block/zram0/comp_algorithm
$ echo 2G > /sys/block/zram0/disksize
$ mkswap --label zram0 /dev/zram0
Enter fullscreen mode Exit fullscreen mode

enable the swap partition with higher priority

$ swapon --priority 1 /dev/zram0

Tadaa! 🎉 now we can open another chrome tab ;-)

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           15Gi       7.3Gi       1.8Gi       1.5Gi       6.4Gi       6.4Gi
Swap:         2.0Gi       5.0Mi       2.0Gi

Enter fullscreen mode Exit fullscreen mode

to disable the swap storage

$ swapoff /dev/zram0

update: zram is enabled by default

Top comments (1)

Collapse
 
louislow profile image
Louis Low

I install preload to automatically adjust the swap for me. Install and forget, no hassle.