How big should my swap partition be?

Hi all! Hope everyone is having a great Easter weekend so far despite being stuck at home due to this coronavirus crap :stuck_out_tongue:

I thought about adding this question to my existing thread at Likely performance gains after setting up a swap partition on an Archer C7, but that thread is already pretty overcrowded, so I decided to make a new one instead. If a moderator feels like it should be tacked on to that thread, please feel free to do so or LMK :wink:

After reading the advice given in that thread, I am venturing into the land of fdisk partition management and fstab configuration today, then I will attempt to enable SQM and see what kind of performance gains I get. But first thing's first: how big should the swap partition be?

I know that it's an old rule of thumb to use double the amount of RAM in your machine for those with 512MB of RAM or less, and this docs page suggests that the rule still applies to embedded devices. But if I have the space on my extroot drive for a 1GB partition, why not use it?

Would a swap partition that large just be wasted drive space? Or could I expect it to be helpful? MTIA for any advice you guys can provide :smiley:

I seem to remember reading somewhere that using too much swap on a router could actually hurt performance, so it's probably best to stick to the old rule of thumb, I don't use swap though so maybe someone who does will run across this and be able to say for sure.

Thanks mate, I'll go with 256MB for now and see how I go :smiley:

I'd still be interested to know whether a 512MB swap would cause any problems or result in wasted space, for anyone that sees this and can provide any insight. MTIA! :smiley:

You can use the free command on your router to get information about memory and swap usage:
OpenWrt-device without swap:

root@BTHH5A:~# free
              total        used        free      shared  buff/cache   available
Mem:         123160       56300       54412          76       12448       37044
Swap:             0           0           0
root@BTHH5A:~# 

OpenWrt-device with swap:

root@router:~# free
              total        used        free      shared  buff/cache   available
Mem:          58684       31516       19116         388        8052       11528
Swap:        976560        3352      973208
root@router:~# 

Note, on this 64MBram device I configured ~1GB swap on a USB2 stick (not because I think I will ever need it, just because on the 16GB stick 1 GB was cheap enough to not bother*)
I also added the following to /etc/rc.local, to reduce the amount of RAM potentially used by the tmpfs to 16MB (on a 64MB router this defaults to >> 16 MB, and I really really want to avoid OOM ;)):

# minimize the too large tmpfs
mount tmpfs /tmp -t tmpfs -o remount,size=16000k,nosuid,nodev

*) I realize that going to swap is going to be painful performance wise, especially USB2 backed swap, but I prefer that to OOM...

Nice! Have you noticed a significant change in performance since making these changes? I'm just about to setup a 512MB swap partition for my 128MB router; since you've gone more than 16x the size of your RAM I reckon it should be fine for me to go 4x!

I just wonder whether this will really be worthwhile though...guess the only way to know for sure is to try it!

Thanks again everyone :smiley:

The old rule-of-thumb about swap being twice the RAM dates to a couple of decades back, when the Linux kernel needed such a swap space. But that necessity no longer exists.

Now, if you need lots of swap space, then performance is going to hurt, because swap is the slow alternative to RAM. On the other hand, if you do not have as much swap as you need, then you are going to experience errors when RAM is full and the kernel has to start kicking processes.

Having a bit of swap can improve performance, on systems with lots of disk I/O, as the RAM freed by the swap can be used as cache. Ona router, I doubt it will have any impact.

3 Likes

I understand that, but don't understand why everyone seems to say this. Wouldn't the router, or any device for that matter, continue using RAM only until it needs to use swap?? If not, it seems to me as though swap/page files are almost useless...

EDIT: Just realised that you did say need (emphasis mine), so I guess your comment makes sense when you consider that.

1 Like

That depends on memory management, usually swap is used (if available) when you have data in memory that isn't accessed frequently but still needs to be in memory in order to "make room" for data that is. That usually isn't an issue in terms of performance however when you need to access (usually "move") data back and forth because main memory can't store everthing that's when you'll have noticable performance drops.

@eduperez
I think that it also dates back when OSes (which still applies in some cases afaik) do memory dumps to swap in case of crashes.

1 Like

OK thanks for the clarification of eduperez's comment. Given what you said and assuming that it's true for my router, do you think I would notice a lot of performance drops during everyday usage?

The main reason why I need to set all of this up is to facilitate the loading of a large /etc/hosts file (see my other thread, linked in the OP, for details). I can see how the initial loading of that file would be slow due to what you've said, but once it's in memory (RAM, swap or both), it should no longer be an issue, right?

To be more specific to my use case, once dnsmasq has loaded the hosts file on initial boot, how often would the router need to move this data back and forth? On every request?

That's a bit hard to answer, unless you run addtional services I would expect dnsmasq's memory management not to be terrible it should be fine even without swap however if you start seeing out of memory issues you're probably going to see performance issues irregardless of swap usage as running services all more or less use "frequently accessed" memory.

1 Like

Hmmm...so I've basically spent the entire day setting this up, for diddly squat benefit :frowning: I'm afraid you're probably right, but I'll still finish off the process and run some testing just to be sure. Especially since I'm so close now (just created the partitions and formatted them, only thing left to do is re-enable extroot and copy the old data back to the extroot partition, since I had to delete it in order to make space for the swap).

Nope, you will be able to avoid a few situations where the OOM killer might have killed the wrong process or might even have caused a reboot. IMHO swap is for higher "endurance" not for performance, so your wrecent work will have increased the robustness of your router somewhat and hence will be more than "diddly squat" :wink:

LOL cool, thanks for the reassurance. That makes me feel like much less of an idiot LOL.

I know it's unrelated to the topic of this thread, but if someone sees this before my post here, I'd appreciate their insight (if they have any). As always, MTIA! :smiley:

In addition to robustness, the swap space may help by giving the OS somewhere to put pages that really are less used, such as for example the code that runs functions you aren't actually using. wpad has all the code to do wpa_supplicant which is useful if you want to have a wireless client but if you don't run a client it's just fluff... I think it also has a radius server in there somewhere... same kind of ideas with other functions. it might not be much but I could imagine you gain say 10% by swapping out this stuff. on tight devices 10% could mean the difference between not working and working, but also might let you run bigger caches or use larger buffers so performance might improve

1 Like

Thanks mate, I have now successfully setup a 512MB swap partition (I think LOL, at least it shows up in LuCI as enabled after a reboot), so I'll see how things go over the weeks to come.

All I have to do now is either setup my packages again (would really like to avoid this if possible) or copy the old extroot data back on to the new partition from my backup image (much more preferred if possible). Any idea how I would go about the latter?

Why would a [plain] router need swap space?

It's not a [workstation] computer or server...you're not running software and loads that should need balancing beyond the memory, are you???

...on an Archer, I doubt it.

If a router has a need to start using swap, there's a major problem.

1 Like

Read the threads that I linked to in my OP if you want to know the reasons behind my decisions. And plenty of others have supported those decisions (as you can plainly see), so let me answer your question with a counter-question: why would a person feel the need to criticise someone for a perfectly-logical idea???

how did you back it up? if it was a tarball, just untar it. same for other archive formats. if you literally dd dumped it you have to mount it and copy the files. do you have a Linux machine?

Huh?

I've never ran swap on a router...and I don't follow off thread posts.

Sorry you took it as criticism.

And the other thread doesn't explain about an exroot???

Since you didn't answer the question about running programs (which I don't see in either thread), I don't see the sense in thinking i was attacking you...

Bye on dat.

so having swap may not help much in your specific case.
A CUPS spooling server will run just fine when only SWAP is available, whereas some applications may perform very poorly when their data it stored on the SWAP rather then being kept in the β€œreal” RAM.

~ From Wiki

So I guess you're running servers/programs and I misread, apologies.