Skip the following long introduction if you already know what zram is and just want to know how to get it working...
Many of us, may have occasionally or often run into problems with web-browsers freezing and becoming unresponsive. The problem occurs most often with large memory hungry browsers such as recent Firefox or google Chrome, and especially, of course, on computers that only have relatively low amounts of RAM, such as 2GB or less. Opening a new browser tab on a heavy web page such as Facebook or Gmail can require over 100MB of RAM use per tab, so it is not surprising that opening five such tabs can cause a 512MB RAM machine to freeze, and so on (you can test/measure this effect by watching free/available memory space with top utility running whilst opening multiple gmail tabs...).
Traditionally, the way to avoid the computer thus crashing due to RAM running out is to use a separate swap partition or swap file, such that the overall 'memory' (called virtual memory) available becomes "RAM + swap space" (active RAM memory pages get swapped in and out of RAM to swap space as and when required). Unfortunately, swap space is generally much slower to read and write than actual RAM (being often on slow hard-drive or flash media), so though the computer no longer crashes once actual RAM is exhausted, it typically starts running very slowly.
zram is a technique which uses a portion of actual RAM as if it is a normal block device (such as a hard-drive), but being in RAM it remains fast. Furthermore it uses compression such that for every 1GB RAM used as zram the storage space it provides is typically 2 to 3 times that amount. zram can thus be configured as swap space in memory, with most of the advantages, but much faster than conventional swap. Disadvantage is that is does use some CPU cycles to continually compress and decompress data and it does use some of the actual RAM; nevertheless, in my own measurements (on a 2GB RAM machine), using zram greatly increases the number of heavy browser tabs that can be opened before virtual memory runs out and browser freezes/crashes and performance remains good even during zram swapping.
Note that zram doesn't even start getting used until swapping becomes required, so there appears to be little or no reason not to use it. On a larger RAM machine, no swapping will generally occur so zram is not so necessary, however, it depends on what kind of processing you are doing. For example, if you are doing large compiling jobs, or processing video files, you may well require large amounts of RAM such that swapping remains likely to occur. Hence, even on my 4GB RAM machine (largest I have), I still enable zram since I find no reason (performance-wise) not to.
Furthermore, if you are using flash-based media, such as SSD, an SD card, or usb-flashstick, using zram for swap does not require continual data writes to the flash device, which helps it last longer. Even modern flash devices can only stand a finite number of write cycles and if, like me, you use ten year old computers (or want your computer to last at least that long) minimising flash-writes is definitely a good thing... Note that there is nothing to stop you also using conventional swap in addition to zram; the zram swap can be prioritized such that it will be used first such that any slower conventional swap is only ever used in particularly critical situations.
wiak (edited 26 Jan 2018)
Most of the following was tested on a XenialDog 64bit system, so file locations may or may not be different on other distribution variants. EDIT: The installation method will also vary for different Linux distributions, such as tradition Pups.* Introduction from https://www.kernel.org/doc/Documentatio ... v/zram.txt
The zram module creates RAM based block devices named /dev/zram<id>
(<id> = 0, 1, ...). Pages written to these disks are compressed and stored
in memory itself. These disks allow very fast I/O and compression provides
good amounts of memory savings. Some of the usecases include /tmp storage,
use as swap disks, various caches under /var and maybe many more
With thanks to backi, dancytron and fredx181 for much of the useful information.
Note that you need to be root user or use 'sudo' to do most of the following:
A. zram
1. Can be installed (on Debian apt-based systems such as above XenialDog platform employed in my tests) using:
Code: Select all
apt-get update && apt-get install zram-config
Code: Select all
/lib/systemd/system/zram-config.service
Code: Select all
[Service]
ExecStart=/usr/bin/init-zram-swapping
ExecStop=/usr/bin/end-zram-swapping
...
3. You can check you have zram compressed swapspace available using either top, free or zramctrl command. For example:
Code: Select all
free (or free -h)
Code: Select all
zramctl
Code: Select all
zramctl --help
Code: Select all
# initialize the devices
for i in $(seq ${NRDEVICES}); do
DEVNUMBER=$((i - 1))
echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm #wiak added
echo $mem > /sys/block/zram${DEVNUMBER}/disksize
mkswap /dev/zram${DEVNUMBER}
swapon -p 5 /dev/zram${DEVNUMBER}
done
Code: Select all
systemctl restart zram-config.service
Code: Select all
systemctl stop zram-config.service
Code: Select all
/usr/bin/end-zram-swapping
Code: Select all
systemctl start zram-config.service
Code: Select all
/usr/bin/init-zram-swapping
Code: Select all
mkfs -t ext4 /dev/zram1
Alternatively (or in addition) you can manually create a swap partition on another pre-created zram block device using, for example:
Code: Select all
/usr/bin/end-zram-swapping
modprobe zram num_devices=2
zramctl -r /dev/zram0
zramctl -a lz4 -s 512M /dev/ram0
mkswap /dev/zram0
swapon /dev/zram0
Code: Select all
#!/bin/sh
# load dependency modules
NRDEVICES=$(grep -c ^processor /proc/cpuinfo | sed 's/^0$/1/')
if modinfo zram | grep -q ' zram_num_devices:' 2>/dev/null; then
MODPROBE_ARGS="zram_num_devices=${NRDEVICES}"
elif modinfo zram | grep -q ' num_devices:' 2>/dev/null; then
MODPROBE_ARGS="num_devices=${NRDEVICES}"
else
exit 1
fi
modprobe zram $MODPROBE_ARGS
# Calculate memory to use for zram (1/2 of ram)
totalmem=`LC_ALL=C free | grep -e "^Mem:" | sed -e 's/^Mem: *//' -e 's/ *.*//'`
# mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024)) #wiak commented-out
mem=$(((totalmem * 75 / 100 / ${NRDEVICES}) * 1024)) #wiak added
# initialize the devices
for i in $(seq ${NRDEVICES}); do
DEVNUMBER=$((i - 1))
echo lz4 > /sys/block/zram${DEVNUMBER}/comp_algorithm #wiak added
echo $mem > /sys/block/zram${DEVNUMBER}/disksize
mkswap /dev/zram${DEVNUMBER}
swapon -p 5 /dev/zram${DEVNUMBER}
done
http://www.murga-linux.com/puppy/viewto ... 747#980747
More details of that earlier zram discussion can be provided in the pages of that same thread around that post.
You can also find details of setting up zram on systems that still use sysvinit rather than systemd at:
https://wiki.gentoo.org/wiki/Zram
9. Some related zram links provided by backi and others can be found in these earler XenialDog64 pages, including:
https://www.kernel.org/doc/Documentatio ... v/zram.txt
http://www.murga-linux.com/puppy/viewto ... 985#979985
http://www.murga-linux.com/puppy/viewto ... 217#980217
http://www.murga-linux.com/puppy/viewto ... 733#980733
http://www.murga-linux.com/puppy/viewto ... 737#980737
http://www.murga-linux.com/puppy/viewto ... 814#980814
https://wiki.debian.org/ZRam
https://starbeamrainbowlabs.com/blog/ar ... -zram.html
https://mxlinux.org/forum/viewtopic.php?f=108&t=42935
Some older zram-related murga-forum threads noted by backi:
http://www.murga-linux.com/puppy/viewto ... 1587d507c2
http://www.murga-linux.com/puppy/viewto ... 257c2a98a8
http://bkhome.org/news/201701/overlayfs ... -zram.html
Next I'll write-up some details of the zswap alternative for this thread.
wiak