0

About Linux Swap, for resize purposes I found three approaches

  1. Delete the current /swapfile and create a new one with the new required size
  2. Add more gigas as required for the current /swapfile
  3. Add another Swap file

NOTE: be sure to disable first swap through either sudo swapoff -v /swapfile or sudo swapoff -a commands

The approaches 2 and 3 are mentioned/covered here:

The approach 1 is covered here:

Question

  • What is the recommendable approach? and Why?

Extra Questions

  • When would be mandatory and what is the advantage to add another /swapfile? - therefore /swapfile2 ... etc
  • How does the kernel work with 2 or more swap files? I am assuming there are some advantages ...
  • Exists a recommendable limit to add swap files? only to up 3 for example?

Normally I used to work with the swap partition approach where only existed one partition, so, this approach about to add some swap files at a first glance is rare/unsual

Manuel Jordan
  • 1,768
  • 7
  • 30
  • 50

2 Answers2

4

As you've clearly done your research, let's see if we can go through the questions one by one.

Q. What is the recommendable approach? and Why?

The ideal approach is the one you're most comfortable with, simply because you're more comfortable with it.

One of the biggest advantages that I've seen with swap files over swap partitions is the simplification of swap management for people who really couldn't care less about how a computer works. Partitions are 70s-era constructs with 70s-era rules dictating how people should and should not interact with them. This is fine for operating systems, but not for people. By allowing ordinary people to add/remove swap space without fear of breaking their partition tables and losing all the important data they've never once thought about backing up, the process becomes less intimidating.

Q. When would be mandatory and what is the advantage to add another /swapfile? - therefore /swapfile2 ... etc

The only time adding swap is mandatory is when your system runs out of memory and locks up hard, forcing a hard reboot. The advantage of adding another file is that your system can continue to work through memory-intensive workloads for longer without completely locking up ... though the I/O load may exceed ideal thresholds with the bulk of the operations being swap thrashing.

An example of this can be seen with applications that use millions of "little" files, particularly those found in academia where researchers attempt to create simulations taking into account trillions of conditions (weather mapping, climate models, astrophysics, etc.).

Q. How does the kernel work with 2 or more swap files? I am assuming there are some advantages ...

When one is full, the next is used. There is no balancing of data across the swap files, as this would incur additional performance penalties.

Q. Exists a recommendable limit to add swap files? only to up 3 for example?

To the best of my knowledge, the maximum number of swap files a system can have is limited only by the number of files a given file system is configured for. You can check this with the following command:

cat /proc/sys/fs/file-max

If you are using a modern system, you will probably see a number that looks like this:

9223372036854775807

9.2-quintillion files ought to be enough for everybody.

matigo
  • 22,138
  • 7
  • 45
  • 75
  • 2
    Perhaps a detail to correct: multiple swap spaces with the same priority are used on a "round-robin" basis, i.e., simultaneously, once writing to one, then to the other. With different priorities it is as you indicate: the higher priority is filled first, then the next one. See man 2 swapon – vanadium Apr 06 '22 at 06:57
0

Option 1 seems useless. Why create a new one when you can do #2? (unless corrupted or similar)

Option 2 appears the best from my eyes, if that's possible.

Option 3, I don't believe having many swaps on one FS is a good idea because the kernel may try to do simultaneous writes slowing down the disk.

Add an option 4: Create a swap partition. Depends on preference & FS, but I prefer this.

0xLogN
  • 73
  • 11