User avatar
Botspot
Posts: 1911
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas

Increase usable RAM on your Raspberry Pi with this new script (ZRAM swapdrive)

Fri Jan 07, 2022 5:57 pm

Hi forum! I don't post here often, but I am still very involved in the larger Raspberry Pi world. Today I am pleased to announce the arrival of the 163th app available in Pi-Apps. This app is called More RAM, and its goal is to eliminate RAM-related freezes on the Raspberry Pi.
On default Raspberry Pi OS, if a program consumes all of the available RAM, the entire system will slow down, or even freeze. Once that happens, you have to pull the plug, lose any unsaved work, and risk SD-card corruption. It has happened to me, and I know that it has happened to you too.

But this was not how Linux was designed! When free RAM runs out, the kernel is supposed to kill memory-intensive processes. But on Raspberry Pi OS, this rarely happens. Result: frequent freezes, especially on Raspberry Pi models with less than 2GB of RAM. Most people think the only solution is to buy a more expensive Pi, but you can usually get by with a simple tweak.

This app enables and configures a ZRAM swapdrive. ZRAM is a kernel module that compresses data on-the-fly and stores it in RAM. It combines RAM's high speed with compression. This makes it perfect for use as Swap - spare memory for the Linux kernel to use for when available RAM is in short supply.
A standard Swapfile/Swap-partition is slow because the data must pass through a bottleneck, like the SD card or the USB interface. But with ZRAM, the data never passes through a bottleneck.
Using the zstd compression algorithm, I've seen it reduce the size of a piece of data by a factor of five. While the contents of RAM is rarely that compressible, it can routinely be reduced to a third of its size. In other words, ZRAM can effectively triple the amount of RAM on your system.
And that's exactly what this app does - it enables and configures ZRAM to make the most of your RAM.

While writing the script, I wanted to see just how well it would work. My Pi has 4GB of RAM, and from past experience I know better than to open 50+ tabs in Chromium. So, with ZRAM enabled, I tried opening all of my browser bookmarks at once. In total, over 200 tabs were open in Chromium, (many of them Facebook, YouTube, and other heavy sites) and the system still did not freeze. I checked my system monitor to see how much ZRAM-swap was in use:
2022-01-06-161430_1920x1080_scrot2.png
2022-01-06-161430_1920x1080_scrot2.png (4.55 KiB) Viewed 18183 times
Chromium was using 7GB of memory, yet my Pi has only 4GB of RAM. I wasn't using any conventional Swap, and yet the system was still responsive!
If that's not impressive, I don't know what is. :lol:

Thanks to Pi-Apps, it's incredibly easy to try it out yourself. If you decide you don't like it, it takes just one click to remove it.
  • To install Pi-Apps:

    Code: Select all

    git clone https://github.com/Botspot/pi-apps
    ~/pi-apps/install
  • To install the More RAM app:

    Code: Select all

    ~/pi-apps/manage install 'More RAM'
    You can try running the script separately from Pi-Apps, but then you would miss out on any updates or bugfixes.
  • Also it's a good idea to read the description for more details.

    Code: Select all

    ~/pi-apps/gui 'More RAM'
  • If you're new to Pi-Apps, keep in mind that there are many other useful apps in there. You can launch Pi-Apps by going to the Start Menu -> Accessories -> Pi-Apps.
Some of you may be wondering how this compares to Novaspirit's ZRAM tutorial from a few years back. Short answer: mine is much better.
  • Novaspirit created one /dev/zram device per core, but only one is necessary because ZRAM is multithreaded. These unnecessary devices actually harmed performance.
  • Novaspirit used LZ4 compression, while this app uses the new ZSTD compression algorithm. ZSTD can reach a 5:1 compression-ratio, while LZ4 rarely exceeds 2.5:1.
  • Novaspirit didn't change any kernel parameters, like swappiness or cache_pressure, so ZRAM didn't end up being nearly as effective as it could have been.
  • Novaspirit didn't disable the dphys-swapfile service, which would now be unnecessary as it's never used.
  • Novaspirit used the legacy rc.local autostart method, whereas my script uses a systemd service.
Feel free to comment below! I'd love to see how well ZRAM works for you. If you have any problems with the 'More RAM' app, feel free to ask below. I can also be contacted on Discord and you can open an issue on Github.
That's all for now. Have fun! :)
Last edited by Botspot on Fri Jan 07, 2022 11:41 pm, edited 2 times in total.
Easily install anything on your Pi using Pi-Apps. Over 2 million users.
Over 200 apps including image editors, IDEs, games, web browsers, appearance themes, and more to upgrade your Raspberry Pi desktop.
More info: https://github.com/Botspot/pi-apps

W. H. Heydt
Posts: 16567
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 6:12 pm

That doesn't actually increase the RAM on the Pi. It just reallocates how the RAM is used. Thread title is misleading.

DarkElvenAngel
Posts: 2952
Joined: Tue Mar 20, 2018 9:53 pm

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 6:26 pm

I don't know the title reminds me of the 90's software trends like Double Disk double your disk disk space without the expensive upgrade.

There was a bunch of Ram related ones too memory managers with promises of more free memory.

At least in the case of ZRAM it works I used it for chromium on at Pi Zero 2W.

LTolledo
Posts: 6657
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 6:27 pm

the topic title kind of reminds me of "SoftRAM" for the PC
https://www.youtube.com/watch?v=8rxssVFeKr8
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

User avatar
Botspot
Posts: 1911
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 6:30 pm

W. H. Heydt wrote:
Fri Jan 07, 2022 6:12 pm
That doesn't actually increase the RAM on the Pi. It just reallocates how the RAM is used. Thread title is misleading.
While no script can literally add capacity to the physical RAM chip, this script is able to increase how many bytes of Random-Access-Memory can be stored in the same available space.
So, in layman's terminology, this script increases RAM.
While that statement may be slightly inaccurate, I believe it's better than a cryptically-precise title like: "Shell script to configure dynamic, on-the-fly RAM-compression using ZRAM-swap"
Remember, the Raspberry Pi computer is supposed to be child-friendly and approachable. For people who are just getting into programming, it's important to keep things simple, at least at first. For those who like getting into the weeds and understanding how this all works, I've placed several links to articles explaining ZRAM, in the app's installation script. Also the app's description includes an accurate explanation of ZRAM and how it works.
Easily install anything on your Pi using Pi-Apps. Over 2 million users.
Over 200 apps including image editors, IDEs, games, web browsers, appearance themes, and more to upgrade your Raspberry Pi desktop.
More info: https://github.com/Botspot/pi-apps

W. H. Heydt
Posts: 16567
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 7:38 pm

Botspot wrote:
Fri Jan 07, 2022 6:30 pm
W. H. Heydt wrote:
Fri Jan 07, 2022 6:12 pm
That doesn't actually increase the RAM on the Pi. It just reallocates how the RAM is used. Thread title is misleading.
While no script can literally add capacity to the physical RAM chip, this script is able to increase how many bytes of Random-Access-Memory can be stored in the same available space.
So, in layman's terminology, this script increases RAM. While that statement may be slightly inaccurate, I believe it's better than a cryptically-precise title like: "Shell script to configure dynamic, on-the-fly RAM-compression using ZRAM-swap"
The Raspberry Pi computer is supposed to be child-friendly and approachable. For people who are just getting into programming, it's important to keep things simple at first. For those who like getting into the weeds and understanding how this all works, I've placed several links to articles on ZRAM, in the app's installation script.
We're going to have to agree to disagree. Claiming (as your thread title does) that it is an easy way to increase the RAM on a Pi is a flat-out false statement. As you agree above, the amount of RAM doesn't change. How it is USED changes, by using a compression algorithm to make more efficient (for some types of efficiency...but not all of them) use of what RAM there is.

User avatar
Botspot
Posts: 1911
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 7:42 pm

W. H. Heydt, would you like to propose a more accurate title that is both expert-friendly and beginner-friendly?

LTolledo
Posts: 6657
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 7:56 pm

how about "Easily Optimize RAM use on your Raspberry Pi"? ;)
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

User avatar
rpdom
Posts: 23522
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 7:58 pm

Botspot wrote:
Fri Jan 07, 2022 6:30 pm
While no script can literally add capacity to the physical RAM chip, this script is able to increase how many bytes of Random-Access-Memory can be stored in the same available space.
So, in layman's terminology, this script increases RAM.
While that statement may be slightly inaccurate, I believe it's better than a cryptically-precise title like: "Shell script to configure dynamic, on-the-fly RAM-compression using ZRAM-swap"
Remember, the Raspberry Pi computer is supposed to be child-friendly and approachable. For people who are just getting into programming, it's important to keep things simple, at least at first.
Ah, so a case of "lies to children".
Unreadable squiggle

W. H. Heydt
Posts: 16567
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 7:59 pm

Botspot wrote:
Fri Jan 07, 2022 7:42 pm
W. H. Heydt, would you like to propose a more accurate title that is both expert-friendly and beginner-friendly?
The one LTolledo suggests isn't bad. I would note that beginners are probably not well advised to use a RAM compression package in the first place, though.

User avatar
TimG
Posts: 334
Joined: Tue Apr 03, 2012 12:15 am
Location: Switzerland

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 8:04 pm

Botspot wrote:
Fri Jan 07, 2022 5:57 pm
When free RAM runs out, the kernel is supposed to kill memory-intensive processes. But on Raspberry Pi OS, this rarely happens.
Could you expand on that a bit? Why aren't RAM-hogging processes on the Pi killed as normal?

User avatar
Botspot
Posts: 1911
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 8:09 pm

LTolledo wrote:
Fri Jan 07, 2022 7:56 pm
how about "Easily Optimize RAM use on your Raspberry Pi"? ;)
I think that would incorrectly imply that this script can reduce (optimize) the memory-footprint programs have. But that's not what is happening - ZRAM can't debloat Chromium. ;)
Easily install anything on your Pi using Pi-Apps. Over 2 million users.
Over 200 apps including image editors, IDEs, games, web browsers, appearance themes, and more to upgrade your Raspberry Pi desktop.
More info: https://github.com/Botspot/pi-apps

User avatar
Botspot
Posts: 1911
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 8:10 pm

rpdom wrote: Ah, so a case of "lies to children".
I wouldn't go that far. Because ZRAM increases the memory-capacity, then one could say that ZRAM is increasing your RAM, even though technically RAM is a hardware term.
It really depends on perspective - if you look at it from the software/usability side, then the statement is true. But if you look at the hardware side then the statement is false.
Easily install anything on your Pi using Pi-Apps. Over 2 million users.
Over 200 apps including image editors, IDEs, games, web browsers, appearance themes, and more to upgrade your Raspberry Pi desktop.
More info: https://github.com/Botspot/pi-apps

User avatar
Botspot
Posts: 1911
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 8:17 pm

TimG wrote:
Botspot wrote:
Fri Jan 07, 2022 5:57 pm
When free RAM runs out, the kernel is supposed to kill memory-intensive processes. But on Raspberry Pi OS, this rarely happens.
Could you expand on that a bit? Why aren't RAM-hogging processes on the Pi killed as normal?
I don't know why the OOMkiller rarely works on RPiOS, but I'm not the only one to experience a RAM-related frozen screen.
When OOMkiller has worked, it's usually a scenario like this: When web-browsing, I watch helplessly as the RAM-monitor spikes to 100%. The screen freezes, but a few seconds later it's responsive again. Then I notice that a few Chromium tabs have crashed (due to the OOMkiller)
Most of the time however, the frozen screen never recovers and after an hour I finally give up waiting and pull the plug. Those are the majority of cases, and plenty of other people have reported similar incidents.

I often wonder why RPiOS doesn't ship with a RAM-monitor in lxpanel by default. It used to, but now with Stretch, Buster, and Bullseye you have no clue that the RAM is about to max out, until it's too late. For that matter, I wonder why RPiOS doesn't use ZRAM by default - plenty of other embedded OS'es do.
Easily install anything on your Pi using Pi-Apps. Over 2 million users.
Over 200 apps including image editors, IDEs, games, web browsers, appearance themes, and more to upgrade your Raspberry Pi desktop.
More info: https://github.com/Botspot/pi-apps

LTolledo
Posts: 6657
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 8:22 pm

"optimize" does not really mean to reduce.....

....well at least its try and use for free....
unlike the other "vintage programs" stated above that required purchase..... thus were called "scam"
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

User avatar
Hafenzoll
Posts: 5
Joined: Sat Oct 21, 2017 12:55 am
Location: Northwestern Germany (Lower Saxony)

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 8:25 pm

This is an irritating thread.

If you want to flush the

Code: Select all

buff/cache
(buffer/caching) simply deploy :

Code: Select all

sync; echo 1 > /proc/sys/vm/drop_caches

Tested with RasPi 2B+ and RasPi 4B.
___________________________________________________
https://dosboot.org/

memjr
Posts: 3823
Joined: Fri Aug 21, 2020 5:59 pm

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 8:56 pm

What kind of hit to cpu perfornace does compressing and decompressing ram on the fly causes?

User avatar
Botspot
Posts: 1911
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 9:11 pm

memjr wrote:
Fri Jan 07, 2022 8:56 pm
What kind of hit to cpu perfornace does compressing and decompressing ram on the fly causes?
Correct, compressing and decompressing RAM will use CPU cycles. I haven't found a good way to measure it, but I think this is being looked at the wrong way.
Some people think that with ZRAM enabled, the entire contents of RAM is being compressed all the time. But that's not true. When ZRAM is unnecessary, it remains dormant and uses basically zero resources. Of course, when ZRAM is necessary, then it will use some CPU cycles, but without it, the system would have either locked up, or the OOMkiller would have been deployed.

In other words, I cannot think of a single situation when you wouldn't want this in place, functioning as backup memory for times when it's necessary.
Easily install anything on your Pi using Pi-Apps. Over 2 million users.
Over 200 apps including image editors, IDEs, games, web browsers, appearance themes, and more to upgrade your Raspberry Pi desktop.
More info: https://github.com/Botspot/pi-apps

DarkElvenAngel
Posts: 2952
Joined: Tue Mar 20, 2018 9:53 pm

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 9:20 pm

You could say
Easily increase usable RAM on your Raspberry Pi
Or
Easily increase available RAM on your Raspberry Pi
Or
Easily expand usable RAM on you Raspberry Pi
Just some ideas. You can add the * does not add more physical RAM if you feel the need. It's just memory management and compression.

hippy
Posts: 16116
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 9:23 pm

Botspot wrote:
Fri Jan 07, 2022 8:10 pm
Because ZRAM increases the memory-capacity ...
Funnily enough, I'm happy with your title, but for those who aren't I was going to say a better one might be "Increase your RAM capacity on a Pi".

What would be interesting to see is how this fares on a Zero Two W, whether it can bump that up to make it suitable for desktop use, browsing etc. Edit : Missed this in first read of the thread - viewtopic.php?p=1958703#p1958703

User avatar
Botspot
Posts: 1911
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas

Re: Easily increase usable RAM on your Raspberry Pi with this script (ZRAM swapdrive)

Fri Jan 07, 2022 9:43 pm

Title has been changed. Hopefully this one will appease both the experts and the beginners. ;)
Easily install anything on your Pi using Pi-Apps. Over 2 million users.
Over 200 apps including image editors, IDEs, games, web browsers, appearance themes, and more to upgrade your Raspberry Pi desktop.
More info: https://github.com/Botspot/pi-apps

cleverca22
Posts: 8824
Joined: Sat Aug 18, 2012 2:33 pm

Re: Easily increase RAM on your Raspberry Pi

Fri Jan 07, 2022 9:54 pm

Hafenzoll wrote:
Fri Jan 07, 2022 8:25 pm
This is an irritating thread.

If you want to flush the

Code: Select all

buff/cache
(buffer/caching) simply deploy :

Code: Select all

sync; echo 1 > /proc/sys/vm/drop_caches
Tested with RasPi 2B+ and RasPi 4B.
but that is rather pointless
because linux will automatically discard those caches whenever its low on ram
and forcibly clearing them will make the system slower, because it has to read from the SD card again

DarkElvenAngel
Posts: 2952
Joined: Tue Mar 20, 2018 9:53 pm

Re: Increase usable RAM on your Raspberry Pi with this new script (ZRAM swapdrive)

Sat Jan 08, 2022 1:29 am

Do you have to install pi apps to get your script? I used the novaspirt's script with my Pi 0 W 2 just wanted to compare the performance.

I liked with his script I could run it when I needed the boost.

ejolson
Posts: 12308
Joined: Tue Mar 18, 2014 11:47 am

Re: Increase usable RAM on your Raspberry Pi with this new script (ZRAM swapdrive)

Sat Jan 08, 2022 1:43 am

Botspot wrote:
Fri Jan 07, 2022 5:57 pm
Hi forum! I don't post here often, but I am still very involved in the larger Raspberry Pi world.
I like the new title for this thread.

I find it interesting that Armbian

https://www.armbian.com/

has ZRAM enabled by default. Turning it off is one of the first things I do on a fresh install. I'm sure ZRAM has uses (especially if one of more cores would otherwise be idle), but I never found it helped much with the tasks I wanted to perform.

User avatar
RasPiGaming
Posts: 408
Joined: Mon Jul 26, 2021 1:06 pm

Re: Increase usable RAM on your Raspberry Pi with this new script (ZRAM swapdrive)

Sat Jan 08, 2022 3:10 am

8GB is plenty for me, I rarely use more than 4GB RAM at once 8-) . No need for more RAM. I like your pi-apps store though!
An RPi can be a gaming PC. As long as you have the right packages and software anyway.
Using a RPi4 8GB with Ubuntu Desktop 21.04, main interests are gaming and programming. Also occasionally uses two RPi3B+ and a RPi3B.

Return to “General discussion”