Differences between revisions 3 and 13 (spanning 10 versions)
Revision 3 as of 2014-03-26 23:25:25
Size: 1518
Editor: ?AntonioGalea
Comment:
Revision 13 as of 2017-05-18 02:23:37
Size: 1962
Editor: PaulWise
Comment: clarify systemd section
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#language en
Line 6: Line 7:
 * http://en.wikipedia.org/wiki/Zram  * WikiPedia:Zram
Line 12: Line 13:
#!/bin/bash #!sh
#!/bin/sh
Line 20: Line 22:
# Description: Use compressed RAM as in-memory swap  # Description: Use compressed RAM as in-memory swap
Line 24: Line 26:
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or above, see terms at
# https://gnu.org/licenses/gpl-3.0.txt
Line 28: Line 33:
CPUS=`grep -c processor /proc/cpuinfo` CPUS=`nproc`
Line 37: Line 42:
      echo $SIZE > /sys/block/zram$i/disksize        echo $SIZE > /sys/block/zram$i/disksize
Line 45: Line 50:
      swapoff /dev/zram$i       swapoff /dev/zram$i && echo "disabled disk $n of $CPUS" &
Line 47: Line 52:
    wait
Line 57: Line 63:
then instruct you system to start it at boot time, with the command then you may have to make the script executable:
{{{
chmod +x /etc/init.d/zram
}}}

then instruct your system to start it at boot time, with the command
Line 61: Line 72:

== NB! ==

For systems running systemd, the [[https://packages.ubuntu.com/zram-config|Ubuntu package zram-config]] should be upgraded to 0.3 and newer.

zram (previously called compcache) can create RAM based block devices. It is an experimental (staging) module of the Linux kernel since 3.2.

Background information

Startup script

Copy the following script to /etc/init.d/zram:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          zram
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     S
# 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 <antonio.galea@gmail.com>
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or above, see terms at
#      https://gnu.org/licenses/gpl-3.0.txt

FRACTION=75

MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' < /proc/meminfo`
CPUS=`nproc`
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))
      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
    ;;
  *)
    echo "Usage: `basename $0` (start | stop)"
    exit 1
    ;;
esac

then you may have to make the script executable:

chmod +x /etc/init.d/zram

then instruct your system to start it at boot time, with the command

  insserv zram

NB!

For systems running systemd, the Ubuntu package zram-config should be upgraded to 0.3 and newer.