Differences between revisions 1 and 21 (spanning 20 versions)
Revision 1 as of 2014-03-26 23:23:04
Size: 1548
Editor: ?AntonioGalea
Comment:
Revision 21 as of 2022-07-15 06:51:50
Size: 2788
Editor: ?JohnEod
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= ZRam = #language en
'''zram''' (previously called '''compcache''') can create RAM based compressed block devices. It is a module of the Linux kernel since 3.2.
Line 3: Line 4:
'''zram''' (previously called '''compcache''') can create RAM based block devices. It is an experimental (staging) module of the Linux kernel since 3.2. See https://code.google.com/p/compcache/ and WikiPedia:Zram for more info.
Line 5: Line 6:
== Background information == == Installation and use ==
Line 7: Line 8:
 * https://code.google.com/p/compcache/
 * http://en.wikipedia.org/wiki/Zram
Starting at buster (released July 2019) it's possible to simply install the '''zram-tools''' package, customize '''/etc/default/zramswap''' for your needs.
Line 10: Line 10:
== Startup script == It will create RAM based compressed swap device eliminating need for physical swap device. If physical swap device exist, it will also be used by the system, but with a lower priority than a Zram device.
Line 12: Line 12:
Copy the following script to /etc/init.d/zram: Similar results as with Zram can be achieved with [[https://wiki.debian.org/Zswap|Zswap]], though Zswap needs a physical swap device (or swap file).

Install Zram and allow up to 60% of the RAM to be used as a zstd compressed swap space:
Line 14: Line 16:
## /etc/init.d/zram
#!/bin/bash
sudo apt install zram-tools
echo -e "ALGO=zstd\nPERCENT=60" | sudo tee -a /etc/default/zramswap
systemctl reload zramswap.service
}}}

For systems using a standard SysV-based init system, it's possible to copy the following script to ''/etc/init.d/zram'':
{{{

#! /bin/sh
# Author: Antonio Galea <antonio.galea@gmail.com>
#
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or later, see terms at
# https://gnu.org/licenses/gpl-3.0.txt
Line 23: Line 38:
# Description: Use compressed RAM as in-memory swap  # Description: Use compressed RAM as in-memory swap
Line 26: Line 41:
# Author: Antonio Galea <antonio.galea@gmail.com>
Line 29: Line 42:

MEMORY=`perl -ne'/^MemTotal:\s+(\d+)/ && print $1*1024;' < /proc/meminfo`
CPUS=`grep -c processor /proc/cpuinfo`
SIZE=$(( MEMORY * FRACTION / 100 / CPUS ))
MEMORY=$(perl -ne '/^MemTotal:\s+(\d+)/ && print $1*1024' < /proc/meminfo)
CPUS=$(nproc)
SIZE=$((MEMORY * FRACTION / 100 / CPUS))
Line 35: Line 47:
  "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
    done
sleep .5
    modprobe -r zram
;;
  *)
    echo "Usage: `basename $0` (start | stop)"
    exit 1
    ;;
    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 --priority 10
        done
    
;;
    stop)
    
for n in $(seq $CPUS)
        do
      
i=$((n - 1))
       swapoff /dev/zram$i && echo "zram: disabled disk $n of $CPUS" &
        done

        wait
    
sleep .5
        modprobe --remove zram
    
;;
    *)
     echo "Usage: $(basename $0) (start | stop)"
        exit 1
     ;;
Line 58: Line 75:

# End of file
Line 60: Line 79:
then instruct you system to start it at boot time, with the command then make the script executable:
Line 62: Line 81:
  insserv zram chmod +x /etc/init.d/zram
Line 64: Line 83:

then instruct your system to start it at boot time, with the command (skip this on systemd)
{{{
apt-get install insserv
insserv zram
}}}

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

See https://code.google.com/p/compcache/ and Zram for more info.

Installation and use

Starting at buster (released July 2019) it's possible to simply install the zram-tools package, customize /etc/default/zramswap for your needs.

It will create RAM based compressed swap device eliminating need for physical swap device. If physical swap device exist, it will also be used by the system, but with a lower priority than a Zram device.

Similar results as with Zram can be achieved with Zswap, though Zswap needs a physical swap device (or swap file).

Install Zram and allow up to 60% of the RAM to be used as a zstd compressed swap space:

sudo apt install zram-tools
echo -e "ALGO=zstd\nPERCENT=60" | sudo tee -a /etc/default/zramswap
systemctl reload zramswap.service

For systems using a standard SysV-based init system, it's possible to copy the following script to /etc/init.d/zram:

# Author: Antonio Galea <antonio.galea@gmail.com>
#
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or later, see terms at
# https://gnu.org/licenses/gpl-3.0.txt

### 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

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 --priority 10
        done
        ;;
    stop)
        for n in $(seq $CPUS)
        do
            i=$((n - 1))
            swapoff /dev/zram$i && echo "zram: disabled disk $n of $CPUS" &
        done

        wait
        sleep .5
        modprobe --remove zram
        ;;
    *)
        echo "Usage: $(basename $0) (start | stop)"
        exit 1
        ;;
esac

# End of file

then make the script executable:

chmod +x /etc/init.d/zram

then instruct your system to start it at boot time, with the command (skip this on systemd)

apt-get install insserv
insserv zram