0

What is the technical minimum size for a functional (holds some pages) swap partition or file on Linux. If it's architecture dependent, or depends on the size of physical memory or something, how would I calculate an estimate?

I'm not asking for recommendations, or viable sizes for a modern full-scale system. Just how low can it go and still function as swap

davolfman
  • 479
  • 2
  • 7

2 Answers2

4

So a swap file has some overhead because of header information and stuff.

If you try a too small file... in this case 1 byte

# dd if=/dev/zero of=tst1 bs=1c count=1
1+0 records in
1+0 records out
1 byte (1 B) copied, 0.000141358 s, 7.1 kB/s
# ls -l tst1
-rw-r--r-- 1 root root 1 Mar 10 22:19 tst1
# mkswap tst1
mkswap: error: swap area needs to be at least 40 KiB

So we need at least 40k (at least on RedHat 7 on x86_64)

# dd if=/dev/zero of=tst1 bs=1c count=40960
40960+0 records in
40960+0 records out
40960 bytes (41 kB) copied, 0.183741 s, 223 kB/s
# mkswap tst1                              
Setting up swapspace version 1, size = 36 KiB
no label, UUID=4d559295-45c6-4952-8c14-f8eb55f3c201
# swapon tst1
swapon: /home/sweh/tst1: insecure permissions 0644, 0600 suggested.
# cat /proc/swaps 
Filename                                Type            Size    Used    Priority
/home/sweh/tst1                         file            36      0       -2

And that provides 36K of swap.

Stephen Harris
  • 42,369
  • 5
  • 94
  • 123
  • So the minimum is about 10 pages, with at least one page being used for bookkeeping at that size? – davolfman Mar 11 '22 at 19:50
  • https://github.com/util-linux/util-linux/blob/master/disk-utils/mkswap.c defines `MIN_GOODPAGES` as 10, so I'd guess that sounds about right. – Stephen Harris Mar 11 '22 at 20:39
0

You can get the page size of your system by entering the following

getconf PAGE_SIZE

My system returns 4096 as the current page size, or 4kB. This is also the minimum page size, and on some systems, the pages can be much larger. See this answer for more details.

That being said, to get an estimate for a minimal functional size, you should take into account that the smaller the size of the swap space, if it's being used, the more page faults will occur, which will slow the performance of the computer, as more and more time will be spent retrieving pages from memory.

  • So I guess the lowest it can possible go would be 4kB, and although it would function, it would probably not do so very well. – Braden Carlson Mar 11 '22 at 02:21