[SOLVED] How to configure zRAM?

Questions about applications and software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
majpooper
Level 8
Level 8
Posts: 2087
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

[SOLVED] How to configure zRAM?

Post by majpooper »

OK a little back ground - I am running a 10 year old HP Pavilion with an 8 core AMD bulldozer CPU with 16Gb of RAM with two 160Gb SSDs. It was initially configured for RAID 0 which Catweasel help me set up about 5 years ago - I have since abandoned RAID and use the second SSD for DATA. I have completely removed swap (deleted the swap file and commented out swap in fstab). I have run so long without swap with no noticeable ramifications I thought the 2Gb swap file was wasted space - not that I needed the space. Then I saw comments about zRAM as an alternative to swap and trying to understand it better and would like to be able to configure zRAM on systems that are memory challenged. So I am trying to set it up on my system - I don't think I need it but it can't hurt and I want to know how to do it properly.

I have found a HOW-TO but there a few steps I don't understand.
https://www.techrepublic.com/article/ho ... -on-linux/
How to enable zRAM
The zRAM module is controlled by systemd, so there's no need for an fstab entry. And since everything is already installed out of the box, we only need to create a few files and modify one. 
Open a terminal window and create a new file with the command:
1.)

Code: Select all

sudo nano /etc/modules-load.d/zram.conf
In that file, add the word:
2.)

Code: Select all

zram
Save and close the file. 
Next, create a second new file with the command:
3.)

Code: Select all

sudo nano /etc/modprobe.d/zram.conf
In that file, paste the line:
4.)

Code: Select all

options zram num_devices=1
Save and close the file.
Next, we need to configure the size of the zRAM partition. Create a new file with the command:
5.)

Code: Select all

sudo nano /etc/udev/rules.d/99-zram.rules
In that file, paste the following (modifying the disksize attribute to fit your needs):
6.)

Code: Select all

KERNEL=="zram0", ATTR{disksize}="512M",TAG+="systemd"
Save and close the file.
#4 zram num-devices=1 - is zram considered a device? will it show up in disks? Would you ever need more than one instance of zram?
#6 is where I get stuck in that what am I trying to accomplish with disksize ? Also what is the syntax - do I remove the curly braces so it would look like

Code: Select all

KERNEL=="zram0", ATTR2="512M",TAG+="systemd"
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: How to configure zRAM ? ? ?

Post by MrEen »

Hi majpooper,

I know nothing about zRAM myself, but Pjotr has stuff about it on his site. Maybe that might help?

EDIT: And you leave the curly braces as they are.
Robot1me

Re: How to configure zRAM ? ? ?

Post by Robot1me »

Hey there,

funnily enough I spent time with that recently and I can tell you the easiest way. You can let zram-config do the work for you:

Code: Select all

sudo apt install zram-config
After a system reboot, you can check if zram works by using

Code: Select all

zramctl
and

Code: Select all

swapon -s
Additionally, you can increase the vm.swappiness value to 100 in order to make the kernel swap to zram sooner. It's especially worth it if you want to maximize your RAM and if CPU performance is no bottleneck. You can check this answer to find out how to do that: https://askubuntu.com/questions/463281/ ... ter-reboot

Should you change your mind, you can uninstall zram-config to remove the zram devices again:

Code: Select all

sudo apt purge zram-config
DAMIEN1307

Re: How to configure zRAM ? ? ?

Post by DAMIEN1307 »

Additionally, you can increase the vm.swappiness value to 100 in order to make the kernel swap to zram sooner
That is about the best way to do zram that i have seen thus far...DAMIEN
User avatar
majpooper
Level 8
Level 8
Posts: 2087
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: How to configure zRAM ? ? ?

Post by majpooper »

Robot1me wrote: Mon Jun 22, 2020 7:53 am Hey there,

funnily enough I spent time with that recently and I can tell you the easiest way. You can let zram-config do the work for you:

Code: Select all

sudo apt install zram-config
After a system reboot, you can check if zram works by using

Code: Select all

zramctl
and

Code: Select all

swapon -s
Additionally, you can increase the vm.swappiness value to 100 in order to make the kernel swap to zram sooner. It's especially worth it if you want to maximize your RAM and if CPU performance is no bottleneck. You can check this answer to find out how to do that: https://askubuntu.com/questions/463281/ ... ter-reboot

Should you change your mind, you can uninstall zram-config to remove the zram devices again:

Code: Select all

sudo apt purge zram-config
OK this is good stuff - it makes setting it up easy, but . . . . . there is still too much I don't understand and so far can't find answers to. First of which why do I want to swap to zram sooner?
DAMIEN1307 wrote: Mon Jun 22, 2020 9:27 am
Additionally, you can increase the vm.swappiness value to 100 in order to make the kernel swap to zram sooner
That is about the best way to do zram that i have seen thus far...DAMIEN
What is really crazy is I have configured zRAM on two PCs and a laptop with three different results although all three have similar CPUs and all have 16Gb of RAM. I have configured zRAM on PC(majpooper) 8 core AMD Bulldozer w/ 16Gb RAM where I deleted the swap file and commented out swap in fstab. The laptop (dufus) i7 CPU w/16Gb RAM where I set swapiness =1 and the swap file remains. And PC (fluffy) i7 CPU w/16Gb RAM where I set swapiness =1 and the swap file remains. Why are all three zRAM configurations so different ?

It seems I would want to be able to configure how big I want zRAM to be based on the CPU and how much RAM is available. In my case with three similar systems it goes from 1Gb with no swapfile to 8Gb with a 1Gb swapfile all the way to 16Gb with a 2Gb swapfile if I am reading things correctly.

And really in the end I though what I wanted to accomplish is replace say a 2Gb swapfile on the hard drive with 2Gb of zRAM hence delete and comment out the swapfile. And I really didn't think I wanted the system to go to swap any sooner with zRAM than I did with a swapfile I just thought zRAM would be faster when the system did go to swap.

Again I really don't need this for my rigs although I will leave things be for now to see how they react if at all. Why I want to understand how to configure zRAM is for instances that I run into when asked to install linux on an old or resource challenged PC or laptop so I want some control in terms of how zRAM is configured

Code: Select all

majpooper@1150z:~$ zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0             998.9M   4K   74B   12K       8 [SWAP]
majpooper@1150z:~$ swapon -s
Filename				Type		Size	Used	Priority
/dev/zram0                             	partition	1022824	0	5

Code: Select all

dufus@T430:/$ zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram3               1.9G   4K   74B   12K       4 [SWAP]
/dev/zram2               1.9G   4K   74B   12K       4 [SWAP]
/dev/zram1               1.9G   4K   74B   12K       4 [SWAP]
/dev/zram0               1.9G   4K   74B   12K       4 [SWAP]
dufus@T430:/$ swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-2                              	partition	1003516	0	-2
/dev/zram0                             	partition	2014628	0	5
/dev/zram1                             	partition	2014628	0	5
/dev/zram2                             	partition	2014628	0	5
/dev/zram3                             	partition	2014628	0	5

Code: Select all

fluffy@hp700:~$ zramctl
NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram7             994.9M   4K   74B   12K       8 [SWAP]
/dev/zram6             994.9M   4K   74B   12K       8 [SWAP]
/dev/zram5             994.9M   4K   74B   12K       8 [SWAP]
/dev/zram4             994.9M   4K   74B   12K       8 [SWAP]
/dev/zram3             994.9M   4K   74B   12K       8 [SWAP]
/dev/zram2             994.9M   4K   74B   12K       8 [SWAP]
/dev/zram1             994.9M   4K   74B   12K       8 [SWAP]
/dev/zram0             994.9M   4K   74B   12K       8 [SWAP]
fluffy@hp700:~$ swapon -s
Filename				Type		Size	Used	Priority
/swapfile                              	file    	2097148	0	-2
/dev/zram0                             	partition	1018788	0	5
/dev/zram1                             	partition	1018788	0	5
/dev/zram2                             	partition	1018788	0	5
/dev/zram3                             	partition	1018788	0	5
/dev/zram4                             	partition	1018788	0	5
/dev/zram5                             	partition	1018788	0	5
/dev/zram6                             	partition	1018788	0	5
/dev/zram7                             	partition	1018788	0	5
Robot1me

Re: How to configure zRAM ? ? ?

Post by Robot1me »

majpooper wrote: Mon Jun 22, 2020 1:42 pm OK this is good stuff - it makes setting it up easy, but . . . . . there is still too much I don't understand and so far can't find answers to. First of which why do I want to swap to zram sooner?
It largely depends on what you want to achieve. Since you enabled this handy feature, most likely you want to get the most out of it (more total memory). Therefore it's better to increase the vm.swappiness value, especially since you have plenty of RAM. Because in that case, the Linux kernel is veeeeery restrained to actually swap out. Where zram really shines is when you push the boundaries of your RAM. Without it, memory would be swapped out to a swap partition. Which is bad for hard drives (extremely slow) and SSDs (impacts total lifetime write cycles).
KBD47
Level 7
Level 7
Posts: 1836
Joined: Fri Jul 29, 2011 12:03 am

Re: How to configure zRAM ? ? ?

Post by KBD47 »

Useful guide. Thank you.
User avatar
majpooper
Level 8
Level 8
Posts: 2087
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: How to configure zRAM ? ? ?

Post by majpooper »

Robot1me wrote: Mon Jun 22, 2020 2:27 pm Therefore it's better to increase the vm.swappiness value, especially since you have plenty of RAM. Because in that case, the Linux kernel is veeeeery restrained to actually swap out.
Sorry for being soooo dense - but I have two discreet situations:

First, on my systems that I am testing zram on - yes I have plenty of RAM and never have to go to swap based on how I use my systems - I want the kernel to be restrained from using swap - as a matter of fact I don't want the kernel to ever use swap which is why I actually removed swap completely from my daily driver and reduced vm.swapiness =1 on the others. When I am done playing with zram I will remove it from all my systems because again I really don't need any form of swap on any of them.
Robot1me wrote: Mon Jun 22, 2020 2:27 pm . . . . most likely you want to get the most out of it (more total memory). Without [zram] memory would be swapped out to a swap partition. Which is bad for hard drives (extremely slow) and SSDs (impacts total lifetime write cycles).
Second, and the crux of the situation - I am in the process of installing linux on an old MacBook (a laptop) with a puny Intel Core 2 Duo CPU and 2Gb RAM. How to most effectively configure zram to improve performance is the real question. Does it make sense to remove swap entirely and replace it with zram ? Do I leave swap and install zram in addition? Do I just let the zram installer use the defaults knowing that typically defaults are engineered as a compromise and usually a little "fat". Since this laptop will basically be just a browser/YouTube platform and maybe an occasional document I want to optimize RAM before going to swap. That is why I like the idea of understanding how to configure zram rather than leaving it to the script.
Robot1me

Re: How to configure zRAM ? ? ?

Post by Robot1me »

majpooper wrote: Tue Jun 23, 2020 10:43 am First, on my systems that I am testing zram on - yes I have plenty of RAM and never have to go to swap based on how I use my systems - I want the kernel to be restrained from using swap - as a matter of fact I don't want the kernel to ever use swap which is why I actually removed swap completely
That is fine of course, though it can be risky not to have any swap partition or file at all. In that case it's definitely the best to set swappiness to 1 and call it a day.
majpooper wrote: Tue Jun 23, 2020 10:43 am Second, and the crux of the situation - I am in the process of installing linux on an old MacBook (a laptop) with a puny Intel Core 2 Duo CPU and 2Gb RAM. How to most effectively configure zram to improve performance is the real question. Does it make sense to remove swap entirely and replace it with zram ? Do I leave swap and install zram in addition? Do I just let the zram installer use the defaults knowing that typically defaults are engineered as a compromise and usually a little "fat". Since this laptop will basically be just a browser/YouTube platform and maybe an occasional document I want to optimize RAM before going to swap. That is why I like the idea of understanding how to configure zram rather than leaving it to the script.
The default settings are actually great, especially on all recent Linux distributions. Because since kernel version 5.1, the performance of zram was improved even further (one source: https://www.phoronix.com/scan.php?page= ... er-Perform). It is definitely better to keep the swap partition. Because once the zram devices are full, the out of memory killer is triggered. So all in all, the default settings are very decent. I would not worry about it if I was you. But if you feel like trying to test something, change the swappiness value. Just my humble opinion.
User avatar
majpooper
Level 8
Level 8
Posts: 2087
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: [SOLVED] How to configure zRAM ? ? ?

Post by majpooper »

OK - makes sense I will leave the swap file - the HD is 250Gb so that is not a big deal and add zram with the zram installer. I am installing LM 20 beta Cinnamon which is running kernel 5.4 so that should reap the benefits outlined in the article. I still do not understand why all my systems were all over the place in terms of how the script configured zram though. But we will see how zram works on this old MacBook.

As an aside - there was no difference in performance between live LM 20 Xfce and LM 20 Cinnamon . . . . ? ? ? ? ?
Locked

Return to “Software & Applications”