Question regarding RAM compression (zram)

Hi all,:wave:

I have a question regarding zram.

Reading up on the subject on Enabling RAM Compression on Lubuntu - Documentation - Lubuntu Discourse I found out that it might make sense to enable RAM compression (especially on a 4 GB RAM system, as is the case with me).

I don´t want to have it enabled at all times but want to experiment with it first.
Therefore I created the following script ( and made it executable:

#! /bin/bash
modprobe zram
sleep 1
zramctl --find --size=400M
mkswap /dev/zram0
swapon /dev/zram0

So this would create a 400 MB compressed RAM disk, which then acts as a swapfile. :wink:

I first tried it out on my Debian system (vm).
On this system there´s already a swap partition (/dev/vda5):

rosika2@debian ~> sudo swapon -s

Filename				Type		Size	Used	Priority
/dev/vda5                              	partition	999420	0	-2

Now I ran

rosika2@debian ~> sudo /home/rosika2/Dokumente/Ergänzungen_zu_Programmen/zu_RAM-compression/

Setting up swapspace version 1, size = 400 MiB (419426304 bytes)
no label, UUID=72b75a83-3f2a-4bf4-b84f-6ca74f88be0a

I checked again:

rosika2@debian ~> sudo swapon -s

Filename				Type		Size	Used	Priority
/dev/vda5                              	partition	999420	0	-2
/dev/zram0                             	partition	409596	0	-3

So now there are two swap devices active at the same time. Indeed both seem to be in use as I looked it up in top and realized the size-values were added.

Hmm, now I´m a bit confused :thinking: :
How does the system know what device to use if it needs swapping :question:

On my host Lubuntu it´s a bit different:

Here I have no swap partition but a swapfile enabled:

swapon -s

Dateiname                               Typ             Größe   Benutzt Priorität
/swapfile                               file            1048572 0       -2

Now I tried running, which curiously enough failed: :confused:

sudo /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Ergänzungen_zu_Programmen/zu_zram/

zramctl: /dev/zram0: Zurücksetzen fehlgeschlagen: Das Gerät oder die Ressource ist belegt
mkswap: Fehler: der Auslagerungsbereich muss mindestens 40 KiB groß sein
swapon: /dev/zram0: Swap-Header konnte nicht gelesen werden

Sorry, that´s in German.
Rough translation:

zramctl: Reset failed: The device or resource is busy
mkswap: Error: the swap space must be at least 40 KiB
swapon: Swap header could not be read

Then I turned off the use of the existing swap file: sudo swapoff -a

and after that running worked. :+1:

To experiment a bit further:

I deactivated zram:

sudo swapoff /dev/zram0
sudo zramctl --reset /dev/zram0

and re-activated the use of my original swapfile: sudo swapon -a

Now everything was back to its original state.

Out of curiosity I attempted to run again and this time it worked immediately - without having to de-activate my swap partition first. :innocent:
No idea why this couldn´t be achieved the first time but now it worked:

sudo /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Ergänzungen_zu_Programmen/zu_zram/

Auslagerungsbereich Version 1 wird angelegt, Größe = 400 MiB (419426304 Bytes)
keine Bezeichnung, UUID=c327f91e-007f-45f0-9e60-050149fd7482

rough translation again:

Swap space version 1 is created, size u003d 400 MiB (419426304 bytes)
no label, UUIDu003dc327f91e-007f-45f0-9e60-050149fd7482

This is the state in Lubuntu now:

swapon -s

Dateiname                               Typ             Größe   Benutzt Priorität
/swapfile                               file            1048572 0       -2
/dev/zram0                              partition       409596  0       -3

So basically the same as in Debian aside from the fact in Debian there are two swap partitions whereas in Lubuntu there´s a swap file and a swap partition.

So to ask my original question again:

How does the system know what device to use if it needs swapping :question:
The last entry in swapon-s is priority? May this be a clue ?

Thanks a lot for your help.

Many greetings
Rosika :slightly_smiling_face:

Hi Rosika,
Its quite simple. The system will use both swapfiles or partitions

I use 2 swap partitions on 2 separate disks all the time.
When it swaps, it shares the usage across both swap spaces
You can have more than two

It is supposed to be more efficient to have a swap partition, than to use a file.


1 Like

Hi Neville, :wave:

thanks so much for your very quick reply. :heart:

I see.
O.K., that´s good as I don´t have to de-activate one of them first before activating the other.

Still… I see that zram0 has a priority of “-3” whereas the other swap device (or file) has a priority of “-2”.
I´m not sure what that might tell me. :thinking:

That´s the main thing I wanted to know. Thanks a lot, Neville.

Many greetings
Rosika :slightly_smiling_face:

1 Like

Hi Rosika
If you want it to share the two swap areas equally, you should make the two priorities equal

1 Like

Thanks Neville, :wave:

Good idea, I looked it up in the man-pages:

−p, −−priority priority
Specify the priority of the swap device. priority is a value between −1 and 32767. Higher num-
bers indicate higher priority. See swapon(2) for a full description of swap priorities.

Hmm :thinking:

Given the range -1 to 32767 it makes me wonder what the priorities in my example “-2” and “-3” really mean. They´re awfully close together after all.

Never mind, I think it doesn´t matter all that much whether I set both devices/files to -2 or to -3.

Thanks so much for your help, Neville. :heart:

Many greetings
Rosika :slightly_smiling_face:

1 Like

Hi again (after a while), :wave:

I just had a little time left to deal with the subject; so I thought about how I would go about doing so. :thinking:

Taking a look at (the script I employ to get zram started) I see at present it looks like this:

bat /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Ergänzungen_zu_Programmen/zu_zram/
       │ File: /media/rosika/f14a27c2-0b49-4607-94ea-2e56bbf76fe1/DATEN-PARTITION/Dokumente/Ergänzungen_zu_Programmen/zu_zram/
   1   │ #! /bin/bash
   2   │ modprobe zram
   3   │ sleep 1
   4   │ zramctl --find --size=400M
   5   │ mkswap /dev/zram0
   6   │ swapon /dev/zram0


swapon tells me this:

swapon -s
Dateiname                               Typ             Größe   Benutzt Priorität
/swapfile                               file            1048572 107724  -2
/dev/zram0                              partition       409596  0       -3

I know the priority can be set using the “-p” parameter.
My swapfile seems to be always started with a priority of “-2”.

To make the two priorities equal… would it be the correct way to change line 6 in thus:

swapon /dev/zram0 -p -2 :question:

Thus both swap devices would have an equal priority.

Many greetings
Rosika :slightly_smiling_face:


Hi Rosika,
I was trying to understand what you are wanting to achieve.
It would seem that you want to reduce ram usage
There is another technique apart from zram
It is called zswap. It does a slightly different thing. It compresses the memory pages in situ, and by doing so reduces the need to swap pages in and out . That might be better for what you want.


1 Like

I tried out zram some time ago on one of my RPi’s (a 3 or a 4 - not a Zero) - thought I’d set it on my main RPi 4B (4GB) “server” (was running PiHole, but now disabled) OpenVPN and my ssh jumpbox from the wild interwebs into my home LAN (note: - SSH not running on default port and Fail2Ban installed and confgured) - but I can’t see it there, so I must have tried it elsewhere then dispensed with that Raspbian install.

Just about to try it out again, on my shiny new RPi Zero 2 W… 4 cores makes a huge difference, and 512 is “just enough” for a headless machine, but could do with a tad more elbow room, I’ve disabled swap (swapfile) as I don’t want constant writes bringing death to my 512 GB SD card too soon… I’ve read articles where zram is an ideal solution for this scenario (i.e. instead of swap partition, or swapfile, to save from constant writes to SD card).

But anyway - hmmm… I seem to have broken something on the Pi Zero 2W - takes forever to boot up now… Actually seems to NEVER boot up…

Might wipe it and go back to the drawing board I reckon… Then try out zram…

Probably follow this guide here :

Always pays to check cables and stuff (like a previous time where I attempted to trouble shoot my gigabit ethernet with the same dodgy UTP cable, repeatedly ad nauseam) - turns out my PC was having trouble with a daisy chained USB hub - i.e. a powered 7 port USB 2 hub, from my PC (Ubuntu 22), with a USB powered (i.e. red and black for data and power) hub connected to the 7 port powered hub… it was nothing to do with the Pi… So - today I’ll be trying out zram on Pi Zero 2 W… I’ll let you know how I go…

A Raspberry Pi is a good example use/case for a minimal computer that needs some fine tuning to get the most out of it - the same could be said for laptops with 4 GB or less RAM too I guess…

My Pi 4B headless server is just fine and perfectly adequate without needing fine tuning… Just rebooted it and it’s mostly idle and using only 325 MB of its hardwired 4 GB… This one is booting off an external USB3 SSD (512 GB) and it has swap enabled, but, I might disable it - it’s not needed…

OK - now running zram config on my Pi Zero 2W (using that guide link I posted earlier and the author’s github).

root@mimas:~# grep -v "#" /etc/zram-swap-config.conf
root@mimas:~# zramctl
/dev/zram0 lz4         355.9M 21.5M  5.8M  6.5M       4 [SWAP]

At the end of the author’s github for this, they suggest completely removing raspbian swap services :

sudo apt-get remove dphys-swapfile

or disable

sudo systemctl disable dphys-swapfile

I only did the last one (I’d already set it to zero “CONF_SWAPSIZE=0” in /etc/dphys-swapfile).

top - 14:03:21 up 18 min,  3 users,  load average: 0.04, 0.08, 0.08
Tasks: 119 total,   1 running, 118 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.7 us,  0.1 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    474.5 total,    136.9 free,    183.2 used,    154.4 buff/cache
MiB Swap:    355.9 total,    333.9 free,     22.0 used.    226.9 avail Mem

Note - this kinda makes it look like it’s now using more RAM that it was before (previously had about 300 MB free) - but - if I add MiB Mem and MiB Swap :
(note - that “zswap” is not on disk - one of the goals with this is to reduce writes to the SD-Card, prolonging the life of it).

root@mimas:~# echo $((474+356))

I’ve now got more memory available than I started out with - and - with 4 cores - it’s good enough to do the compression / decompression on the fly on demand - I would NOT do this on single core machine… When Pi Zero 2W are more readily available, I may get another one and try out 64 bit Raspbian on it - with zram…

Yeah yeah - I know - e.g. “what’s the point as there’s only 512 MB RAM?” - but - there are PLENTY of arguments for running 64 bit where you can, e.g. bigger words per CPU cycle or whatever - man - imagine someone wondering why you’d bother with a WHOPPING 128 MB RAM on your 32 bit machine - why not keep running 8 bit or 16 bit FFS? Digital and Silicon Graphics had 64 bit machines long before 4+ GB RAM became realistically affordable… I have a pair of mid 1990’s 64 bit (MIPS RISC) SGI Indys in storage, one’s got 64 MB RAM, the other’s got 256, BOTH were running 64 bit IRIX … That argument is like the old chestnut of Microsoft wondering why anyone would EVER need more than 640K of RAM :smiley:


Hi @nevj and @daniel.m.tripp, :wave:

thanks so much for your latest posts and help.


Well, as I pointed out in post #1:

So basically I wanted to take a look at it and see whether zram would be of any help for my system.
Well, I haven´t run into any difficulties so far. So I guess sticking to my present setup with 1GB of swapfile would be o.k.
I more or less wanted to experiment with it.

Yes, you´re quite right, Neville. That would be the ultimate goal. :+1:
Thanks also for the links. :heart:

It creates a new compressed swap volume in the RAM, that is – hierarchically – placed between the RAM and storage devices. Then, when RAM is filled with data, it moves the excess to the compressed swap. When that, too, is filled up, zram starts moving data to the swap partition in the storage drive.

(from maketecheasier )

That´s a pretty good explanation of what zram does.

As I wanted to know whether my command (i.e.setting the priority for zram is correct I tried it the manual way:

sudo modprobe zram
sudo zramctl --find --size=400M
sudo mkswap /dev/zram0
sudo swapon /dev/zram0 -p -2

I know the existing swapfile has a priority of “-2”. So I checked:

swapon -s
Dateiname                               Typ             Größe   Benutzt Priorität
/swapfile                               file            1048572 11264   -2
/dev/zram0                              partition       409596  0       -3

Hmm. that´s odd. The setting of zram ´s priority seems to have been ignored. It´s still listed as havning a priority of “-3”.
No idea what´s going on. :thinking:

Perhaps this one might be a clue:

This new swap device [zram] is assigned a higher priority than the ones that reside in the PC’s slower actual storage, to be used before them.

Well, in my case zram has a priority of “-3”. Wouldn´t that be a lower priority :question:

Zswap is more complicated than zram.

Oh dear. :slightly_frowning_face:

[ZSwap] first compresses everything that doesn’t fit in the actual RAM. If the data is compressed, zswap stores it in RAM. If it isn’t, it stores it to the existing swap.

It´s a bit hard to follow for me. :blush:

First the authors says zswap compresses everything…
Then he makes a distinction: If the data is compressed … and if it isn´t. That seems to elude my mental grasp. Sorry. :neutral_face:

Since it relies on the existing swap, zswap is better for Linux installations on every PC, especially those with less than 4GBs of RAM.

… which theoretically sounds good.

For enabling zcache I found a good article here: .
I might try that in my Debian VM.

Thanks a lot. Neville. :heart:


Thanks to you as well. Dan, and for providing the link.:heart:

I realize you have a new signet picture again. :+1:

Seems to make perfect sense.

Thanks also for your detailed account of what you came up with.
Perhaps I should take a closer look at zram-config-
Seems to provide a better solution for dealing with the priority problem I have encountered (SWAP_PRI).

Thanks to both of you and many greetings
Rosika :slightly_smiling_face:

1 Like

Hi, :wave:

although I haven´t found a solution for setting the priority for zram I still found out the following:

The assignment of priority values seem to be dependent on the start sequence of swap devices:

I currently had just zram activated with a size of 1GB.
Now I activated the swapfile as well: sudo swapon -a.

As can be seen now zram has the higher priority:

swapon -s
Dateiname                               Typ             Größe   Benutzt Priorität
/dev/zram0                              partition       1048572 67840   -2
/swapfile                               file            1048572 0       -3

For the sake of comparison: It was the other way round when activating zram after the swapfile: :wink:

swapon -s
Dateiname                               Typ             Größe   Benutzt Priorität
/swapfile                               file            1048572 107724  -2
/dev/zram0                              partition       409596  0       -3

Many greetings from Rosika :slightly_smiling_face:


maketecheasier says about zram:

when RAM is filled with data, it moves the excess to the compressed swap. When that, too, is filled up, zram starts moving data to the swap partition in the storage drive.

This new swap device is assigned a higher priority than the ones that reside in the PC’s slower actual storage, to be used before them.

When setting up zram after swapfile is already running I see that it´s still (only) swapfile that is continually used… :thinking:

1 Like

Hi @Rosika ,

Another nice post / thread. I learn a little about zram. I also did not know a linux user could have a swap file. I had always put aside a small partition for swapping.
I was just wondering what cause you to think you had a swapping problem? Did you run some reports that showed how much swap space was being used or show what the average swap rate was over some period of time?


Hi Howard, :wave:

thanks so much for your friendly post and the compliment. :heart:

if there is no swap and the RAM is full, the Linux kernel terminates programs at its own discretion in order to free up RAM.
This usually results in data loss.

To prevent exactly this, Ubuntu (like all other distributions) creates a swap area (storage area) by default during installation. Newer Ubuntu versions use a swap file for this.

If you are still using Xenial or have upgraded from an older Ubuntu version, a swap partition is used instead of the swap file.

(source: Swap › Wiki › )
(translated from German, bold by me)

At the time this was new to me as well. In the meantime I´ve grown accustomed to using a swapfile. :blush:

Oh, actually I haven´t got any problems regarding swap. I was intrigued by the article (from Lubuntu Discourse) I referred to in my post #1 and wanted to experiment with zram.

That said I still have to confess it was just recently I ran into RAM-related problems when
using a WIN programme (for my tax declaration) in playonlinux/wine.

As the previous version of this tax-programme is listed with the “silver” status (WineHQ - Steuer 2020) it was to be expected that not every functionality would be implemented perfectly.

I inadvertently clicked on a certain button (shouldn´t have done that :frowning_face: ) and my RAM (4 GB) as well as the swapfile were soon filled to the brim.

Well, I guess in this particular case zram or zswap wouldn´t have been of significant help either… :thinking: .

No, actually not. But I´d be interested to learn how this could be achieved.

Thanks again and many greetings.
Rosika :slightly_smiling_face:

Hi Howard,
I think it is actualy more efficient to use a swap partition , than a swap file. Using the filesystem to swap adds overhead.
I think swap file was intended as a convenience thing. I dont know why Ubuntu would have changed recently to setting up a swap file on install, as @Rosika says. Most distros do not do that, they ask you to make a swap partition.
Most modern computers rarely swap , so it does not matter much today. It was a different matter 20 years ago when memory was measured in megabytes.


Countless tests have shown that the difference of performance between the two methods is mostly unnoticable. So, it’s more of a matter of personal preference. I personally prefer a swap partition.

However, swap files became more popular, because they are more flexible. With partitions, you have to plan ahead and do it right from the beginning. Whereas with files, you can just change it any time, all the time.


I VASTLY prefer the simplicity of using a swapfile, over a swap partition…

I can disable a swapfile at any time… I can create a new one, I can add an additional one - and I don’t have to mess about with partitions or think about “scale” when I’m deploying a machine…

Micromanaging storage is one of the greatest bugbears I have with linux systems - where some “architect” (inverted commas implies sarcasm) has decided that a 2 GB swap partition is adequate, and 4 GB for a separate /var partition is all anyone will ever need… Maybe I shouldn’t complain, as such inane decisions keep me in my job - but jeeze - storage is DIRT CHEAP these days, and ALWAYS allocate MORE than you think you’ll need: storage is VASTLY cheaper than a sysadmin’s 6 figure salary - AT DOUBLE time!

Note given that probably 95% of ALL the linux systems running “the cloud” are virtualised, the virtual system doesn’t have access to the spindles and platters of the backend storage (that’s mostly the hypervisors job, sometimes that’s even abstracted too - e.g. on a NAS), where or how to implement swap space is mostly moot, a swapfile makes things more flexible from a system administration perpsective.


Hi, :wave:

If I understand it correctly zswap might still rely on an additional swap device


[…]instead of moving memory pages to a swap device when they are to be swapped out, zswap performs their compression and then stores them into a memory pool dynamically allocated in the system RAM.
Later, writeback to the actual swap device is deferred or even completely avoided, resulting in a significantly reduced I/O for Linux systems that require swapping

(source: wikipedia )

Maybe a stupid question by me, but I´d like to ask it anyway. :blush:

As zram

provides a compressed block device using RAM for storing data, which acts as a regular and separate swap device.

… would it theoretically be possible to use zram and zswap together (at the same time) :question:

I´m sure with my 4 GB RAM that wouldn´t much sense. Therefore my theoretical question. :blush:

Many greetings
Rosika :slightly_smiling_face:

Hi @nevj,

Yes, I have a long history with swapping and swap partitions. I actually started my career back when the mainframe / computer did not swap out memory. If the program did not fix into the 32k of memory (actually less because the OS ran in the first 10K) you had to either remove some code or split the program into two different jobs. Back when I first hear about a swap partition, we called it ‘paging’.


Hi Rosika,
Yes indeed it does. It could be a swapfile or a swap partition, but it is needed.
I dont know if you could combine zram and zswap and make the swap device compressed ram. That may not make sense anyway.
I think @daniel.m.tripp’s reply is very much to the point. Efficiency is a minor issue, do what is convenient. @Akito says the same.

1 Like

As an extension to this topic, I just wanna throw in how DietPi handles low I/O logging options.

It’s not directly related to swap, but it’s a very similar topic, because it essentially tackles the issue of reducing writes to the storage medium.


Note : this is purely anecdotal - but my general impression is that the Pi Zero 2W boots considerably faster with zram enabled, than otherwise… Note here we’re talking about the 2W not the “W”… the 2W is 64 bit capable (although I’m running 32 bit), has 4 cores… the plain W and it’s predecessor the first gen Pi Zero, were 32 bit armhf / armel and had a single core…

I’m quite impressed with this funky little feature… If I was still running Pi-Hole on my Pi4 headless server, I’d probably enable it on there too - however - as it’s headless, and pretty much only does a few things as daemons, it barely touches the sides of the 4 GB hardwired (and swap is disabled).

And I’m quite impressed with the Pi Zero 2W itself - so much packed into a board the actual size of a credit card. Note : IT CHEESES ME off when people talk about the original Pi, and 2 and 3 and 4, and a range of other SBC’s being “credit card” sized - they’re NOT - imagine having something that big in your wallet? No! I can’t either…