Enable zram on rpi2

I’m running OSMC on a rpi2.

I have quite a few addons and a fairly complex skin as I depend on the functionality these provides. So I can’t really change any of this. Of the 1GB ram the rpi2 has I have dedicated 256M to graphics.

The problem is I keep running out of RAM so OSMC keeps crashing, it happens a few times every day. Since upgrading with more RAM is not possible, and adding swap to a flash card is not feasible, I thought I would try zram.

For those who doesn’t know, zram is a Linux kernel module for creating a compressed block device in RAM, i.e. a RAM disk, but with on-the-fly “disk” compression. The block device created with zram can then be used for swap or as general-purpose RAM disk. Since it’s typically compresses to a 3:1 ratio the RAM configured for use by zram will essentially be tripled. And since swapping to RAM is fast, almost as fast as keeping the pages in memory, this is a very efficient method of making the most out of hardware with limited RAM.

Anyhow, it seems like zram is either removed or not included in the first place in OSMC. It’s available in OpenELEC and Raspbian, but can’t find it in OSMC. The command “modprobe zram” just gives this error message:
modprobe: FATAL: Module zram not found in directory /lib/modules/4.9.29-15-osmc

I find this old “issue” on github requesting that zram is added to OSMC:

THis ticket refers to a “pull request” (https://github.com/osmc/osmc/pull/338) that indicates that zram has indeed been added to OSMC.

And lastly these are the code changes that should have enabled zram support: [package] [kernel-osmc] Add zRAM support · osmc/osmc@2ee7eca · GitHub

But I still cannot get this to work, the zram kernel module is nowhere to be found on my up-to-date OSMC installation.

What happened here and how can I enable zram support?

1 Like

I would also be interested in this please :slight_smile:

Hi Guys,
Any chance of shipping the ZRAM module with the RPi3 version… Does not seem to be in the package.

Moved your post here, don’t see a reason why you open a new thread

No probs. Thanks

Hi

Hopefully the issue is now addressed with the following commit:

I’d appreciate it if you could test this and provide feedback before we potentially release this as an update to other users. To test this update:

  1. Login via the command line
  2. Edit the file /etc/apt/sources.list
  3. Add the following line: deb http://apt.osmc.tv stretch-devel main
  4. Run the following commands to update: sudo apt-get update && sudo apt-get dist-upgrade && reboot
  5. Your system should have have received the update.

Please see if the issue is resolved.

I also recommend you edit /etc/apt/sources.list again and remove the line that you added after updating. This will return you to the normal update channel.

Hi Sam,
So far so good. I did was you said and created a systemd service to start it up.
I can see the swop in sitting there.

Will keep you posted on how it goes. :slight_smile:

Thanks a mil :wink:

Thanks a million for adding this, so far so good and this seems to benefit my setup a lot. I had constant memory problems with daily crashes, and my CPU was mostly idling. So the slight CPU overhead of compressing/decompressing zram is not noticed.

I’m using this script to enable the usage of zram: How enable zram module on kernel (debian lik distro)? · GitHub

Thanks again, this is just great!

Hi Sam, chjohans,duppieza and others.

Thanks for supporting Zram, Sam.
But…
Is it necessary to work with a script as chjohans talk about?
Is the script working (and how to check in terminal)?
What steps need to be taken to create a ZRAM-swap in OSMC?

Thanks for all the help we get…!

What has been added is just the ability to use zram swap, you need to actually enable it. The script I linked to is one way of doing that, it takes care of everything including calculation your amount of ram and counting your cpu cores and it sets up one “zram swap” per core, each the size of “available mem” divided by number of cpu cores.

So if you have 4 cores and 1GB ram, you’ll get 4 “zram swap partitions” each 250MB in size.

I modified the script slightly to just use “available ram” * 0.75 for zram swap, but it should be OK to use the script as it is.

After installing the script and rebooting use the “swapon” command to verify your new “zram swap partitions”.

I don’t have understand… it’s need to be activate or it’s active by default on recent VERSION_ID=“2018.03-2” ?

If i need to activate it’s suggest to activate and it’s really obtain a better performance ?

If it ain’t broken, don’t fix it! :slight_smile:

By that I mean if you don’t have any issues with OutOfMemory then do not enable zram. This will NOT give you better performance, it will use cpu cycles to compress/de-compress data to/from RAM. But if you experience frequent OOM then this might be worth a try.

In any case, google “zram” and read up on it, it is important to understand what this is, what it does and what it’s pros/cons and limitations are before attempting to use it.

2 Likes

The very curious it’s because the constructor has choice to maintain same maintain gpu chip and same size of ram every after 4 generation and because only the cpu has obtain a better product.
I know it’s a very low cost but i don’t thinks put, for example, 2 or 4gb chip of ram would cause a big surcharge… also on size problably the size of chip it’s the same like the ram on a pc :slight_smile:

I assume they haven’t changed because more RAM is not needed for the purpose the Raspberry PI was designed for while more CPU helps.

The Raspberry Pi’s VC4 is a 32-bit architecture and can currently only address 2^30 RAM (1GB). The last two bits are used as tag bits. RAM is increasing in price, so the priority is probably lower.

If you want 2GB RAM and a good system that runs OSMC, then I am sure you can find one.

Sam

So far so good. I enabled with the standard debain script, then updated with this ZRAM config for Raspberry Pi 3 · GitHub …I really appreciate your time in this. I’m liking osmc on the pi.

Thanks chjohans,
Understood. I used the script as it is and all went well.
For the moment I have no memoryproblems anymore.
Just another 2 questions:
Is there also the possibility to use LZ4?
I saw the compress/decompress time is much better.
Or is it so that LZ4 is indeed the standard compression-algorithm?
(as explained in Wikipedia)
And second: I see much more available scripts for using zram;
what should be the best?
f.e. GitHub - novaspirit/rpi_zram: script to enable zram for raspberry pi
ZRAM config for Raspberry Pi 3 · GitHub
How enable zram module on kernel (debian lik distro)? · GitHub

there can be other examples too…

Thank you very much!

Zram supports both the LZ4 and the LZO compression algoriths, and LZ4 is the default. In short LZ4 is faster at compressing/de-compressing while LZO can possibly compress a bit more efficient. I suggest you stick with the default LZ4, especially with a relatively slow CPU as the rPi’s have.

There are plenty of examples of scripts “out there”, I linked to the one I liked the best as it’s just more “professionally written”, and it handles both “start” and “stop”, although handling “stop” is less important since the “swap” is not on a physical drive.

The first script you’re linking to does essentially the same thing, and it’s kind of “short and sweet”.

The second script you’re linking to might be “optimized” for a 1GB raspberry Pi (although I disagree) and it has every value hard coded and it makes assumptions regarding the number of CPU cores and the amount of memory on your system.

Which brings me to the third script you are linking to, which is the one I like the best and what I’m using myself.

I made one small modification to the script, I added this to the top of the script, but after all the comments in the header:
FRACTION=75

I then modified this line:
echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize

so it reads this:
echo $((mem_total * FRACTION / 100 / num_cpus)) > /sys/block/zram$i/disksize

This lets me adjust “FRACTION” (I keep it at 75) which is how much (in percentage) of your memory (after the part that is allocated for the GPU) to use for zram.

By default the script will use 100% of your memory for zram. And although the zram manual page claims a typical compression ratio of 2:1 this is a “typical average”, and since my usage is OSMC which typically deals a lot with already compressed media files I chose to set this a bit lower at 75%.

Take note that I’m in no way affiliated with OSMC, however I’ve been using and administrating Linux systems for a couple of decades.

Sam, could you possibly enable LZ4 also?. Currently only “deflate” and “lzo” are available.

Thanks.

I’m happy to accept a PR on this

Sam

1 Like