[HowTo] Install and configure zram using zram-generator

Install and Configure ZRAM using zram-generator

:information_source: Note that zram is not compatible with hibernation unless combined with a swap device. :information_source:

If you are here just to have a quick install method you may skip to this section.


Install zram-generator

Execute:

sudo pacman -Syu zram-generator

Configure zram-generator

Create file /etc/systemd/zram-generator.conf with contents:

[zram0]
zram-size = ram * 2
compression-algorithm = zstd
swap-priority = 100
fs-type = swap
Instructions on how to create the file

Use either of the following methods.

Create/Edit main configuration file `/etc/systemd/zram-generator.conf` by hand

To create or edit the /etc/systemd/zram-generator.conf file you will need elevated privileges.

Execute:

sudoedit /etc/systemd/zram-generator.conf

If you have not set SUDO_EDITOR then you can define the variable with your favorite editor.

Execute:

SUDO_EDITOR=micro sudoedit /etc/systemd/zram-generator.conf

OR

Run this one-line command

Execute:

printf '[zram0]\nzram-size = ram * 2\ncompression-algorithm = zstd\nswap-priority = 100\nfs-type = swap\n' | sudo tee /etc/systemd/zram-generator.conf

zram-generator should now create a zram swap device double the size of available physical memory.

Extra Notes
  • Notice the arithmetic in zram-generator.conf for zram-size. You can likewise use /2 to create zram half the size of physical memory. Or *1 for equal the amount of physical memory.

  • Due to compression multiples of RAM should be safe even higher than *2 or 2 times the size of physical RAM. *2 may be considered a conservative maximum.


Disable zswap

zswap is a similar tool that compresses swap cache. It is incompatible with zram.

Add the option zswap.enabled=0 to your kernel parameters.

Instructions on how to add the boot option

Use either of the following methods.

Edit main configuration file `/etc/default/grub`

To edit the CMDLINE lines of /etc/default/grub you will need elevated privileges.

Execute:

sudoedit /etc/default/grub

If you have not set SUDO_EDITOR then you can define the variable with your favorite editor.

Execute:

SUDO_EDITOR=/usr/bin/micro sudoedit /etc/default/grub

Assuming no other options then it would appear as:

GRUB_CMDLINE_LINUX="zswap.enabled=0"

Save and close the file.

OR

Create drop-in configuration file `/etc/default/grub.d/disable-zswap.cfg`

Execute:

sudo mkdir -p /etc/default/grub.d && printf 'GRUB_CMDLINE_LINUX+="zswap.enabled=0"' | sudo tee /etc/default/grub.d/disable-zswap.cfg

Execute:

sudo update-grub

OR:

sudo grub-mkconfig -o /boot/grub/grub.cfg

Reboot & Check

After installing, configuring zram-generator and disabling zswap:

REBOOT

After reboot you may check the status of swap.

Instructions on how to view zram/swap usage

Execute:

swapon

AND/OR

Excute:

zramctl

Quick Install Instructions

If you are absolutely sure you want zram, to disable zswap, and that I know whats best, then you can just enter these commands.

Click to show

Simply execute the following:

sudo pacman -Syu zram-generator
printf '[zram0]\nzram-size = ram * 2\ncompression-algorithm = zstd\nswap-priority = 100\nfs-type = swap\n' | sudo tee /etc/systemd/zram-generator.conf
sudo mkdir -p /etc/default/grub.d && printf 'GRUB_CMDLINE_LINUX+="zswap.enabled=0"' | sudo tee /etc/default/grub.d/disable-zswap.cfg
sudo grub-mkconfig -o /boot/grub/grub.cfg

After a reboot you should have zram up to 2x, double, your physical memory.


More Information

Kernel Documentation

zram: Compressed RAM-based block devices — The Linux Kernel documentation

Online Manual for zram-generator

zram-generator(8) — Arch manual pages

Online Manual for zram-generator.conf

zram-generator.conf(5) — Arch manual pages

Online Manual for zramctl

zramctl(8) — Arch manual pages

ArchWiki zram Page

zram - ArchWiki

ArchWiki SWAP Page

Swap - ArchWiki

6 Likes

Shouldn’t this be zram-size = ram / 2 to let zram use half of the RAM?

1 Like

Wonderful tutorial as usual. I will just add a note: the default is zswap. And on systems with a small amount of ram (4-8 GB) it is in my opinion preferable to zram, because when it runs out of zswap it will continue with regular swap as in the old times.

This is covered in the notes. You are able to use any equation you like. But the compression works in such a way that you should safely be able to use multiples. Really I would generally encourage that. Even in my case of 16GB of RAM I need extra for certain compilations. (looking at you big browsers).


I might probably disagree excepting in certain circumstances. Like really low physical memory and a really high need. 1GB of ram but need 16 or so … then only a SWAP device of some sort will do.

This could also be related to an ‘additional SWAP backing device’ which is also possible.

I might write a tutorial on that … as it can also circumvent the hibernation drawback.*

* Or …maybe? The ArchWiki explicitly states this should not work. But there are existing projects, including in the AUR, that claim to accomplish this.


Thanks :slight_smile:

1 Like

Sorry, I missed the extra note. I’m using zram at the moment, the tutorial I followed to configure told to select how many ram you want to reserve for zram with / so that’s why I thought there were a typo.

In my computer is so easy, zram size = RAM / 3, I have 12 Gb so 4Gb are for swap that compressed can be converted up to 16Gb (as read in the tutorial).

What I can’t understand is how zram can manage to use up to 4x total RAM, how many RAM remains then for using applications in that moment??? These modern things drive me crazy… :smile:

And of course thanks for this tutorial. :+1:

So the number you give to the conf is the maximum uncompressed size.
The actual number allowed for you will depend on the compression method.
In the case above we use zstd which has a pretty high compression ratio.

Though looking around I cannot find the higher numbers I was using.
So I will change all suggestions to a maximum of *2.

To quote:

Your example of ram / 2 is an equation for the size of the zram ‘device’ … meaning it will create a zram device with a total size of half of your physical RAM. In the case of 8gb that means 4gb uncompressed. Due to compression this 4gb zram swap will only actually occupy between 1-2gb of your RAM. Likewise a from 8gb memory a ram * 2 zram ‘device’ would be 16gb in size, but would only occupy at most 7.15gb of RAM.

For whatever its worth we can also compare some raw numbers here.
If we consider memory+swap all ‘memory space’, and if we assume that ‘more is better’…
Using the same 8gb as an example and a conservative 2.23x compression, in a ‘totally full’ state…

zram config zram size reserved memory Total memory space
ram / 2 4 GB 6.21 GB 10.21 GB
ram / 1 8 GB 4.42 GB 12.42 GB
ram * 2 16 GB 0.83 GB 16.83 GB
1 Like

I have my zram set as two-thirds of my total RAM:

zram-size = ram / 1.5

free                                                                                                                         ✔ 
               total        used        free      shared  buff/cache   available
Mem:           28992        5566        2470         194       21580       23426
Swap:          19327           0       19327

I guess I just like to be different…

I’ve never gone anywhere near filling up my zram swap, although when I do have a lot of Firefox tabs open with large images in them (sometimes more than 50MB each picture), my swap does (very rarely) reach 20-30%. That’s probably because I disabled the disk cache for Firefox & changed the settings in about:config to greatly increase Firefox’s memory cache and also the size of individual files it can store in the memory cache.

As with any compression method, the compression ratio varies from the data that is compressed. Me personally would assume a conservative scenario, i think in the wiki was written that the average for zstd is 1 to 4. So writing twice the ram uncompressed will mean half of the ram compressed, and will leave you with half of the ram. Which is exactly why i would not recommend it for anything =< 8 GB. But for 32+, sure.

I definitely make use of profile-sync-daemon as well, which affects firefoxs memory and cache.

My need for extra SWAP did not come from general use … it came from compiling ungoogled-chromium. Which you might not even be able to do with 32gb (alone, no swap) these days.

Just installed it, but I probably should have exported all of my (100+) add-on configurations first, as all of their settings were reset (and I should have backed up my profile, as recommended, too).

Oh well - I guess my Firefox was due for a tidy-up, and the only thing that might take some time is recreating some of my Redirector rules (most of which were old & never used anyway). All of my history, passwords & bookmarks are retained, so it isn’t a catastrophic issue.

Still, I’ll stick with psd & see how it goes - thanks for the recommendation.

Oh no. Did you check the directory (in ~/.mozilla) to be sure?
I dont think I recall a settings-wipe/profile-reset, but its been a while.

It wasn’t as bad as I thought - only a few of my extensions were affected. I’ve had a quick look through the ~/.mozilla directory, but couldn’t find anything retrievable. I assume it depends on the way the add-ons store their data.

Its not much of a hassle though. With no NRL/AFL on today, it has given me something to do to fill in the afternoon. I actually found that some of my extensions have had new features added that I wasn’t aware of since I initially installed them, so the time spent going through each of them isn’t wasted.

Edit: Also, I’ve learnt quite a bit since I wrote many of my Redirector rules. For one image site I had about 6-8 separate rules to redirect various types of small-sized image & thumbnail URLs to the full-sized images. I just wrote a new rule that (I hope) will cover everything for that site.

1 Like

Thank you very much for the tutorials. zram is working fine now.

 zramctl 
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo-rle      30.6G   4K   80B   12K       8 [SWAP]

Interesting. I wonder if the overhead of compression/decompression would negate any benefit of using zram for /tmp rather than tmpfs. I may have to have a play when time permits.

[Edit]: yes it does negate any benefit. A 580M text file takes about twice as long to transfer to a zram filesystem as it takes to transfer to tmpfs. And even longer than to transfer it to a spinning rust filesystem.

Interesting to try, though…