make zram use lz4 compression algorithm

5,191

This works:

# initialize the devices
for i in $(seq ${NRDEVICES}); do
  DEVNUMBER=$((i - 1))
  echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm
  echo $mem > /sys/block/zram${DEVNUMBER}/disksize
  mkswap /dev/zram${DEVNUMBER}
  swapon -p 100 /dev/zram${DEVNUMBER}
done

Note: it seems you need to define the compression algorithm before zram size. Set swapon -p to priority level 100 to use zram actively as it improves performance.

Share:
5,191

Related videos on Youtube

Sirajus Salekin
Author by

Sirajus Salekin

Updated on September 18, 2022

Comments

  • Sirajus Salekin
    Sirajus Salekin over 1 year

    Ubuntu 18, trying to make zram to use lz4 compression.

    Tried modifying /usr/bin/init-zram-swapping file

    #!/bin/sh
    ...........
    ...........
    for i in $(seq ${NRDEVICES}); do
      DEVNUMBER=$((i - 1))
      echo $mem > /sys/block/zram${DEVNUMBER}/disksize
      echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm # <= added this line
      mkswap /dev/zram${DEVNUMBER}
      swapon -p 5 /dev/zram${DEVNUMBER}
    done
    

    But, after rebootzramctl still shows I'm using lzo algortihm

    NAME       ALGORITHM DISKSIZE  DATA  COMPR TOTAL STREAMS MOUNTPOINT
    /dev/zram3 lzo           466M  2.9M 614.2K    1M       4 [SWAP]
    /dev/zram2 lzo           466M  2.9M 631.2K    1M       4 [SWAP]
    /dev/zram1 lzo           466M  2.9M 536.1K 1020K       4 [SWAP]
    /dev/zram0 lzo           466M    3M   610K    1M       4 [SWAP]
    

    I also tried adding adding it inrc.local, but it throws device already in use warning, because as far as I know, the change needs to be done before mkswap and swapon runs.

  • Modestas
    Modestas over 5 years
    Yes, it seems you need to define the compression algorithm before zram size. Set swapon -p to 100 swapiness to use zram actively as it improves performance.
  • Sirajus Salekin
    Sirajus Salekin over 5 years
    thanks, i will check after going home. Too excited if this works :D
  • Tomofumi
    Tomofumi about 4 years
    IMHO I don't understand why you use swapon -p 100 here, rather than sysctl vm.swappiness=100?
  • Modestas
    Modestas about 4 years
    Tomofumi I don't know. Please tell if you find out.
  • nrc
    nrc over 2 years
    swapon -p 100 indicates the priority amongst swap partitions. You may have for example, a priority 10 zram swap and a priority 20 hdd swap partition. swappiness is instead the threshold of free memory at which point the system starts to use the swap.