Update 02.03.2015: added (modified) Centos / Redhat:

Update 24.11.2022: added (modified) Promox 7.3x / Debian 11

A successor to compcache is zram which is fully integrated in the Linux kernel since 2.6.37.1 and uses lzo compression. The idea behind it is to create swap devices made of chunks of the ram and to compress those chunks on the fly to increase the available space used and ideally reduce the need of swapping to slow disks.

It uses a small extra amount of the CPU, however, the reduced i/o usage should more than make up for this. This is primarily interesting for a small scaled VPS, Netbooks or low memory devices. Also virtualisation hosts should benefit of compressed memory.

Unfortunatly the zram-config script is currently not part of the Debian and Centos distributions. I will run some further tests and update here.

In Ubuntu, from 12.04 onwards, the install script is included and it takes only a minute to setup zram.

How to setup in Ubuntu:

sudo apt-get install zram-config

To Test:

# cat /proc/swaps

Filename Type Size Used Priority
/dev/zram0 partition 482900 0 5
/dev/zram1 partition 482900 0 5
/dev/zram2 partition 482900 0 5
/dev/zram3 partition 482900 0 5

# free -h
total used free shared buffers cached
Mem: 3.7G 2.6G 1.1G 255M 46M 1.0G
-/+ buffers/cache: 1.5G 2.2G
Swap: 1.8G 0B 1.8G

In the above case, I haven’t had any swap configured prior to using zram.

There are also guides for Debian and Centos/RedHat but they involve bit more work and use external scripts.

Debian 7 / Proxmox:

This is taken from here…   https://wiki.debian.org/ZRam

vi /etc/init.d/zram 
#insert
#!/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 <[email protected]>
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization

FRACTION=75

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

You 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

To test it:

Before:
# free -h
total used free shared buffers cached
Mem: 2.0G 1.1G 841M 0B 336K 127M
-/+ buffers/cache: 1.0G 969M
Swap: 2.4G 0B 2.4G

# cat /proc/swaps 
Filename Type Size Used Priority
/dev/zd0 partition 2490364 0 -1

After:

# cat /proc/swaps 
Filename Type Size Used Priority
/dev/zd0 partition 2490364 0 -1
/dev/zram0 partition 1534428 0 10

# free -h
total used free shared buffers cached
Mem: 2.0G 675M 1.3G 0B 776K 60M
-/+ buffers/cache: 614M 1.4G
Swap: 3.8G 0B 3.8G

Proxmoxm 7.3x / Debian 11:

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

CentOS / Redhat:

Some sources state that you need Kernel version 2.6.37.1 or better compiled with zram module.
However, my CentOS 6.6 has 2.6.32-504.8.1.el6.x86_64 and it seems to work fine there too.

You can test that prerequisite by using:

 modprobe zram

If it doesn’t complain (return an error) you should be good to go.

For CentOS / RedHat install guide, I refer either to a guide from extremeshock. https://extremeshok.com/5094/centos-6-x-rhce-6-redhat-6-zram-compression-compressed-swap-residing-in-ram-over-allocating-memory/

Or to https://github.com/mystilleef/FedoraZram

Both ways worked flawless under CentOS 6.x and 7.x However, I had to reboot to get it really going.

Leave a Reply