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

#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