zram and zswap

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

zram and zswap

#1 Post by wiak »

zram and zswap

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)
* 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 :)
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.

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
2. On booting, systemd then initialises and starts zram via:

Code: Select all

/lib/systemd/system/zram-config.service
which contains lines:

Code: Select all

[Service] 
ExecStart=/usr/bin/init-zram-swapping 
ExecStop=/usr/bin/end-zram-swapping 
...
By default to created /dev/zram* block filesystems are automatically prepared for use as (compressed) swap filesystems using compression type lzo; one such filesystem per system CPU core. For example, on a dual-core CPU system you will have /dev/zram0 and /dev/zram1 prepared and activated as compressed swap filesystems.

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)
or, for more detail, use:

Code: Select all

zramctl
Note that the zramctl program (provided by zram-config deb) can also be used for changing swapsize, compression type and so on. For usage help, you should refer to it's online man page or for usage summary:

Code: Select all

zramctl --help
4. If you want to use lz4 compression in preference to that lzo default, edit /usr/bin/init-zram-swapping to contain the extra line shown as "#wiak added" below:

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
You can then re-activate zram swap either by re-booting your system or re-starting the zram-config.service with command:

Code: Select all

systemctl restart zram-config.service
5. When using the systemd service method of using zram you can disable it simply using command:

Code: Select all

systemctl stop zram-config.service
or by directly executing bash script:

Code: Select all

/usr/bin/end-zram-swapping
6. You can start it up again using either:

Code: Select all

systemctl start zram-config.service
or by directly running bash script:

Code: Select all

/usr/bin/init-zram-swapping
7. Assuming you have disabled zram being used solely for swap, you can use the /dev/zram* block devices like any other block device. For example, you can create a compressed ramdisk (i.e. in RAM memory) with a command such as:

Code: Select all

mkfs -t ext4 /dev/zram1
and use it for anything you like (such as mounting /tmp on it).

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
A simpler way of doing the above, however, is simply to make a re-named copy of the script /usr/bin/init-zram-swapping and modify that for the purpose your desire. One example would be to simply modify that script for a different swap size calculation as follows (refer to #wiak added):

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
8. An alternative to using zram-config (or systemd methods) is to use the debian script, dancytron modded for lz4 compression use, and provided in the following link:

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
Last edited by wiak on Mon 26 Mar 2018, 09:21, edited 23 times in total.

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#2 Post by wiak »

reserved for further info

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#3 Post by wiak »

reserved for further info

backi
Posts: 1922
Joined: Sun 27 Feb 2011, 22:00
Location: GERMANY

#4 Post by backi »

Hi wiak !
So you have been quite busy lately !You are a real Working-Horse .
Maybe you should mention in your Introduction Zram/Zwap can be used for machines with low Ram....... . Not Everyone is familiar with the Concept of Zram .
Nevertheless great Compilation for Starters ..... Work in Progress .

Greetings :)

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#5 Post by wiak »

Hi backi,

First post edited to first explain zram in some detail.

wiak

backi
Posts: 1922
Joined: Sun 27 Feb 2011, 22:00
Location: GERMANY

Any Experiences with zram ?

#6 Post by backi »

Hi Everybody !

What`s your Experience with zram on low-ram Machines ?

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#7 Post by Flash »

Wiak, thanks for the explanation. (I took the liberty of changing your title a bit. I hope you don't mind. If you do, please feel free to change it back. :) )

For what it's worth, according to Hardinfo, Quirky Werewolf 64-bit, running entirely from DVD without a hard disk drive, uses nearly all of the 4 GB of RAM in my computer, so there wouldn't be any left in which to put some swap.

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#8 Post by wiak »

I noted something changed re 'ze Dogs' in my main post content text also, but have no time or desire to add to zram/zswap info at present - busy on other things. Got rid of the for 'ze Dogs' additions though since not my own personal intention at all.

grammar edits
Last edited by wiak on Mon 26 Mar 2018, 09:46, edited 5 times in total.

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#9 Post by wiak »

zram and zswap definitely not just potentially useful for the Dogs, despite my statement in first post that I did my tests using XenialDog system - only the install method via apt-get applies only to Debian apt-based systems, everything else also applies to Puppies and Linux more generally. With all due respect, I would be grateful if consent was requested before modifying technical document published by author - my name is on the post so any inaccuracy introduced appears mine... Also, even simple puppy forum technical posts inherently contain Intellectual Property rights (whether worthwhile, worthless, valuable or valueless in content) and these rights should be respected. By all means ask me if something is offensive or wrong in such a way that I can amend or delete according to my own choice (or then delete if I refuse). That would be much preferred, thanks. Personally I wouldn't contribute otherwise (not that I'm saying that matters... and I doubt anyone has time to make anything 'perfectly' correct or 'complete' anyway, though I already did add quite a bit extra, at backi's request, regarding what I feel are zram particular benefits for low-ram machines).

wiak

User avatar
Puppus Dogfellow
Posts: 1667
Joined: Tue 08 Jan 2013, 01:39
Location: nyc

some success in 64 bit, none so far in 32.

#10 Post by Puppus Dogfellow »

hello, wiak!

i think think this is really cool and got a bit inspired by some early success (here's the basic structure and contents of the pets):

Code: Select all

root# tree -alfhi:
.
[  60]  ./bin
[ 23K]  ./bin/zramctl
[  80]  ./pet.specs
[  46]  ./pinstall.sh
[  80]  ./root
[  60]  ./root/my-applications
[ 120]  ./root/my-applications/bin
[ 383]  ./root/my-applications/bin/zramctll
[1.4K]  ./root/my-applications/bin/zram.sh
[  25]  ./root/my-applications/bin/zram-start
[  24]  ./root/my-applications/bin/zram-stop
[  60]  ./root/puppy-reference
[ 100]  ./root/puppy-reference/mini-icons
[8.5K]  ./root/puppy-reference/mini-icons/zraminfo.xpm
[8.5K]  ./root/puppy-reference/mini-icons/zramstart.xpm
[8.5K]  ./root/puppy-reference/mini-icons/zramstop.xpm
[  60]  ./tmp
[4.2K]  ./tmp/zram-config_0.5_all.deb
[  60]  ./usr
[  60]  ./usr/share
[ 100]  ./usr/share/applications
[ 209]  ./usr/share/applications/zramctll.desktop
[ 160]  ./usr/share/applications/zram-start.desktop
[ 156]  ./usr/share/applications/zram-stop.desktop

10 directories, 14 files
that's the 32 bit version. apart from zramctl having been compiled (click the "compile.sh" in zramctl-master (see zramctll for link) in a 64bit tahr spin (underneath it's tahr64 605) rather than a 32 bit xenial spin (a modded 704 that has always been a bit buggy for me, but the pet failed similarly on xenial 7.5 32 and tahr 605 32 bit), the 64 bit below is the same, albeit a bit more succussful (mounted zram as expected via dancytron's script, but zram-stop failed to stop it. zramctll (

Code: Select all

#!/bin/sh
echo "see
zramctl main: https://github.com/Railag/zramctl
wiak's thread: http://murga-linux.com/puppy/viewtopic.php?p=981202#981202
dancytron's script: http://www.murga-linux.com/puppy/viewtopic.php?p=980747#980747
for more.

zram status via swapon -s and zramctl commands:

" > /tmp/zramstatc
swapon -s >> /tmp/zramstatc
zramctl >> /tmp/zramstatc
leafpad /tmp/zramstatc
)
gives a snapshot of the activity (or non-existence, in the case of my 32 bit attempts) of the zram disk you may or may not have created. in the xenial 704 pup, the activate zram scripts included in the pet just close the terminal (slam it shut), but in my old gateway desktop (1.8 gig of ram, 2 Ghz), things worked so well that i made a 32 bit pet for my even slower machine (asus netbook with 2 gig of ram clocked at 1.6 Ghz), which i'm hoping you can help me get to work.

subjective review and what i remember from the (maybe more objective) snapshots:

the main browser on the machine is palemoon, and that's what's most readily available to anyone who wants to use it. my wife, having discovered yahoo no longer displays properly in it for her, has taken to booting firefox on top of an already running and loaded palemoon instance (mostly ad heavy and other parasitic sites). i notice the swap activity via zramctll was more active than my 7gb swap partition, despite being relatively less nice (priority is 5 for the zram versus -1 for the partition).(though i may have that whole thing ass backwards. assuming i don't--)

less priority but it's just a more active aspect in general?

seeing that things weren't looking too good for speed and comfort, i opened a tab to a site that recently slowed my asus down, the ergodox ez keyboard site--the gateway handled it after a little bit of a delay--i saw the zram swap increase via the leafpad window that opens, felt that it was helping and would help considerably on the other machine. so,

what am i doing wrong? why won't it work for me at all in 32 bit? (i'd take the inability to swap off the zram in exchange for being able to enable/create it).

to give a rundown on what i did and why:

1. ran the scripts linked to above--no results.
2. installed/compiled (/"compiled"--clicked ./zramctl-master/compile.sh with the devx loaded) zramctl, ran the scripts again. --still no results.
3. installed the zram-config_0.5_all.deb pet (actually .1, but i don't think it matters--found the newer package later--none helped with the 32 bit pups i mentioned). ran the scripts again. the output from swapon -s and zramctl (just a maybe for this one) were what they should have been.
4. made pets that fail for select 32 bit pups.

errors in xenial pup 704:

Code: Select all

root# zram.sh start
/root/my-applications/bin/zram.sh: line 33: echo: write error: Invalid argument
mkswap: error: swap area needs to be at least 40 KiB

Usage:
 mkswap [options] device [size]

Options:
 -c, --check               check bad blocks before creating the swap area
 -f, --force               allow swap size area be larger than device
 -p, --pagesize SIZE       specify page size in bytes
 -L, --label LABEL         specify label
 -v, --swapversion NUM     specify swap-space version number
 -U, --uuid UUID           specify the uuid to use
 -V, --version             output version information and exit
 -h, --help                display this help and exit

swapon: /dev/zram0: read swap header failed: No such file or directory
/root/my-applications/bin/zram.sh: line 33: echo: write error: Invalid argument
mkswap: error: swap area needs to be at least 40 KiB

Usage:
 mkswap [options] device [size]

Options:
 -c, --check               check bad blocks before creating the swap area
 -f, --force               allow swap size area be larger than device
 -p, --pagesize SIZE       specify page size in bytes
 -L, --label LABEL         specify label
 -v, --swapversion NUM     specify swap-space version number
 -U, --uuid UUID           specify the uuid to use
 -V, --version             output version information and exit
 -h, --help                display this help and exit

Code: Select all

swapon: /dev/zram1: read swap header failed: No such file or directory
root# # initialize the devices
root# 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
seq: missing operand
Try 'seq --help' for more information.
a zramctll snapshot from tahr64 6.0.5:
see
zramctl main: https://github.com/Railag/zramctl
wiak's thread: http://murga-linux.com/puppy/viewtopic. ... 202#981202
dancytron's script: http://www.murga-linux.com/puppy/viewto ... 747#980747
for more.

zram status via swapon -s and zramctl commands:


Filename Type Size Used Priority
/dev/sda2 partition 8191996 681876 -1
/dev/zram0 partition 720396 192600 5
/dev/zram1 partition 720396 192696 5
Usage: zramctl <name> <size <alg> <threads>
zramctl zram0 1024M lz4 4
zramctl find 1024M lz4 4
zramctl reset zram0 zram1 ...
lzo|lz4 # compress algorithm
*|{K|M|G} # size of zram disk
<name> # zram* or find
# if find, print and setup first free device



help appreciated. here are the pets if anyone wants to mess around with them (small enough (18k) that i could've just attached them here).

zram (info and pets in progress): https://drive.google.com/drive/folders/ ... sp=sharing


in short, i notice an performance improvement in a 2gb ram, 64 bit machine, and i'd like to get it working in a machine that i think would benefit even more from it (32 bit, 2gb asus 1000ha netbook).

thanks, wiak (and backi and dancytron)--this seems to be a really key find for underpowered pups (even if implemented imperfectly--i see no reason to disable the zram, and so the fact that bit doesn't work doesn't bother me).

...after a day or so, the htop reading changed to add the zram to the total swap figure...64 bit machine still seems better for having it….

thanks again.

___

here's a shot of off, on, and info, which has a weak icon even among some pretty terrible company. anyway, apart from the not working in 32bit bit, i'm happy enough with this as the interface (or just dragging the start script to start up (which i haven't tried but should work in one form or another (sometimes dragging a desktop file works, sometimes you have to put a mini script to call something, sometimes that won't work either...)) and skipping a proper interface altogether).

the second green one gets the status reports:
Attachments
2zramctrll.png
(157.15 KiB) Downloaded 930 times

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#11 Post by wiak »

Hi Puppus Dogfellow,

You have obviously put in a lot of work there. Sorry, I can't help right now because I'm also in the middle of something that's taking me days and a lot of energy. Hopefully, you will manage to sort out the issues you are having with zram on 32bit Pup.

May be a while before I finish what I'm doing just now, but I will check back on this thread after that to see if you have everything you want sorted out. But to be honest, that may be a week or more till I get round to that, so hopefully someone else in Pup community will take a look and help you out meantime.

Yes, I agree, zram should certainly improve swap performance for that 32bit machine you describe, and won't do any harm anyway.

wiak

zagreb999
Posts: 567
Joined: Fri 11 Apr 2014, 06:39
Location: Yugoslavija

#12 Post by zagreb999 »

zram is useless...
only

increase, increment of
ram memory is
useful...

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#13 Post by wiak »

I've no idea why you are claiming zram is 'useless' zagreb999. I know very well that it is in fact measurably useful, though additional real RAM addition would be even better of course, if available.

Anyway, personally I am thankful to the developers of zram - this 2GB RAM old computer, running XenialDog, used to crash regularly but now rarely does (and only usually now when I open a few too many browser tabs). Of course I can add some traditional swap too - but once the system starts using that it slows down to a crawl...

All of the 'Swap' shown via free command below is zram-provided swap. The 'free' RAM value can be added to the 'buffer/cache' amount (which can be freed up too - in fact Linux will dynamically free it up as required...) and in addition to that you can add the zram-based swap. The total virtual memory from all that is significantly in excess of the total real 2GB RAM of the machine - hence the machine can, for example, open more browser tabs without crashing...

wiak
Attachments
zram_providing_swap.jpg
(10.62 KiB) Downloaded 829 times

hamoudoudou

Tuto video hongrois by noczak

#14 Post by hamoudoudou »

Puppy Linux teachers no longer will write the lessons on the blackboard. They teach what is ZRAM by screencast, with SimpleScreenRecorder (or Wex, perhaps). Well class is done in magyar , but i understand however that zram is a compressed swap, isn'it ?
I never use Swap..
New ! tuto just realeased for next exam (june)
ZRAM lesson Better you download it.
Attachments
ZRAM.jpg
For Learners (high level)
(29.43 KiB) Downloaded 755 times
Last edited by hamoudoudou on Fri 04 May 2018, 03:20, edited 1 time in total.

User avatar
bigpup
Posts: 13886
Joined: Sun 11 Oct 2009, 18:15
Location: S.C. USA

#15 Post by bigpup »

Furthermore, if you are using such as a usb-flashstick, using zram for swap does not require continual data writes to the flash device, which helps it last longer
.
Is this working the same as the ram disk that Puppy uses when running from a USB flash drive?
Does this conflict with proper operation of the Puppy ram disk?
How Puppy works:
http://bkhome.org/archive/puppylinux/de ... works.html
The things they do not tell you, are usually the clue to solving the problem.
When I was a kid I wanted to be older.... This is not what I expected :shock:
YaPI(any iso installer)

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#16 Post by wiak »

bigpup wrote:
Furthermore, if you are using such as a usb-flashstick, using zram for swap does not require continual data writes to the flash device, which helps it last longer
.
Is this working the same as the ram disk that Puppy uses when running from a USB flash drive?
In short, no. The description ram-DISK (which is nothing to do with zram) basically indicates that a portion of RAM is being used to store programs (as they would be on a disc such as hard drive or cd or dvd or flashdrive, for example). That pseudo-disc stored in RAM is obviously using up some of the RAM for pure 'storage' purposes, but of course some RAM has to be kept left over because actually running a program requires it to be loaded (and uncompressed) into RAM so it can be executed. Problem is big programs executing from RAM may end up wanting more space than RAM is available. Traditional way out of that is to have what is called swap space on external (non-RAM) storage - then VIRTUAL RAM is the size of (total RAM - any ramdisc size) + size_of_swap_space. You probably know that Virtual RAM works by swapping out some actual active executing programs (when they are not actively running) into the swap-space and then loading them back in again when CPU wants to execute them (this is a slight simplification of the process of course). Trouble is the swapping-out and swapping-in process from RAM to external storage is SLOW...

zram is a clever trick, where a portion of the 'free non-Virtual RAM' (i.e. actual RAM) is used as an in-RAM swap space. At first sight that idea seems ridiculous because clearly that zram swap space is using some actual RAM, which would appear to defeat its purpose. However, the 'trick' is that the programs swapped into that zram area are automatically COMPRESSED. The total Virtual RAM will be whatever (actual RAM remains + the zram data-compressing space), which is same formula as conventional swap space. But the difference it that the zram data is highly compressed so effectively Virtual RAM is much bigger since effectively = actual RAM + (zram-size x compression factor), and it's fast cos really it is all in actual RAM (so fast access). So end advantage of using zram is that yes, actual non-zram-swap RAM will be less since some used for zram swap, but the zram swap, being compressed is like having part of the actual RAM magnified in size (without disadvantage of slow access to external storage swap).

Yes, decompression does take CPU cycles (time) but generally that decompression is done much faster than external storage access would have taken.
bigpup wrote: Does this conflict with proper operation of the Puppy ram disk?
How Puppy works:
http://bkhome.org/archive/puppylinux/de ... works.html
No. It's a separate thing. Puppy's methodology, uses some RAM, including simply storing programs inside a pseudo storage disc in RAM (ramdisk), which can certainly be accessed fast since in RAM (but still has to be copied into other part of RAM for actual instruction execution since ramdisk is acting like a storage disc, not like RAM, if you see what I mean...); what is left can be divided into RAM + zram-swap per above description, which gives a much larger overall VIRTUAL RAM in which instructions are (effecively) actually executed (albeit via page swapping in and out of the fast effectively size-magnified compressed RAM-based swap space).

Unfortunately, I've seen comments dismissing zram as not-working, which reveals they have never actually tested its operation. Of course it magnifies effective Virtual RAM memory, which means you can run more processes before machine runs out of virtual RAM space (after which it would crash...). You can test that from a webbrowser by loading memory-hungry pages such as gmail or facebook and opening more and more tabs of these till crash inevitably happens - you will find you can open more such tabs with zram enabled. You can also monitor RAM usage in either 'free' or 'top' utility and see the RAM+zram-swap actually running out. And using zram-swap is MUCH faster than when external slow storage space swap is being used, so it is a win win situation, but of course if you want to run even more processes you also need to have external storage swap, but zram-swap will be used first (once external swap actually starts being used you would notice your system slowing down........ but at least would help prevent actual system crash/freeze...).

wiak

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#17 Post by rufwoof »

wiak wrote:Yes, decompression does take CPU cycles (time) but generally that decompression is done much faster than external storage access would have taken.
The likes of lz4 decompression approaches ram bus speeds such that you can actually achieve the benefits of in effect doubling up the capacity (assuming 2:1 average compression ratio) without sacrificing speed - at least on the read side (write is slower, but still very quick for lz4 compared to many of the alternatives).

Personally I don't run with swap as for one that leaves fingerprints that could potentially be read. For another ... if you start filling up swap especially in the case of the 'average' PC and relative size of most Puppies, likely that would continue (run-away process) anyway and once swap fills the system pretty much locks up the exact same as if there'd been no swap anyway i.e. grinds to a near halt (but just a little sooner without swap compared to if there was swap) such that you have to hard-reset. The exception being if you run large programs/activities such as video editing etc, in which case I just create a swap file interactively for that task. Swap IMO is more appropriate for multi-user server type setups (or if you're running very old hardware with small amounts of ram - when generally the interaction would be too slow for comfort anyway).

s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

#18 Post by s243a »

Can I use zram with swap? So that if I run out of zram then my zram will use swap?

dancytron
Posts: 1519
Joined: Wed 18 Jul 2012, 19:20

#19 Post by dancytron »

s243a wrote:Can I use zram with swap? So that if I run out of zram then my zram will use swap?
Yes. You'll need to check the priority so that it swaps to zram first and then to your regular swap file. Not sure how to change it since mine seems to work out correctly by default, but to check it:

Code: Select all

root@live:~# swapon -s
Filename				                 Type		   Size	   Used	 Priority
/dev/sda3                              	partition	1430524	0	-1
/dev/zram0                             	partition	1152164	0	10
/dev/zram1                             	partition	1152164	0	10

Post Reply