Question regarding RAM compression (zram)

Hi all, :wave:

thanks for your latest comments.

@nevj:

O.K. Good to know.
So I just have to watch out not to run my rampak.sh for zram if I go for zswap (which will then be active by default). :wink:

@Akito:

Thanks for the link. IĀ“ll look into it.

@daniel.m.tripp:

Sounds interesting.
In this scenario: do you have zram set as your sole swap device or do you have a swap partition/swapfile available in addition to that?

Many greetings to you all.
Rosika :slightly_smiling_face:

1 Like

Just a thought, but by taking memory (ram) away to assign it to zram gives less ram to the OS which in turn could cause more swapping.

Itā€™s a good idea, though it depends on the situation. If your OS truly uses all the RAM, then yes, this could bear its own issues. However, if a fraction of the available RAM is usually reservered, though not ā€œin useā€, then it shouldnā€™t cause such issues, I think.

2 Likes

That is true. But if you are not using all your ram, is there really a need for zram?

1 Like

Not sure. Maybe, if you are highly optimising your situation, you might do something even smarter than what the computer does. Iā€™m not sure about that.

The amount of ram used varies dynamically, depending on what software the user is running.
There can be times when it runs out of ram and needs to swap, even when the OS only uses a small fraction of ram when idle.
My main PC has 64Gb of ram, and I have succeeded in making it swap by running one very large process. So I favour a swap partition, but everyones situation is different.

1 Like

This makes my eyes tear, considering it mainly has to run Void. Itā€™s like driving a Porsche on plastic wheels.

1 Like

That was Debian. It is multiboot. I keep 300Gb in swap partitions

No - have disabled swapā€¦ part of the reason for the exercise, was to avoid swap, itā€™s slow, and it wears out storage (in this case SD Card which have a finite number of writes - SSDā€™s are the same - finite number of writes).

I often disable swapā€¦ Iā€™m not using it on my 32 GB Ryzen desktop machine, Iā€™m not using it on my RPi4Bā€¦ I donā€™t know where, or care, to know, where / how MacOS does swap (but both only have 8 GB RAM)

Iā€™ve never hit an issue where Iā€™ve run out of RAM on a 16 GB machinesā€¦

I can see where Iā€™m running a massive database or something - sure - Iā€™ll need swapā€¦ But I donā€™t with my desktop machine, or the headless Pi 4 server, or my headless Pi Zero 2Wā€¦

Damn - Iā€™ve got a pair of unused 16 GB DDR4 (and 4 x 4 GB DDR4) - but - I recently upgraded my RAM to much higher clockspeed, and it does make a difference, Iā€™m not going to hobble that advantage by adding much slower RAM just to get to 64 GB RAMā€¦ I guess I should try and sell it but Iā€™m incredibly lazy and just canā€™t be arsedā€¦

If this machine was running lots and lots of VMā€™s yeahā€¦ Iā€™d want 64 GB or moreā€¦

But 32 GB is plenty for my desktop experience - and - my AMD GPU has 8 GB DDR5ā€¦ so in essence Iā€™m running with 40 GB of RAMā€¦

1 Like

Hi all, :wave:

Thanks @nevj for your insights.

Yes, I think I experienced something like this in the past as well :wink:

Thanks @daniel.m.tripp for the info.

That certainly makes a lot of sense :+1:

@Akito:

Just out of curiosity:

When you say ā€œIf your OS truly uses all the RAMā€ ā€¦
ā€¦ do you mean the ā€œpureā€ OS itself, i.e. after reboot/fresh boot without starting anything by the user

or:

the OS with applications running, like firefox etc. :question:

Many greetings
Rosika :slightly_smiling_face:

1 Like

Iā€™m talking about RAM usage in general. It is really hard to distinguish what is using how much. You can look in htop and see numbers, but itā€™s still not the entire truth always. For example, if a program uses 10MB, it does not sound like much. But what if that program starts 100 such processes? (Which is entirely normal.) Then you have to multiplay 10*100MB, plus account for other overhead created by so many processes, etc.
Or what if you run a script? Do you count in the interpreterā€™s RAM usage or only what the script itself computes?

Itā€™s all not so easy. This is another reason, why I would recommend against looking too extremely into precise RAM numbers and then changing everything based on a couple of RAM numbers.

One of the stupidest things we see a lot are minimalism fanatics, who open free -h once for a second, look at the numbers and say ā€œah, this OS is using too much RAM, why does it use so much RAM, I need something more minimal!!!ā€. This is just bad and does not reflect reality, at all.
Yes, free shows RAM usage, but you also have to know how those numbers are created. What they mean. If they are actually that high or if it just feels high, because of things the user looking at it does not know.

So, I would recommend taking any RAM usage number with a big ball of salt. Itā€™s best to look at it in ballparks.
Are you over 10GB usage? Are you under 20 GB of usage? Or do you only use 4GB of RAM?
These are more appropriate questions, because they do not focus too much on precise numbers, which are often not as bad as they seem.

2 Likes

Thanks a lot @Akito for your very detailed account. :heart:

Right. Thanks.

ThatĀ“s certainly good advice.
In actual fact most of the time IĀ“m happy with my 1GB swapfile as it is.
Having 4 GB of RAM available on my system the article I was referring to in post #1 (ā€œEnabling RAM Compression on Lubuntuā€) got me intrigued ā€¦ but rather out of curiosity. :blush:

I still have to look into @nevj Ā“s suggestion of trying zswap instead.

Quite. Thanks. :+1:

O.K. I ask myself this questionā€¦ and can answer: Yes, I use 4GB of RAM.
To be more precise: IĀ“ve got 4GB of RAM available.

Thanks so much @Akito .

Many greetings from Rosika :slightly_smiling_face:

2 Likes

I think thereā€™s some confusion here - I donā€™t think zswap is stored on disk (neither is zram)ā€¦

zswap is a virtual filesystem in RAM, with on the fly compressionā€¦ itā€™s a ā€œcacheā€ for swap on disk storageā€¦

i.e. its sorta / kinda like a ā€œramdiskā€ maybe? Anyway - I guess it can also save some read/write I/O as a compressed cache - AFAIK the ā€œmemcacheā€ in the Linux kernel isnā€™t compressed - but I could be wrong.

Neither zswap or zram actually give you more RAM, but zram comes closer to that goal than zswap maybe?

I also fondly remember MS-DOS days - when you might have a whopping 8 MB of RAM, and an O/S that only went as ā€œhighā€ as 640 K (whoā€™d ever want or need more?), and you might have a game that might take advantage of some memory extenions (e.g. himem.sys up to 1024 kb) but would NEVER need anywhere near 8 MB, and the whole game EXE and libraries were like less than 4 MB - youā€™d setup a 4 MB RAM disk, copy the game binaries there, and run it off RAM disk - instead of running it off like 1200 RPM piece of crap magnetic hard driveā€¦

Here :

Though, as a result of its design, at least one already existing swap device is required for zswap to be used.[15]

3 Likes

Thanks, Dan, for the clarification. :heart:

O.K. I guess that sets it apart from zram then, which functions as a compressed RAM disk .

Many greetings from Rosika :slightly_smiling_face:

1 Like

Sorry to be pedantic but : compressed RAM swap diskā€¦ :smiley: :heart: :smiley: have a nice day

3 Likes

Hi Dan, :wave:

thanks for putting my statement right. :heart:

I guess I was still hanging on to the Lubuntu article in which the author put it like that:

RAM compression works by turning a significant portion of your systemā€™s RAM into a compressed RAM disk.

ā€¦ and the 4th step in rampak.sh is:

mkswap /dev/zram0

Sets up the RAM disk to act as a swapfile.

So you have every right to be ā€œpedanticā€ :wink:

Thanks again and many greetings.
Rosika :slightly_smiling_face:

3 Likes

Hi all, :wave:

I finally found time to enable zswap as per @nevj Ā“s recommendation (post #7).

As usual when trying out something new I did it in my Debian VM. :wink:
So the respective line in /etc/default/grub looks now thus:

GRUB_CMDLINE_LINUX_DEFAULT="quiet zswap.enabled=1"

After performing sudo update-grub I rebooted and it seems to work/be active.
Following the instructions on

I got these results:

sudo dmesg | grep zswap
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.19.0-21-amd64 root=UUID=67f90c65-a50f-472d-bda7-1abeb5d01e52 ro quiet zswap.enabled=1
[    0.126462] Kernel command line: BOOT_IMAGE=/boot/vmlinuz-4.19.0-21-amd64 root=UUID=67f90c65-a50f-472d-bda7-1abeb5d01e52 ro quiet zswap.enabled=1
[    1.455076] zswap: loaded using pool lzo/zbud

and


sudo grep -R . /sys/kernel/debug/zswap
/sys/kernel/debug/zswap/same_filled_pages:0
/sys/kernel/debug/zswap/stored_pages:0
/sys/kernel/debug/zswap/pool_total_size:0
/sys/kernel/debug/zswap/duplicate_entry:0
/sys/kernel/debug/zswap/written_back_pages:0
/sys/kernel/debug/zswap/reject_compress_poor:0
/sys/kernel/debug/zswap/reject_kmemcache_fail:0
/sys/kernel/debug/zswap/reject_alloc_fail:0
/sys/kernel/debug/zswap/reject_reclaim_fail:0
/sys/kernel/debug/zswap/pool_limit_hit:0

Well, as I havenĀ“t done anything noteworthy after rebooting zswap didnĀ“t have much to do, I guess :wink:.

The key parameters to look out for are stored_pages which is the number of compressed pages and written_back_pages which is the number of pages which have been written out to the swap file.
(source: see above)

So IĀ“ll keep that in mind.

Many thanks and many greetings
Rosika :slightly_smiling_face:

1 Like

Hi Rosika,
These things take time. Do not ever feel compelled to try something, just because I commented on it.
You need to make it work hard and see if zswap does the right thing
Might hive it a try in Debian in my small desktop. It has only 8Gb so I can make it swap easily
Regards
Neville

1 Like

Hi Neville, :wave:

thanks a lot.

ThatĀ“s really alright. IĀ“m very glad of any comments posted by you. :+1:

Yes, thatĀ“s perhaps the hardest part - evaluating whether zswap does the right thing.
Perhaps IĀ“ll make it work on my host system (Lubuntu 20.04.5 LTS) and try it there.
ItĀ“s my daily driver after allā€¦

I have just 4 GB RAM on Lubuntu. So swapping incidences should occur even more frequently I guess.

Many greetings
Rosika :slightly_smiling_face:

1 Like

Hi again Neville, :wave:

just one additional question before I try setting up zswap on Lubuntu.

In the past I used to manually clear my swapfile (every now and then) - for no particular reason actually - with sudo swapoff -a and then switching it on again with sudo swapon -a.

I wonderā€¦
If I have zswap enabled by default - what would be the consequences of temporarily turning off my swapfile :question:
I mean zswap still depends on a swap partition or swapfile after allā€¦ :thinking:

Many greetings from Rosika :slightly_smiling_face: