Need help with Swap - Its disabled by default

Hi I was trying to set up fastfetch when I discovered Its already installed on the system, But in that output I discovered that -

Memory: 3.08 GiB / 3.52 GiB (88%)
 Swap: Disabled

And now I understand why my ‘system out of memory’ hangs just cannot recover.

My thoughts with System Hangs

I always assumed that it was a slow read-write speeds of HDD
But was always surprised with the thought of 'just having slow speeds shouldn’t make that big of unrecoverable problem that I need to hard restart the system everytime.

I thought Swap would be enabled by default.

Now I need to know how Swap is enabled
I saw some articles on how to do it, but I don’t trust internet with commands I’m not familiar with, hence asking here.

The part in articles I don't like

It’s saying to create a file with
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
and set some permissions and stuff…

I’m being overly careful with this system, as its my only system currently

Some system Info
Host: 20AMS36W0J (ThinkPad X240)
OS: Manjaro Linux x86_64
Kernel: LinuxKernel: Linux 6.18.18-1-MANJARO
Packages: 1932 (pacman)[stable], 65 (flatpak)
Shell: zsh 5.9
DE: KDE Plasma 6.5.6
CPU: Intel(R) Core(TM) i5-4300U (4) @ 2.90 GHz
GPU: Intel Haswell-ULT Integrated Graphics Controller @ 1.10 GHz [Integrated]
Disk (/): 111.49 GiB / 465.76 GiB (24%) - btrfs

I will wait for someone experienced on this forum to reply here.

I use a swapfile, no need for partitions IMO… and if you don’t hibernate then no need for it to match your RAM either.

However, you only have 4GB installed, so that’s always gonna be very tight.

2 Likes

sudo mkswap -U clear --size 4G --file /swapfile

Activate the swap file:

sudo swapon /swapfile

Finally, edit the fstab configuration to add an entry for the swap file:

sudo nano /etc/fstab

and add a new line for permanent use at every boot:

/swapfile none swap defaults 0 0

Take a look here: Swap - ArchWiki

2 Likes

The user has btrfs, so having the swapfile like this, could lead to problems.

2 Likes

Okay I’ll read he wiki instructions first and follow

Yes that is why I asked here on forum, stuff like this that articles may neglect
I’ll read the wiki in mean time

2 Likes

oh gosh… I forgot that.. thanks :wink:

@AwwsmGaurav please follow the first part here, so 1, 2 and 3. Hibernation is optional.

5 Likes

Okay I read the arch wiki Swap - ArchWiki and BTRFS Swap File - ArchWiki
I understand how swap and swap files work now.

Then I followed :

Below are all the commands and their outputs during this process, I am yet to reboot.
If anyone can take a look at it to confirm it’s fine, It’ll be helpful and I’ll proceed to reboot…


    ~  export fs_uuid=$(findmnt / -o UUID -n) && echo ${fs_uuid}                                                           1 ✘ 
ab1f84f1-6ca3-43bb-be66-d7a9dda7067a
    ~  sudo mount -m -U $fs_uuid /mnt/system-${fs_uuid}                                                                      ✔ 
[sudo] password for gaurav: 
    ~  sudo btrfs subvolume create /mnt/system-${fs_uuid}/@swap                                                              ✔ 
Create subvolume '/mnt/system-ab1f84f1-6ca3-43bb-be66-d7a9dda7067a/@swap'
    ~  sudo umount /mnt/system-${fs_uuid}                                                                                    ✔ 
    ~  sudo mount -m -U ${fs_uuid} -o subvol=@swap,nodatacow /swap                                                           ✔ 
    ~  export swp_size=$(echo "$(grep "MemTotal" /proc/meminfo | tr -d "[:blank:],[:alpha:],:") * 1.6 / 1000" | bc ) && echo ${swp_size}m
5901m
    ~  sudo btrfs filesystem mkswapfile --size ${swp_size}m --uuid clear /swap/swapfile                                      ✔ 
create swapfile /swap/swapfile size 5.76GiB (6187646976)
    ~  sudo umount /swap                                                                                                     ✔ 
    ~  echo -e "UUID=$fs_uuid\t/swap\tbtrfs\tsubvol=@swap,nodatacow,noatime,nospace_cache\t0\t0" | sudo tee -a /etc/fstab    ✔ 
UUID=ab1f84f1-6ca3-43bb-be66-d7a9dda7067a       /swap   btrfs   subvol=@swap,nodatacow,noatime,nospace_cache    0       0
    ~  echo -e "/swap/swapfile\tnone\tswap\tdefaults\t0\t0" | sudo tee -a /etc/fstab                                         ✔ 
/swap/swapfile  none    swap    defaults        0       0
    ~  sudo systemctl daemon-reload                                                                                          ✔ 
    ~  sudo mount /swap                                                                                                      ✔ 
    ~  sudo swapon -a                                                                                                        ✔ 
    ~  swapon -s                                                                                                             ✔ 
Filename                                Type            Size            Used            Priority
/swap/swapfile                          file            6042620         0               -2
    ~ 
2 Likes

:trophy: :tada:

Memory: 2.19 GiB / 3.52 GiB (62%)
 Swap: 5.59 MiB / 5.76 GiB (0%)

If you aren’t already using ZRAM, it would be a real help with 4 GB of RAM:

sudo pacman -S zram-generator
sudo systemctl enable zram-generator --now
3 Likes

❯ cat /etc/systemd/zram-generator.conf

     │ File: /etc/systemd/zram-generator.conf
─────┼─────────────────────────────────────────────────────────────────────────────────
   1 │ [zram0]
   2 │ zram-size = ram   # Use your RAM size in MiB (16GB = 16384)
   3 │ compression-algorithm = zstd
   4 │ swap-priority = 100

Heck yes.

NAME       TYPE      SIZE USED PRIO
/swapfile  file        4G   0B   -1
/dev/zram0 partition  16G   2G  100

This takes me back to Amiga days, where we would use a compressed ‘disk’ in ‘fast memory’ to keep things running looser - but without the compression.

This basically expands your RAM for a small price.

2 Likes

Nowadays zswap is recommended over zram, as it is better at managing what pages get compressed in the RAM, and moving stale pages from the RAM to the disk. I switched from zram to zswap a few days ago after seeing the following article listed in this week’s Web Review, Week 2026-13 - ervin:

I also did some searching on the web before I made the change, and the almost unanimous consensus is that zswap is now the best way to go. Here’s another article from last September:

zswap on my system:

swapon && free -h 

NAME      TYPE SIZE  USED PRIO
/swapfile file  16G 32.2M   -1

               total        used        free      shared  buff/cache   available
Mem:            28Gi       6.8Gi       1.5Gi       508Mi        18Gi        21Gi
Swap:           15Gi        32Mi        15Gi
sudo grep -r . /sys/kernel/debug/zswap
[sudo] password for scotty: 
/sys/kernel/debug/zswap/stored_incompressible_pages:0
/sys/kernel/debug/zswap/stored_pages:6396
/sys/kernel/debug/zswap/pool_total_size:5160960
/sys/kernel/debug/zswap/written_back_pages:1090
/sys/kernel/debug/zswap/decompress_fail:0
/sys/kernel/debug/zswap/reject_compress_poor:0
/sys/kernel/debug/zswap/reject_compress_fail:0
/sys/kernel/debug/zswap/reject_kmemcache_fail:0
/sys/kernel/debug/zswap/reject_alloc_fail:0
/sys/kernel/debug/zswap/reject_reclaim_fail:0
/sys/kernel/debug/zswap/pool_limit_hit:0
3 Likes

Wow that is an awesome recommendation!
I was coompletely unaware of such thing as zRAM
I just got it set up, and set the swappiness to 30, and I can already feel the smoothness.

Its like I have so much ram to open multiple apps now!

vm.swappiness = 30
NAME           TYPE      SIZE   USED PRIO
/swap/swapfile file      5.8G 908.8M   -2
/dev/zram0     partition 3.5G 144.1M  100

[zram0]
zram-size = 3604
compression-algorithm = zstd
swap-priority = 100

I am getting framework 12 soon, so I’ll give it a try!

3 Likes

zswap is ridiculously easy:

sudo grep -r . /sys/kernel/debug/zswap
[sudo] password for ben: 
/sys/kernel/debug/zswap/stored_incompressible_pages:0
/sys/kernel/debug/zswap/stored_pages:0
/sys/kernel/debug/zswap/pool_total_size:0
/sys/kernel/debug/zswap/written_back_pages:0
/sys/kernel/debug/zswap/decompress_fail:0
/sys/kernel/debug/zswap/reject_compress_poor:0
/sys/kernel/debug/zswap/reject_compress_fail:0
/sys/kernel/debug/zswap/reject_kmemcache_fail:0
/sys/kernel/debug/zswap/reject_alloc_fail:0
/sys/kernel/debug/zswap/reject_reclaim_fail:0
/sys/kernel/debug/zswap/pool_limit_hit:0

❯ swapon && free -h
NAME      TYPE SIZE USED PRIO
/swapfile file   4G   0B   -1
               total        used        free      shared  buff/cache   available
Mem:            14Gi       5.8Gi       5.0Gi       176Mi       4.4Gi       9.2Gi
Swap:          4.0Gi          0B       4.0G
3 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.