New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
why is swap used when RAM is avaiable
Hi,
I have a 512MB OpenVZ vps with the following output of "free -m". My question is, why is it using swap when there is plenty of free memory in the "cached" portion of memory, which as I understand it, is free and ready to be utilized when needed?
total used free shared buffers cached
Mem: 512 507 4 0 0 429
-/+ buffers/cache: 78 433
Swap: 512 39 472
Comments
Compare that to dacentec's vm:
That 40MB on swap's nothing to worry about on ovz. It's slowed down ram from what I've been told.
This is actually a good thing, since it puts rarely used pages out of memory so it can use it for more active tasks. If you want to prevent this behaviour, consider adjusting /proc/sys/vm/swappiness
As I know swap is used when the server is out of memory (oversold) but not sure.
run cat /proc/sys/vm/swappiness and set to 30 or something .
You can read (learn) a bit more here :
https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-centos-7
my current swappiness is set to 60, perhaps too high.
From what I know, you can't change swappiness in OpenVZ container (but you can try sysctl -w vm.swappiness=30). You shouldn't worry about it, as others have said, it's used for rarely accessed pages, and it's actually stored in RAM and just artificially slowed a bit. 60 is a typical default swappiness for many distributions; there's no optimal value since it depends on the application.
Its actually normal and good actually, when it comes to the system later needing psychical memory for something else.
When the OS "utilizes" (i.e., reclaims) from the cached memory, the cache gets smaller, and less effective. This may result in more disk I/O to fetch file contents.
When the OS chooses to swap a page out rather reclaiming an additional page from cached memory, it's betting that the page it is swapping out won't be used as often as the least frequently used page in the cached memory.
The people who wrote the OS actually know a lot more about these things than you do, so my suggestion is not to mess with the swappiness settings unless you learn at least as much as they know about it, and can figure out how changing the swappiness value will benefit your use case.
Additionally, don't assume that non-zero values in swap means that your pages aren't in RAM. Swap may appear to be "used" because the OS writes pages that haven't been written in a long time to swap in "readiness" to swap them out if/when necessary. Usage of swap doesn't necessarily mean the pages aren't in RAM.
"cat /proc/meminfo" give a bit more info than free presents, and an important figure to look at is SwapCached. This counts pages that are both in RAM and on disk, because they were swapped out and back in, but since being copied back into RAM they haven't changed (so the copy on disk is still accurate) and the space in swap hasn't been needed for anything else. This means the Kernel can swap therm back out for free later: there is no write cost because the page is already saved to swap.