Questions about resizing swap partition and swap in general

My swap partition is only 2GB (I didn’t check to expand during install), and I want to expand it. I have 16GB of RAM.

Edited to add: I have full disk encryption with LUKS

I’ve had some system slowdown with kswapd0 using a lot of CPU when swap is full. Maybe it was because almost all the free RAM was cached. I don’t know much about this.

output of fdisk -l at bottom.

I have some questions about what is smart to do:

  1. Use yast partitioner? If yes, would I first have to shrink root, then expand swap? Can I do this on the fly? Can I rollback with snapper if something fails at reboot?
  2. Use gparted or some other tool?
  3. Use a swapfile and add it do fstab? If yes, could I then delete the swap partition? Is there any specific place swapfiles should be placed?
  4. Should I change system swappiness?
  5. Should swap size match RAM size?

Thanks for any help.

fdisk -l 
**Disk /dev/nvme0n1: 476.94 GiB, 512110190592 bytes, 1000215216 sectors**
Disk model: PC SN730 NVMe WDC 512GB                  
Units: sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disklabel type: gpt  

**Device****  Start****       End****  Sectors****  Size****Type**
/dev/nvme0n1p1    2048    1050623   1048576   512M EFI System 
/dev/nvme0n1p2 1050624 1000215182 999164559 476.4G Linux LVM 


**Disk /dev/mapper/cr_nvme-PC_SN730_NVMe_WDC_512GB_210524802564-part2: 476.44 GiB, 511570157056 bytes, 999160463 sectors**
Units: sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 


**Disk /dev/mapper/system-root: 474.43 GiB, 509419192320 bytes, 994959360 sectors**
Units: sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 


**Disk /dev/mapper/system-swap: 2 GiB, 2147483648 bytes, 4194304 sectors**
Units: sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes

I will not try to answer all of your questions, because some of them are about “How to execute the plan” and the others are “What plan should I make”.

With 16GB of memory, what are you doing that you still need swap for swapping?

With 2BG of swap and 16GB of memeory you can not suspend to disk, but as you do not complain about that, I assume you are not wanting to do suspend.

You can add swap space by using swap files (as you hinted already), those are added with swapon (and removed with swapoff). Nice for trying. You can also define to swapon on boot using /etc/fstab. See

man swapon
man fstab

Of course using a swap file (e.g. in the / partition) would make it unneeded to resize partitions (and thus file systems).

But in your case, it seems that your / file system and swap space are on Logical Volumes. Thus no changing of partitions is needed, changing of LVs (and the file system inside) is needed.

Just adding my 2cents to what @hcvv already pointed out.

  1. You should not tinker with system parameters unless you really know what you are doing and why since dramatic side effects are possible (and at the moment you don’t appear to have a clear picture …)
  2. swap size should match RAM size only to ensure suspend to disk (and even in that case most of the time a swap about half as large might be enough due to disk image compression).
  3. When you actively use swap, the system slows down even with a larger swap; of course if your swap is regularly near to full you are stressing your system beyond its capabilities and something must be done, but just enlarging the swap looks like a band-aid.

@zimz:

For the current state of play –

kernel: Lockdown: swapper/0: hibernation is restricted; see man kernel_lockdown.7

The openSUSE default of 2 GB for the Swap partition, is an average for the industry consensus of opinion for the case of “enough main memory” and “no need to hibernate” …

When the current issue with Hibernation/Encryption/Swap is solved, then you’ll need “amount of active Memory” as the size of the Swap partition if, you need to be able to Hibernate …

Turn off swap and run the machine without swap. Works fine here:

**3400G:~ #** inxi -SCm 
**System:    Host:** 3400G **Kernel:** 5.12.0-2-default x86_64 **bits:** 64 **Console:** tty pts/1 **Distro:** openSUSE Tumbleweed 20210510  
**Memory:    RAM:****total:** 29.27 GiB **used:** 3.49 GiB (11.9%)  
           **Array-1:****capacity:** 128 GiB **slots:** 4 **EC:** None  
           **Device-1:** DIMM_A1 **size:** No Module Installed  
           **Device-2:** DIMM_A2 **size:** 16 GiB **speed:** 2133 MT/s  
           **Device-3:** DIMM_B1 **size:** No Module Installed  
           **Device-4:** DIMM_B2 **size:** 16 GiB **speed:** 2133 MT/s  
**CPU:       Info:** Quad Core **model:** AMD Ryzen 5 3400G with Radeon Vega Graphics **bits:** 64 **type:** MT MCP **cache:****L2:** 2 MiB  
           **Speed:** 1724 MHz **min/max:** 1400/3700 MHz **Core speeds (MHz):****1:** 1724 **2:** 1258 **3:** 1259 **4:** 1260 **5:** 1285 **6:** 1258 **7:** 1258 **8:** 1327  
**3400G:~ #**

If you constantly run out of RAM double its size.

As @dcurtisfra pointed out already in comment #4, running without any swap space is not a good idea in general, even if the system can still perform satisfactorily if RAM is “double” than needed, which is not the case for the OP apparently…

Thank you for your replies. I guess what I’m going try to do is increase the swap size and lower swappiness. Let’s see how that goes.

Yeah, hint taken, I have a well established unfocused way of asking questions, I’m working on it. Should probably just read the docs eh. I guess mostly what I’m wondering is, is it easy and safe to resize logical volumes on the fly (not partitions, sorry and thank you), and does encryption matter, and then suddenly I had a few other questions, heh.

Well, 16GB is not huge, you know. Hop back and forth between two-three virtual desktops for work etc, don’t reboot for a good while, maybe a code editor or ide open on one with some big-ish files, maybe more firefox windows and tabs loaded (from a session you’ve had going for a year) than is probably reasonable, might start up a VM with 4GB of dedicated memory, a couple of videos loaded and play some music, then boom goes the dynamite.

Well, here’s the thing, with Linux caching so much to RAM (not a byte wasted they say… I get that it’s a feature) it fills up fast for me due to my usage pattern, and swappiness seems high to me as swap will fill up while still having a a few gigs of RAM free due to 7-8GiB of RAM being taken up by cache. Over the years I’ve had enough severe problems with oom killing (having to hard reboot) that I like to have an out in the form of swap. I figured if I have low swappiness but a lot of swap it’ll keep me safe in my crazy usage. I get that I should probably change my usage patterns, but it is what it is.

I get this in theory, but in my practical case it’s different. I don’t think the oom killer always performs well. Call me a blade runner, living on the edge of RAM, but the oom killer is causing me problems. Yesterday I had a few GB RAM available but most of it was cached, the 2GB swap was already full, then I opened dolphin and the system ground to a halt because of oom killer, as it couldn’t dump the cache quick enough I guess. Checking the journal, the oom-killer was invoked and everything just stopped for a while until I hard rebooted as the laptop was about to melt.

Edit to add:

I think I would have to seriously change my habits (which I probably should tbh) to run without swap.
Unfortunately I’m stuck at 16 GB RAM and can’t upgrade.

Having some swap used while RAM is still available is not bad in principle. If “sleeping” processes that are not going to be reclaimed soon are swapped, you just have more RAM available for your active processes.
Trouble begins when “active” processes (virtual desktops e.g.) are being swapped because you just don’t have enough RAM to run them all at the same time. If that is the case, enlarging swap will save your system from crashing and needing a hard reboot but you will still experience an unresponsive, “clogged” system from time to time since swapping in and out active processes is waaaay slower than working from RAM.
And lowering swappines is not going to help. Very low values are telling the system to “not use swap if at all possible”, so the system tries to swap out smaller chunks while active processes keep asking for more RAM over and over, clogging the system bus even more.
I would not worry about “Linux caching so much to RAM” unless you have large streaming processes (video editing e.g.) or an unusual cache config for your disks, Linux should not “steal” RAM from active processes just to cache files that it can reclaim from stored files anyway.
And restarting Firefox from time to time is a good idea, it IS a memory hog these days, especially if streaming media including ads are playing…

With the same situation but without a swap the OOM killer would have triggered earlier (memory allocation has lower margins without swap) and a hard reboot unavoidable anyway. Maybe if you waited long enough (15-30 minutes?) the system was still able to recover by itself…

Sure, but as it stands, the 2GB of swap is completely filled very early, even with free and non-cached memory. Is that normal behavior?

Yeah clearly I should change my habits to something more compatible with only 16GB of RAM, but failing that, isn’t it good to have some overflow so that the system doesn’t completely freeze? Of course it is slower than working from RAM (and I do appreciate the way Linux uses RAM as a cache, I didn’t mean to make fun of it) but it certainly isn’t slower than a complete halt.

And lowering swappines is not going to help. Very low values are telling the system to “not use swap if at all possible”, so the system tries to swap out smaller chunks while active processes keep asking for more RAM over and over, clogging the system bus even more.

Thanks for the info. So clearly I have some reading to do about swappiness and how it works. My thinking was basically that, yes, you kind of run a system without swap (as @karlmistelberger wrote), but then use swap when you have to because my experience with the oom-killer is that it kicks in too late when you are on the edge (my problem with the 2GB of swap and it being filled so soon was (it seems to me) exactly that). Then when I notice, I’ll take corrective measures like dumping stuff hogging the RAM etc.

Yeah I didn’t mean to criticize the RAM caching, I get that it’s good and allows whatever is grabbed from there to run faster. I do try to unload some of my fx tabs (or restart it as you suggest) but sometimes I get caught up in what I’m doing and things get full and everything stops. No (well, a select few) ads here FWIW. Again, clearly I need to change some habits, but failing that, it seems like a larger swap is a good idea for people like me.

OK, so clearly I have some reading to do about how the oom-killer works as well. I made the assumption that it should be able to dump the cached stuff quicker and allocate memory on the fly when you are running out of memory, and also have a full swap. Alternatively that it would pick a process to kill sooner/faster.

I’ll try to educate myself a bit on the subject.

IMHO the question is still very simple. When you do not look at all the numbers hat are frightening you, do you think the system is swapping? I have used systems swapping in my life. It is something you detect very easy. On starting something, or even on switching from one open GUI application to another, the disk will start to work very heavily and you very notable have to wait until the application you want to do something is answering. No doubt.

My approach to this always was very practical. As long as you do not experience this, there is nothing wrong re swap. No looking at all sorts of number needed.

Of course it is an interesting hobby to go through all the data available on how your system behaves, but that is something else.

Just my 2 cents.

I am running host erlangen with swap disabled since around 2017, but I never needed to reset the machine. While I am typing this top shows:

Tasks: 5276 total,  24 running, 5065 sleeping,   0 stopped, 187 zombie 
%Cpu(s):  2.9 us, 97.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.1 si,  0.0 st 
MiB Mem : 32046.61+total, 7951.066 free, 11823.36+used, 12272.18+buff/cache 
MiB Swap:    0.000 total,    0.000 free,    0.000 used. 19531.19+avail Mem  

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                                                                                                                          
19205 karl      20   0   17108   6136   1404 S 100.0 0.019   0:00.01 bash                                                                                                                                                                                                                             
16076 karl      20   0   16868   5660   1132 S 100.0 0.017   0:00.00 bash                                                                                                                                                                                                                             
17682 karl      20   0   17024   5704   1132 S 100.0 0.017   0:00.00 bash                                                                                                                                                                                                                             
    1 root      20   0  169400  14320  10152 R 7.556 0.044   0:53.07 systemd                                                                                                                                                                                                                          
14772 root      20   0   19416  12968   6040 R 4.000 0.040   0:00.14 top                                                                                                                                                                                                                              
 1315 karl      20   0 4476660 275300 130488 S 2.222 0.839  14:18.20 kwin_x11                                                                                                                                                                                                                         
 1093 root      20   0 1073200  19268  12288 S 1.333 0.059   0:09.89 udisksd                                                                                                                                                                                                                          
 1157 karl      20   0   17168   6308   1468 S 0.889 0.019   0:00.04 bash                                                                                                                                                                                                                             
 1376 karl      20   0 2911108 377980 163504 R 0.889 1.152   3:40.93 plasmashell  

The mess was created by deliberately starting a fork bomb. However no hard boot is necessary.

Well yeah, you have 8GB free, so why would you need a hard reboot.

Here is mine

[FONT=monospace]free -m 
               total        used        free      shared  buff/cache   available 
Mem:           15717        6849        1114        3221        7753        5309 
Swap:           2047           0        2047



[/FONT]

Is it normal for the system to start using the swap instead of dumping the cache at normal swappiness levels? Beucase that’s what it seems to start doing:

free -m 
               total        used        free      shared  buff/cache   available 
Mem:           15717        8226         159        3583        7331        3663 
Swap:           2047          16        2031



I mean, I agree with you. And that is exactly when I start to notice it. But as has happened a few times, I don’t have time to kill anything once I notice this, because swap has already been filled (probably not with running processes which is why I don’t notice immediately?) but the oom-killer doesn’t kill any of the stuff cached in RAM quick enough it seems like?

It’s not as if I’m not having practical problems and just looking at the scary numbers and worrying, I am experiencing what you’re describing, at which point I kill processes (this is my idea anyway, when swap starts to get used, things get slow, so I kill stuff) but what happens on occasion is that everything stops completely even though there is stuff cached in RAM which can be dumped (?) and I have to hard reboot (unless I wait half an hour).

Yeah, no doubt @karlmistelberger can get along without a swap at current load…

Here is mine

[FONT=monospace]free -m 
               total        used        free      shared  buff/cache   available 
Mem:           15717        6849        1114        3221        7753        5309 
Swap:           2047           0        2047
[/FONT]

Is it normal for the system to start using the swap instead of dumping the cache at normal swappiness levels?

Yes, it might be normal to swap the memory space of processes that have not been active for a while instead of dumping the buffer of an active process (mind “buffer/cache” in memory statistics, so not necessarily “cache” strictly speaking).

Beucase that’s what it seems to start doing:

free -m 
               total        used        free      shared  buff/cache   available 
Mem:           15717        8226         159        3583        7331        3663 
Swap:           2047          16        2031

I’m no kernel specialist, but my understanding is that the oom-killer just kills a process when the memory allocation manager is unable to reclaim back memory the ordinary way, so when any attempt to dump buffers to disk or to clear cached content could not do anything better for any reason. Please be aware of the following points:
1- “buffer” : data stored in RAM waiting to be flushed to disk (you cannot simply clear it and reclaim RAM back)
2- “cache” : data stored in RAM for speedy retrieval, but also available on disk, so it can be immediately cleared from RAM
3- in case of need, flushing “buffers” to disk or swapping memory maps to disk engage the same system bus in a small system, so there is no fast way to “kill stuff already in cache” if that stuff is, indeed, in a “buffer”.
So, when you notice that the system is actively swapping, wait for the system to be responsive again, then kill what you don’t need at that time.
Please note that low swappiness is intended for systems with large RAM allowance (like the one referenced by @karlmistelberger e.g.) since the system is likely to be unresponsive for longer in the (rare) event of swap being actively needed.
With early swap filling like in your system, default swappiness should allow a reasonable recovery time in most instances (whether or not that seems “reasonable” to you is another matter…).

It’s not as if I’m not having practical problems and just looking at the scary numbers and worrying, I am experiencing what you’re describing, at which point I kill processes (this is my idea anyway, when swap starts to get used, things get slow, so I kill stuff) but what happens on occasion is that everything stops completely even though there is stuff cached in RAM which can be dumped (?) and I have to hard reboot (unless I wait half an hour).

A larger swap might help, especially if you keep a memory monitor open and stop inactive applications whenever swap usage goes beyond, say, 1 GB or so.
With the default vm.swappiness=60 I experienced “acceptable” recovery times from a few seconds to a minute or so on an ordinary desktop, growing to half an hour or worse with the extreme vm.swappiness=1 with some VirtualBox running (the system seemed frozen but was still active and eventually recovered!)
You may experiment with the range 50-10 used on some servers, but, as written, don’t expect too much from that alone.

Just an example to show what I am meaning.
Previous post written from a system with:

localhost:~ # free -m 
               total        used        free      shared  buff/cache   available 
Mem:           15882        3500        9473         561        2909       11491 
Swap:           8191           0        8191 
localhost:~ #

Then started a number of VMs to fill up RAM, current status is:

localhost:~ # free -m 
               total        used        free      shared  buff/cache   available 
Mem:           15882       14731         244         517         907         346 
Swap:           8191        1407        6784 
localhost:~ #

Nearly half of the original memory usage has been swapped, still about 1GB of buffer/cache and the system is responsive as usual (only “sleeping” processes have been swapped?)
Now please don’t ask me to open another VM just to see for how many minutes the system becomes “frozen” lol!

System still usable, writing this and music playing smoothly with

localhost:~ # free -m 
               total        used        free      shared  buff/cache   available 
Mem:           15882       15131         156         370         594         104 
Swap:           8191        6307        1884 
localhost:~ #

desktop a bit snaggy, some active swapping going on, not trying to open more apps to avoid tripping the oom-killer.
As you can see, memory management flushed almost all buffers/caches before completely filling up the swap space.

Swap on NVME drive works very fast. With Intel Optane it will work even faster.

To OP:

  1. You can use more than 1 device for swap with Linux. You may use your swap partition and add swap file. Linux has priorities for swap devices.
  2. Check zram/zswap/zcache.
  3. You may add Intel Optane drive specially for swap (16 - 32 GB or more).