IT Knowledge Base

Technical Repository

HowTo: Enable Linux ZRAM Compression

ZRAM compression ( compressed swap residing in ram / over allocating memory )

A successor to compcache, zram is fully integrated in the Linux kernel and uses lzo compression. Compress memory on the fly to reduce swapping. Uses a small amount of cpu, however the reduced i/o usage more than makes up for this.

Note: We will only compress 50% of system memory.

This is done to reduce cpu usage when less than 50% of system memory is needed. You can comment out a line in the script to use 100% of the system memory.

Login as root

sudo -i

Create the file /etc/init.d/zram with the following contents

/etc/init.d/zram
#!/bin/bash
### BEGIN INIT INFO
#Provides: zram
#Required-Start:
#Required-Stop:
#Default-Start: 2 3 4 5
#Default-Stop: 0 1 6
#Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
#Description: Adapted for Raspian (Rasberry pi) by eXtremeSHOK.com using https://raw.github.com/gionn/etc/master/init.d/zram
### END INIT INFO

start() {
    mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')

    modprobe zram

    sleep 1
    #only using 50% of system memory, comment the line below to use 100% of system memory
    mem_total_kb=$((mem_total_kb/2))

    echo $((mem_total_kb * 1024)) > /sys/block/zram0/disksize

    mkswap /dev/zram0

    swapon -p 100 /dev/zram0
}

stop() {
    swapoff /dev/zram0
    sleep 1
    rmmod zram
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 3
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        RETVAL=1
esac
chmod +x /etc/init.d/zram
update-rc.d zram defaults

Restart the system to apply the changes

shutdown -r now

Checking

swapon -s



Leave a Reply