Skip to content

Instantly share code, notes, and snippets.

@cnmoro
Forked from zax4r0/zram Arch.md
Created January 17, 2024 19:07
Show Gist options
  • Save cnmoro/1a7df6c3fe40fb3f7cbf9c197e52ad7e to your computer and use it in GitHub Desktop.
Save cnmoro/1a7df6c3fe40fb3f7cbf9c197e52ad7e to your computer and use it in GitHub Desktop.
Zram On Arch

zRam is a virtual memory compression using block devices named /dev/zram using a fast compression algorithm (LZ4) that compress the least recently used (LRU) or inactive space in the memory allows the GNU/Linux kernel to free up more memory with less performance hit.

zRam is greatly increased the available amount of memory by compressing memory without swap disks/partition. It is recommended for the user to use zRam instead of not use/disable the swap to prevent out of memory (OOM) killer. Create a zRam block devices Load the zRam modules to the kernel using modprobe:

sudo modprobe zram

Set the zRam extremely fast compression algorithm using lz4:

sudo sh -c "echo 'lz4' > /sys/block/zram0/comp_algorithm"

Set 2 Gigabyte available zRam disk space for swap:

sudo sh -c "echo '2G' > /sys/block/zram0/disksize"

Create a swap on zRam block devices:

sudo mkswap --label zram0 /dev/zram0

Enable the zRam block devices for swapping with high priority:

sudo swapon --priority 100 /dev/zram0

Automatically activate zRam swap at startup Create a systemd service (zram.service) using a text editor such as nano:

sudo nano /etc/systemd/system/zram.service

Add following to the zram.service:

[Unit] 
Description=zRam block devices swapping 
 
[Service] 
Type=oneshot 
ExecStart=/usr/bin/bash -c "modprobe zram && echo lz4 > /sys/block/zram0/comp_algorithm && echo 2G > /sys/block/zram0/disksize && mkswap --label zram0 /dev/zram0 && swapon --priority 100 /dev/zram0" 
ExecStop=/usr/bin/bash -c "swapoff /dev/zram0 && rmmod zram" 
RemainAfterExit=yes 
 
[Install] 
WantedBy=multi-user.target

Enable the zram.service to automatically run at startup:

sudo systemctl enable zram

Disable zRam block devices To disable the zRam block devices swapping, run following commands:

sudo swapoff /dev/zram0

Remove the zRam module from the kernel:

sudo rmmod zram

Disable the zram.service from automatically run at startup:

sudo systemctl disable zram.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment