Table of Contents

ChangeLog

28 February 2016

  • Added support for lz4 and restart for the init script.

About

Using zram (formerly compcache) it is possible to slice a portion of the available RAM and create a compressed swap device that will eat some CPU cycles but will overall increase the amount of available RAM.

Setup

  1. Copy the init script to /etc/init.d/zram.
  2. Make the script executable with chmod +x /etc/init.d/zram.
  3. Make the script start on boot using update-rc.d zram defaults.

Code

zram
#!/bin/sh
 
### BEGIN INIT INFO
# Provides:          zram
# Required-Start:    $local_fs $syslog
# Required-Stop:     $local_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Use compressed RAM as in-memory swap
# Description:       Use compressed RAM as in-memory swap
### END INIT INFO
 
# Author: Antonio Galea <[email protected]>
# Thanks to Przemyslaw Tomczyk for suggesting swapoff parallelization
# Added LZ4 support, added restart suport
#    - Wizardry and Steamworks <[email protected]>
 
FRACTION=75
USE_LZ4=true
 
# Skip systemd redirect.
_SYSTEMCTL_SKIP_REDIRECT=OHYES
 
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
 
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
 
MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' < /proc/meminfo`
CPUS=`grep -c processor /proc/cpuinfo`
SIZE=$(( MEMORY * FRACTION / 100 / CPUS ))
 
case "$1" in
  "start")
    param=`modinfo zram|grep num_devices|cut -f2 -d:|tr -d ' '`
    modprobe zram $param=$CPUS
    for n in `seq $CPUS`; do
      i=$((n - 1))
      if [ $USE_LZ4 = "true" ]; then
          echo "lz4" > /sys/block/zram$i/comp_algorithm
      fi
      echo $SIZE > /sys/block/zram$i/disksize
      mkswap /dev/zram$i
      swapon /dev/zram$i -p 10
    done
    ;;
  "stop")
    for n in `seq $CPUS`; do
      i=$((n - 1))
      swapoff /dev/zram$i && echo "disabled disk $n of $CPUS" &
    done
    wait
    sleep .5
    modprobe -r zram
    ;;
  restart|reload|force-reload)
      ${0:-} stop
      ${0:-} start
    ;;
  *)
    echo "Usage: `basename $0` (start | stop)"
    exit 1
    ;;
esac

ZSWAP

Alternatively, ZSWAP is a kernel space solution to compressing RAM.


linux/compressed_ram.txt ยท Last modified: 2022/04/19 09:28 (external edit)

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy, copyright and the plagiarism pages.