SOLVED Should my swap equal my RAM?
I resized my VPS up and now I have 1GB of swap and 4GB of RAM. As apps apply half memory to swap should I increase my swap to 4GB?
@masonbee Yes, having a bit more swap won't hurt. Please see https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-swapspace for some recommendations.
robi last edited by
@girish this guidance doesn't apply well to containerized environments where the system is the container.
Having 25-50% swap space for overloaded memory conditions is ok, but in practice a large waste of space, especially in server type environments in VPS systems where they're not generous with storage or RAM.
If memory is overcommited to a point of swap space use, in a container that means slow performance and likely a memory sizing issue, which the OOM killer will shortly remedy.
Hence why bother with so much swap spread across so many containers halving it's limits when RAM is the actual target.
IMO if any at all, 10%-20% would be plenty as a setting for containers. My preference is 0%.
@girish Thank you. The Red Hat documentation was roughly how I was thinking of it and in that case I should be fine with 1GB of swap (as I am not suspending the server, etc...) although 2GB might be better.
However, if i look at my apps they say, "Cloudron allocates 50% of this value as RAM and 50% as swap.". If that is a hard rule then that might explain why my apps seem to be restarting, even ones that don't get a huge amount of load (eg; low visit website), as after 8 x 256mb apps my swap space is fully allocated.
Now, I am running 17 apps at the moment and according to System Info/System Memory this is using 3GB of memory out of "RAM (3.85 GB) + Swap (984.99 MB)" which would seem to me to be a reasonable headroom unless the swap rule is hard at which point I have (assuming 256mb apps) over 2GB allocated to swap and under 1GB available.
So, is the swap rule hard? Because then it would seem to me to mean I need to have the same amount of swap as RAM. In my case 4GB.
Hopefully that is clear and thank you for your former reply.
So, is the swap rule hard?
It's not. To better understand this: The memory allocation in the app UI is the maximum memory limit. It is not hard allocated to the app. If the app only uses 20MB of it's allocated 256MB (say), then the rest is available for other apps.
About the swap: Note that swap is not really "used" by a process because that's not possible. An app can only use RAM and the swap is just a place in the disk to dump existing RAM to free up RAM (sorry, for my poor explanation ). So, when we say "50% is allocated for swap", it only means that the kernel is told that the app can use that much space on disk for it's own RAM.
Practically speaking, in our case, it seems that out of 4GB, almost 3.85 GB is already used. In this case, I would give more swap. Ideally, you should give it a little bit more RAM (say 2GB) but swap is the cheaper option. The kernel will keep swapping things out as required.
@girish Increasing the swap seems to have cured the problem. Additionally, there may have been a problem caused by a misreading of the amount of swap as originally on the Linode it said there was only 512mb but in Cloudron it said there was almost 1GB.
I have increased the swap from 512mb to 2048mb and I haven't received any app out of memory notifications since then so that seems to have cured it.
Thank you for your help.