Differences between revisions 21 and 23 (spanning 2 versions)
Revision 21 as of 2022-07-15 06:51:50
Size: 2788
Editor: ?JohnEod
Comment:
Revision 23 as of 2022-09-20 03:04:45
Size: 2066
Editor: PaulWise
Comment: formatting
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
See https://code.google.com/p/compcache/ and WikiPedia:Zram for more info. If physical swap devices exist, they will also be used by the system, but with a lower priority than a Zram device.
Line 6: Line 6:
== Installation and use == See [[https://www.kernel.org/doc/html/latest/admin-guide/blockdev/zram.html|Linux zram documentation]] and WikiPedia:Zram for more info.
Line 8: Line 8:
Starting at buster (released July 2019) it's possible to simply install the '''zram-tools''' package, customize '''/etc/default/zramswap''' for your needs. Similar results as with Zram can be achieved with [[Zswap]], though Zswap needs a physical swap device (or swap file).
Line 10: Line 10:
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. <<TableOfContents>>
Line 12: Line 12:
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). == Automated setup ==
Line 14: Line 14:
Install Zram and allow up to 60% of the RAM to be used as a zstd compressed swap space: The packages DebianPackage:zram-tools and DebianPackage:systemd-zram-generator can be used to automatically setup zram devices.

=== zram-tools ===

DebianPackage:zram-tools is available since Debian buster.

It currently only sets up zram devices on systemd-based systems.

To allow up to 60% of the RAM to be used as a zstd compressed swap space:
Line 18: Line 27:
systemctl reload zramswap.service sudo service zramswap reload
Line 21: Line 30:
For systems using a standard SysV-based init system, it's possible to copy the following script to ''/etc/init.d/zram'': ==== sysvinit ====

zram-tools [[DebianBug:960524|does not yet support sysvinit systems]] but there is an [[https://bugs.debian.org/cgi-bin/bugreport.cgi?att=1;bug=960524;filename=zramswap;msg=10|init script]] available. Download it and save it to ''/etc/init.d/zramswap'', then make the script executable and then instruct your system to start it at boot time:
Line 23: Line 35:

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

### 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
sudo chmod +x /etc/init.d/zramswap
sudo apt install insserv
sudo insserv zramswap
Line 79: Line 40:
then make the script executable: === systemd-zram-generator ===

systemd-zram-generator integrates zram devices into systemd using generated swap (and ext2 mount) units. This allows managing them individually with `systemctl` when desired.

== Manual setup ==

DebianMan:zramctl from DebianPackage:util-linux can list the zram devices present and their status and control them.
Line 81: Line 49:
chmod +x /etc/init.d/zram $ sudo zramctl
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4 4G 2.1G 248.6M 260.4M 8 [SWAP]
Line 83: Line 53:

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.

If physical swap devices exist, they will also be used by the system, but with a lower priority than a Zram device.

See Linux zram documentation and Zram for more info.

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

Automated setup

The packages zram-tools and systemd-zram-generator can be used to automatically setup zram devices.

zram-tools

zram-tools is available since Debian buster.

It currently only sets up zram devices on systemd-based systems.

To 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
sudo service zramswap reload

sysvinit

zram-tools does not yet support sysvinit systems but there is an init script available. Download it and save it to /etc/init.d/zramswap, then make the script executable and then instruct your system to start it at boot time:

sudo chmod +x /etc/init.d/zramswap
sudo apt install insserv
sudo insserv zramswap

systemd-zram-generator

systemd-zram-generator integrates zram devices into systemd using generated swap (and ext2 mount) units. This allows managing them individually with systemctl when desired.

Manual setup

zramctl from util-linux can list the zram devices present and their status and control them.

$ sudo zramctl
NAME       ALGORITHM DISKSIZE  DATA  COMPR  TOTAL STREAMS MOUNTPOINT
/dev/zram0 lz4             4G  2.1G 248.6M 260.4M       8 [SWAP]