Move from swap partition to zRAM

Especially when your system includes SSD or SDcard drives and/or have 8GB RAM or more, using zswap or zram is a great idea.

Introduction:

A usual Linux installation includes the presence of a swap partition, or at the best a swap file, that are usually automatically created from the installers. The reason for using swap is to support low system memory situations, which was always a problem, due to the high cost of RAM and the inability of non-modern systems to utilise the extra RAM memory, if it was present. Nowadays, RAM has become more in excess, as the well known mainstream OS demands a lot of it, just to work in idle..

Brief description of zram and zswap

Summary of their implementations:

  1. ZRAM is a compressed RAM based swap device
  2. ZSWAP is a compressed Cache if you already have a swap.

Details:

  • ZRAM: Makes a swap device in the RAM. Pages sent here are compressed as they are stored. It has a higher priority than other swap devices: pages that are swapped out are preferentially sent to the zram device till it is full, only then are any other swap devices used.
    • Benefits: Independent of other (physical) swap devices. It can be used when there is no swap partition to expand the available memory.
    • Disadvantages: If other swap devices (HDD/SSD) are present they are not used optimally. As the zram device is an independent swap device, once it is full, any new pages that need to be swapped out are sent to next swap device directly, hence:
      1. There is a real chance of LRU (least recently used) inversion: It will be the most recently swapped data that goes to the slow disk, while inactive pages that were swapped out long ago will remain in the fast ZRAM
      2. The data sent to and read from the disk will consume a lot of bandwidth as it is uncompressed.
  • ZSWAP: The frontswap system hooks attempts to swap out pages and uses zswap as write-back-cache for a HDD/SSD swap device: An attempt is made to compress the page and if it contains poorly compressible data it is directly written to the disk. If the data is compressed, it is stored in the pool of zswap memory. If pages are swapped out of memory when the total compressed pages in RAM exceeds a certain size, the Least Recently Used (LRU) compressed page is written to the disk as it is unlikely to be required soon.
    • Benefits: Very efficient use RAM and disk based swap. Minimizes Disk I/O by both reducing the number of writes and reads required (data is compressed and held in RAM) and by reducing the bandwidth of these I/O operations as the data is in a compressed form.
    • Limitations: It is an enhancement of disk based swap systems and hence depends on a swap partition on the hard disk.

Linux kernel has included zram and zswap modules/features, since several years. Not using them, is like having a beautifull super-fast car in your garage and never ride it!

Personal History:

Because of the rare usage of my laptop, as I haven't a lot mobilization needs for a long time, I had not given the proper care to its system. I had only installed an old SSD, after upgrading my desktop with a new one, leaving the proper setup to a later time, which never came..
Because of the unexpected need of a mobile system, the laptop was used extensively for the last two months and only recently I remembered, I had to configure SSD properly. Looking for the best options (again) I stumbled upon the z-terms, which then made be find an older greek article about it. And then I did it. I removed swap partition (from the.. SSD, which was a crime..) and enabled zram. Now my system runs fine, with no swap partition or file nad... I have a "new" 4GB partition to experiment my new idea about a rescue/restore partition on Grub-based systems.


Disclaimer:
Both zram and zswap result in a higher (not crazy high, though) CPU usage. It is a user/distro decision if they use it or not by default, depending on their individual needs and priorities.
This is just an easy and quick guide about a specific way of using zram/zswap with systemd-swap official Arch package. You may want to use zramswap from AUR, or the "Arch way", editing files by hand and enable/disable manually, or even create a service unit, or a udev rule. Always check Archwiki for more options.


  • Check your existing swap (example)
swapon --show
NAME      TYPE      SIZE  USED PRIO
/dev/sda3 partition 4,09G 1,2M 100
  • Install systemd-swap helper package
sudo pacman -Syu systemd-swap
  • Create a file with these contents:
zswap_enabled=0
zram_enabled=1
swapfc_enabled=1

Save the file as (the file name can be anything, as long it's *.conf):

/etc/systemd/swap.conf.d/laptop.conf

This file contains your custom overrides to the package defaults at /etc/systemd/swap.conf. You can add more customizations in the new file, after reading the help comments in the main file.

  • Enable and start systemd-swap
sudo systemctl enable --now systemd-swap
  • Disable swap
swapoff /dev/sda3

and disable/remove the swap line in your /etc/fstab (example)

# UUID=3170c524-1317-493c-8afe-d16c502e8452 swap swap defaults,noatime 0 0

Now you may re-format the swap partition to ext4 or other FS type, in order to use the extra disk space. You can use your favorite partition manager's GUI, like GParted. You need to unmount the partition first, so you don't need to reboot.

After you finish, reboot and check if zram swapping is working

swapon --show
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition 1,9G 1,2M 32767

You may use systemd-swap utility to do several things about normal swap, using zswap instead of zram, etc. Also you may not use it, and use a udev rule or kernel parameters. It was just the easiest way for me to do it!
More info on Archwiki. For Greek language there are two nice articles here and here, that I used them as a guide and to help me understand, which is hard...

22 Likes

So it uses your some of your RAM to hold a block device, which gets used as swap.

The ArchWiki uses an example of 32 GB zram. Is that for a 32 GB RAM machine?

Also, what if I use Hibernate (suspend to harddrive)?
Is that no longer possible, since regular swap would hold the session?

I forgot to write? I have to add it, sorry.
(Edit: It was in the opening quote!!)
Mine is 8GB, which is the least I would say to immediately do it :grin:
It is also of help on less RAM HW, because (the whole point is) it compresses cache to the RAM. With zram, you gain from disk long time, since it does not use the disk at all. For zswap, it requires a disk swap partition or file, and I guess it can hibernate. I assume (not tested yet but.. it's logical) with zram you can not hibernate (where to?). But suspend to RAM works fine!!

2 Likes

seems to work great, i have 69 chromium windows, 42 vivaldi, 6 firefox, pamac, term, kitchen sink
NAME TYPE SIZE USED PRIO
/dev/zram0 partition 1.9G 148.3M 32767

didnt kick in until around 5g/ram used, all is well. i dont think ive ever seen my swap go above 0%, although ive never really had 119 windows open either. :grin:

edit: was at 92% of 8g ram

1 Like

Does it buffer/use it in the same way that a standard swap file would be used?

Does vm.swappiness sysctl setting have any effect on it?

What way is this? :thinking:

I don't think so. In /etc/systemd/swap.conf there is a lot of "calculated" instructions on how to "swap" at will. Swappiness is just one setting/limit..

1 Like

Oh? Hmmm, then fyi here's an alternative that does not need that.

In mid-August i disabled my Swap partition. So that i still have a Swap capacity i installed & enabled+started zramswap from AUR.

Details fyi:

BEFORE deactivating my encrypted Swap partition & using zramswap:

Summary
[kdemeoz@GA-Z97-HD3-Tower ~]$ sudo blkid    
/dev/sda1: SEC_TYPE="msdos" UUID="2D58-EE30" TYPE="vfat" PARTLABEL="primary" PARTUUID="bde1c356-f603-4a7e-ad42-c399c35f9750"
/dev/sda2: UUID="b0d0c798-a0ab-46c3-8e4c-c8f9720e4c09" TYPE="ext4" PARTUUID="d34753b8-6bdd-4909-84c2-dc66d04a54a1"
/dev/sda3: UUID="7106d77d-ce05-4059-87fc-9f6de535b652" TYPE="crypto_LUKS" PARTUUID="9669992d-4b4d-400d-bbbf-f48a1043f245"
/dev/sda4: UUID="72c5d4ad-6516-4e96-ab5f-35faa762eb61" TYPE="crypto_LUKS" PARTUUID="0a7e3a3c-1161-4b8a-a4de-d41e42d99d75"
/dev/sdb2: LABEL="SeagateSpare" UUID="f4ae6a8c-a541-4bbd-b086-6358872a3962" TYPE="xfs" PARTUUID="00060756-02"
/dev/sdb3: LABEL="Seagate" UUID="23927deb-03f4-4b7b-9599-9e44c9f86919" TYPE="ext4" PARTUUID="00060756-03"
/dev/mapper/luks-72c5d4ad-6516-4e96-ab5f-35faa762eb61: UUID="dd8e7d66-b245-4086-8c3e-05d8aecc43aa" TYPE="swap"
/dev/mapper/luks-7106d77d-ce05-4059-87fc-9f6de535b652: UUID="b539574d-c9d8-4f5c-8bfd-ea7e6ad81517" TYPE="ext4"
[kdemeoz@GA-Z97-HD3-Tower ~]$ 

Enabling & starting zramswap:

Summary
[kdemeoz@GA-Z97-HD3-Tower ~]$ sudo systemctl enable zramswap
Created symlink /etc/systemd/system/multi-user.target.wants/zramswap.service → /usr/lib/systemd/system/zramswap.service.

[kdemeoz@GA-Z97-HD3-Tower ~]$ sudo systemctl start zramswap

[kdemeoz@GA-Z97-HD3-Tower ~]$ sudo systemctl status zramswap
● zramswap.service - Zram-based swap (compressed RAM block devices)
   Loaded: loaded (/usr/lib/systemd/system/zramswap.service; enabled; vendor preset: disabled)
   Active: active (exited) since Tue 2018-08-14 18:20:24 AEST; 47s ago
  Process: 7176 ExecStop=/usr/lib/systemd/scripts/zramctrl stop (code=exited, status=0/SUCCESS)
  Process: 8076 ExecStart=/usr/lib/systemd/scripts/zramctrl start (code=exited, status=0/SUCCESS)
 Main PID: 8076 (code=exited, status=0/SUCCESS)

AFTER deactivating my encrypted Swap partition, & then using zramswap:

Summary
[kdemeoz@GA-Z97-HD3-Tower ~]$ sudo blkid    
/dev/sda1: SEC_TYPE="msdos" UUID="2D58-EE30" TYPE="vfat" PARTLABEL="primary" PARTUUID="bde1c356-f603-4a7e-ad42-c399c35f9750"
/dev/sda2: UUID="b0d0c798-a0ab-46c3-8e4c-c8f9720e4c09" TYPE="ext4" PARTUUID="d34753b8-6bdd-4909-84c2-dc66d04a54a1"
/dev/sda3: UUID="7106d77d-ce05-4059-87fc-9f6de535b652" TYPE="crypto_LUKS" PARTUUID="9669992d-4b4d-400d-bbbf-f48a1043f245"
/dev/sda4: UUID="72c5d4ad-6516-4e96-ab5f-35faa762eb61" TYPE="crypto_LUKS" PARTUUID="0a7e3a3c-1161-4b8a-a4de-d41e42d99d75"
/dev/sdb2: LABEL="SeagateSpare" UUID="f4ae6a8c-a541-4bbd-b086-6358872a3962" TYPE="xfs" PARTUUID="00060756-02"
/dev/sdb3: LABEL="Seagate" UUID="23927deb-03f4-4b7b-9599-9e44c9f86919" TYPE="ext4" PARTUUID="00060756-03"
/dev/mapper/luks-7106d77d-ce05-4059-87fc-9f6de535b652: UUID="b539574d-c9d8-4f5c-8bfd-ea7e6ad81517" TYPE="ext4"
/dev/zram0: LABEL="zram0" UUID="3fdf510a-a6eb-44c7-a040-f2b61f7373fb" TYPE="swap"
/dev/zram1: LABEL="zram1" UUID="917304d1-197e-4057-8711-9a1872ee046b" TYPE="swap"
/dev/zram2: LABEL="zram2" UUID="824e72ee-a0dc-4523-bf67-1491306c3b0c" TYPE="swap"
/dev/zram3: LABEL="zram3" UUID="30bda29f-8644-4bc4-b1a0-900f6e48b798" TYPE="swap"
/dev/zram4: LABEL="zram4" UUID="2df0988a-ac0c-4626-ba0e-6601e06ecd53" TYPE="swap"
/dev/zram5: LABEL="zram5" UUID="744cc715-a2fe-4f0a-bffa-911806a5012a" TYPE="swap"
/dev/zram6: LABEL="zram6" UUID="63ffd6fc-e1cb-4dfa-8fd9-176128eb3cd4" TYPE="swap"
/dev/zram7: LABEL="zram7" UUID="9fc5ae3b-4691-4388-809d-99c6a324af3a" TYPE="swap"

Subsequent confirmation that despite no longer running a Swap partition i still have Swap capacity:

Summary
[kdemeoz@GA-Z97-HD3-Tower ~]$ free -h
              total        used        free      shared  buff/cache   available
Mem:           30Gi       1.2Gi        28Gi       314Mi       1.2Gi        28Gi
Swap:         6.2Gi          0B       6.2Gi
[kdemeoz@GA-Z97-HD3-Tower ~]$ 
2 Likes

This is information from 2015 so it may not be completely up-to-date. I don't know if zramswap AUR is a hybrid of these.

Summary of their implementations:

  1. ZRAM is a compressed RAM based swap device
  2. ZSWAP is a compressed Cache if you already have a swap.

Details:

  • ZRAM: Makes a swap device in the RAM. Pages sent here are compressed as they are stored. It has a higher priority than other swap devices: pages that are swapped out are preferentially sent to the zram device till it is full, only then are any other swap devices used.
    • Benefits: Independent of other (physical) swap devices. It can be used when there is no swap partition to expand the available memory.
    • Disadvantages: If other swap devices (HDD/SSD) are present they are not used optimally. As the zram device is an independent swap device, once it is full, any new pages that need to be swapped out are sent to next swap device directly, hence:
      1. There is a real chance of LRU (least recently used) inversion: It will be the most recently swapped data that goes to the slow disk, while inactive pages that were swapped out long ago will remain in the fast ZRAM
      2. The data sent to and read from the disk will consume a lot of bandwidth as it is uncompressed.
  • ZSWAP: The frontswap system hooks attempts to swap out pages and uses zswap as write-back-cache for a HDD/SSD swap device: An attempt is made to compress the page and if it contains poorly compressible data it is directly written to the disk. If the data is compressed, it is stored in the pool of zswap memory. If pages are swapped out of memory when the total compressed pages in RAM exceeds a certain size, the Least Recently Used (LRU) compressed page is written to the disk as it is unlikely to be required soon.
    • Benefits: Very efficient use RAM and disk based swap. Minimizes Disk I/O by both reducing the number of writes and reads required (data is compressed and held in RAM) and by reducing the bandwidth of these I/O operations as the data is in a compressed form.
    • Limitations: It is an enhancement of disk based swap systems and hence depends on a swap partition on the hard disk.
1 Like

Was it TLDR?
This (zswapzram, as in the OT title) is what I am describing, just with the "official/repository" utility method. zramswap does the same, also with a systemd service, just it .. maybe smarter! :grin:
OTOH systemd-swap can be used for normal swap, zswap and zswapzram. You just have to change the settings. With default settings, it uses zswap. If you add the mentioned file with overrides, it uses zswap.
zswap is good for SSDs to prevent disk wear, and low memory machines because it compresses cache/"memory", so it seems you "have more RAM" :face_with_raised_eyebrow:. The only drawback I can see is the increased CPU usage, because of the compressing calculations, but this is what it can be decided from a user's priorities :wink:

IDK but as explained, systemd-swap can do both, but not exactly "hybrid". You should/can not do both at the same time. If you want both RAM compression and swap on disk for hibernation, you can just use zswap. It does the same thing.

1 Like

I named it petsam.conf :slight_smile: to know who is is the guilty :wink:

sgs@mx Linux 4.19.1-1-MANJARO x86_64 18.0.0 Illyria

~ >>> swapon --show
NAME       TYPE      SIZE USED  PRIO
/dev/zram0 partition 3,9G   0B 32767

sudo blkid
/dev/zram0: UUID="b1b72ef1-8fa2-4254-abe9-8d6e3bf8211f" TYPE="swap"

grafik

Thank you very much. It worked wonderfully. :slight_smile:

1 Like

No. You won't catch me ever using that mean pejorative insult [except sarcastically, or otherwise bitterly]. I read your post in its entirety, as i do with all posts [whether i fully comprehend everything i read though is a whole other question!].

But does it? The specific, solitary, only reason i bothered posting in your thread, was when my eye caught your statement [which i already quoted, hoping you'd grasp my context]:

To me that seems like a fundamental difference between the two methods / packages, & is why [back in August] i chose to use zramswap. Had you not written that sentence, i'd never have bothered posting. I simply hoped that you / others might like to know of an alternative method that does NOT still require a physical Swap file/partition. Sorry for wasting your time.

Great Article, but the disclaimer should be it depends on the CPU you have since that is where the trade off is. Multiple cores is what is needed.

@AgentS Do you have any performance metrics to compare off with your i3, because I wouldn't go any lower than i3?

1 Like

Hey! Calm down!.. You seem not in a good mood today.. :stuck_out_tongue_winking_eye:

because

I think you keep forgetting that my English language skills are not a native American's or other English native speaker. Please, slowly step backwards and try to not mis-judge my intentions. I am a really nice guy!!

I dislike these "I said/you said" debates, so let's keep it simple.

  • zswap and zram are the "modules" doing some job in the field of compressing RAM for improving performance.
  • systemd-swap and zramswap are assistants for usage and configuration on the mentioned field, which set/change zswap/zram modules settings and activation.
  • Since I only tested systemd-swap, I have read that it can handle settings and activation of all 3 swaps, normal, zram, zswap. I don;t know if and what can zramswap do. If you like to contribute to the tutorial, please do, in a Tutorial-style writing and either edit the wikified post, or I can do it with your input.

I take this option, as I thought you already knew.. Sorry for the trouble

So

is between zram and zswap, not the other two assistants/utilities.
As I state in my profile info, I know only few things. I just RTFM, as it's inherited from my work environment.
I like to provide evidence when I have some

Archwiki

The systemd-swap package provides a systemd-swap.service unit to automatically initialize zram devices. Configuration is possible in /etc/systemd/swap.conf .
The package zramswap (AUR) provides an automated script for setting up such swap devices with optimal settings for your system (such as RAM size and CPU core number).

Are we friends now? :kissing_heart:

1 Like

What is that? I'm just a simple user. I used it to save time for my SSD. I don't need swap now.
My CPU (cooler) may run more than before, but it may be placebo. You may try it for testing. It won't break anything. It's a kernel integrated feature, something like "normal" feature.

1 Like

I have now twice pointed out to you the explicit words that piqued my initial curiosity.

Apparently we seem destined to be still discussing different things. I give up now. I never wanted to argue, only help via providing alternative info. I failed, so won't bother you in this thread any further.

1 Like

hi,

i would like to give zram a whirl, but there is still tmpfs (autocreated during installation?) in my fstab:

tmpfs                                     /tmp           tmpfs   defaults,noatime,mode=1777 0 0

is it safe to deactivate/delete it? i plan to redirect /tmp to zram instead so to have only one 'ramdisk' active.

Yes. That's not normally needed. /tmp is auto-created by systemd, even without an fstab entry.

I don't know what you mean. /tmp is in RAM, the "cloud" :grin:, zram uses-extends cache/swap to RAM. So, what is your reason? Maybe we 'll all gain something new..

2 Likes

thanks for the inspiration, I've now got swap in zram on my notebook which has 16GB RAM using systemd-swap from the community repository rather than installing anything from AUR

swapon --show
NAME       TYPE      SIZE USED  PRIO
/dev/zram0 partition 3.9G   0B 32767

as per the notes on the upstream website regarding usage with SSD devices I've commented out zswap in the automatically created swap.conf and enabled zram only. it's working fine so I have removed the swap partition, resized my home partition and removed all hibernation function references from grub and mkinitcpio since I never actually bother with that.

i'll do the same with my desktop later :slight_smile:

1 Like

i was unsure, wether tmpfs should be disabled after all and/or to run both (tmpfs+zram) in parallel, that is all.

so i deactivated #tmpfs (fstab) and set up a zramdisk device (not systemd-swap) alone, now redirecting /tmp to it - all is good :sunny:

Can you describe this please.

Forum kindly sponsored by