To set the module at boot. As root add:
modprobe zram echo 256M > /sys/block/zram0/disksize mkswap /dev/zram0 > /dev/null && swapon -p 100 /dev/zram0
To the end of /etc/init.d/local.sh
Check with cat /proc/swaps: /dev/zram0 shows up with priority 100, and free -m shows the swap.
Note: cat /sys/block/zram0/comp_algorithm lists the compression methods of the kernel.
The size you set is how much swapped data zram can hold. That data is kept compressed, so it uses less real RAM than its size (often a third to a half), but compressing costs some CPU time. On old machines, don't make it too big. A common rule of thumb:
| RAM in the machine | zram size |
|---|---|
| 512M or less | about half of the RAM (256M on 512M) |
| 1G - 2G | half of the RAM |
| 4G | 1G - 2G |
| 8G and more | rarely needed |
Change the 256M in the commands (and in /etc/init.d/local.sh) to the size you want.
swapoff /dev/zram0 echo 1 > /sys/block/zram0/reset rmmod zram
To stop it at boot as well, remove the three zram lines from /etc/init.d/local.sh.
“sh: write error: Device or resource busy” when setting the size: zram0 is already set up (dmesg shows “zram: Cannot change disksize for initialized device”). Turn it off as above, then set it up again.
Not there after a reboot: check that the lines are at the end of /etc/init.d/local.sh, and look at the messages:
dmesg | grep zram
Live system: on a live USB or CD, the change to local.sh is lost at reboot unless the system is installed.