I'm using a Raspberry Pi 4 with 4GB and one with 8GB for docker/ pi-hole. Currently I use the default settings for the swap file and know with the older Raspberry Pi modells with less RAM, the suggestion was to change the swap file to a HDD. I have old 2.5" drives but 1.) Do I still need a swap file or could I disable it? 2.) how to I measure the performance
-
disable swap is not recommanded as, when the OS decides it needs to swap, a non existing swap will result in a non recoverable failure (in most cases). Without Swap, the OS will call the OOM killer which tries to get some free ram... but ists not a feature intended for that problem... its just for a stalling system to keep logging, etc... up as long as possible (to my understanding) and chances are: there is no free ram, or it kills something you need for your system providing ists services – schnedan Feb 08 '22 at 12:36
-
"its just for a stalling system to keep logging, etc... up as long as possible" -> No, that's not really the case. Nor will it save you from the OOM killer because swap is also finite. The point of it is to allow you to run more software at one time. When RAM usage reaches a certain level, the kernel will begin to "swap out" the data that has/is being accessed the least to swap (which can have various forms including a file, a partition, and compressed normal mem), on the premise that wherever it is, it is slower to access than RAM. Point being, it is not an emergency system. – goldilocks Feb 08 '22 at 14:22
1 Answers
You can run without swap, but it is not advisable. If you are short on memory, either because you don't have much RAM or because you have memory-intensive processes running, you will probably need swap.
But then, how large should your swap space be? As a first idea, twice the size of RAM if RAM is less than 2 GB or size of RAM + 2 GB if RAM size is more than 2 GB. But that's a starting point.
If you really want to know, use vmstat. In the column swpd, you can see how many kB swap you're using. If swpd is consistently 0, you may try to run without swap, if you really want. If it is non-zero, swap is being used. And then it was good that you have it.
The next question would be whether you should put the swap on an external drive. The reasoning behind this is that using an external drive saves writes to the SD card and that will prolong the life of the card. In general: if your swap is heavily used, you will use an external drive. If there is little or no activity, you might leave the swap on the SD card. How do you see whether the swap is actively used? Again, vmstat will show you:
- If the columns
siandsoare non-zero, you have swap activity. - A wildly varying value in the column
swpdis also an indication of swap activity
And now for some examples.
My DNS server has quite sufficient memory to run bind9. This is a sample of the vmstat:
ljm@phi:~$ vmstat 60
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 544664 47520 241992 0 0 0 1 1 1 0 0 100 0 0
0 0 0 544664 47520 241992 0 0 0 0 337 35 0 0 100 0 0
0 0 0 544664 47520 241992 0 0 0 0 378 41 0 0 100 0 0
0 0 0 544664 47520 241992 0 0 0 0 366 37 0 0 100 0 0
^C
This means that the pi could probably do without swap
Pi is my mail server.
ljm@pi:~$ vmstat 60
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 16096 11744 57116 314056 0 0 4 4 3 3 4 24 54 19
1 0 16096 11504 57132 314088 0 0 0 2 1245 1892 0 1 99 0
1 0 16096 11184 57504 311892 0 0 15 6 1265 1938 3 4 93 0
0 0 16096 12012 57872 310632 0 0 1 77 1304 2040 7 9 81 4
1 2 16096 17468 58072 305464 0 0 1169 119 1724 2760 6 14 65 16
3 0 16096 15088 56792 308452 0 0 4390 10 3351 6941 10 50 0 39
3 0 16096 10376 58140 306128 0 0 5212 6 3267 5848 13 45 0 42
1 0 16096 17972 58172 304368 0 0 1515 4 2115 4312 4 22 61 13
1 0 16096 15740 58220 306520 0 0 35 10 1255 1912 3 2 93 2
You see swap > 0. So a swap space here is also being used. However si and so are zero and swpd is consistently 16096. So, your swap could be on the SD card.
Last example I plucked of the Internet:
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
3 1 244208 10312 1552 62636 4 23 98 249 44 304 28 3 68 1 0
0 2 244920 6852 1844 67284 0 544 5248 544 236 1655 4 6 0 90 0
1 2 256556 7468 1892 69356 0 3404 6048 3448 290 2604 5 12 0 83 0
0 2 263832 8416 1952 71028 0 3788 2792 3788 140 2926 12 14 0 74 0
0 3 274492 7704 1964 73064 0 4444 2812 5840 295 4201 8 22 0 69 0
You see so is non-zero. And swpd varies wildly. You have a RAM shortage ans swapping helps to virtually increase the memory. Because swap is so active, it would be wise to use an external drive for swap.
- 2,491
- 9
- 15
-
While this is mostly a decent answer, some of your assertions implying that if swap is used means it is required are a bit strong; if swap is enabled, it will be used long before you actually run out of RAM and in that sense need it. Search online for "linux swapiness," the primary setting that controls how it is triggered. It is not uncommon to adjust this as some people feel swap by default is used too eagerly to no benefit (which is perhaps a bit too tinkery, but makes the point that it happens in normal, lots of RAM still available situations). – goldilocks Feb 08 '22 at 15:49
-
Here's a good article which also directly addresses the errors of some less good explanations: https://www.howtogeek.com/449691/what-is-swapiness-on-linux-and-how-to-change-it/ ...Unfortunately a bit ad heavy. Swap is a bit like climate control. That your AC comes on above a certain temperature does not mean your house was about to catch fire or someone would die from heat exhaustion, etc. There are conventions about room temperature, personal preferences, cost-benefit analysis etc. – goldilocks Feb 08 '22 at 15:55
-
To be honest, my last ordeals with the OOM-killer where on AIX 4 together with SIGDANGER problems. – Ljm Dullaart Feb 08 '22 at 16:22