RHEL 5 Not releasing swap memory
The best thing we have done to release the RAM is by rebooting the server. Anyone know why RHEL 5 tends to use swap and not release it?
Responses
The kernel chooses to use swap because it's faster than reading that page from its original disk storage again.
There's nothing inherently bad about the system using swap, this is the Linux kernel making your system faster than it otherwise would be if it completely dropped that memory page.
If the system is constantly thrashing in and out of swap, that's a different thing, and is bad.
It's common for a page not to be freed from swap once it's been re-read, so a page can appear in both memory and swap, this way there is 0 penalty for freeing that page from memory because it doesn't have to be written to swap again. This is the SwapCached value in /proc/meminfo.
You can control the tendency of the kernel to use swap with vm.swappiness in /etc/sysctl.conf, where 100 is "very likely" and 0 is "not likely". On recent RHEL 6, 0 actually means "don't use swap at all".
If you want to force all swap to either be dropped or paged back in, you can run swapoff -a; swapon -a to disable and enable swap.
Some cloud provider actually shipped images without SWAP configured, which seemed absolutely crazy... at first. Like Jaime mentioned, SWAP is not inherently bad.
However, you need to be concerned with the swapping rates - that is: it is OK to have a significant amount of SWAP consumed, but you DON'T want to be moving a lot of data in-and-out of SWAP.
You can check the SWAP rates using
sar -B
07:40:01 AM pgpgin/s pgpgout/s fault/s majflt/s pgfree/s pgscank/s pgscand/s pgsteal/s %vmeff
07:50:01 AM 0.00 9.61 169.61 0.00 211.64 0.00 0.00 0.00 0.00
08:00:02 AM 0.60 15.38 358.99 0.00 595.59 0.00 0.00 0.00 0.00
08:10:01 AM 0.53 59.85 712.90 0.00 1297.74 0.00 0.00 0.00 0.00
08:20:01 AM 2.51 24.22 320.85 0.02 537.04 0.00 0.00 0.00 0.00
Average: 0.09 13.18 198.40 0.00 262.66 0.00 0.00 0.00 0.00
Also - you can watch the I/O of the system using iotop. I don't currently have a box swapping so my example is not exact...
Total DISK READ : 0.00 B/s | Total DISK WRITE : 0.00 B/s
Actual DISK READ: 0.00 B/s | Actual DISK WRITE: 0.00 B/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
1 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % systemd --switched-root --system --deserialize 23
There is no single-rule to setting the swappiness value. It is very dependent on the environment/application/etc...
For example: I see many folks recommending setting it 0 for msyql.
Swapping can be an expensive task - so to force it to swap in/out arbitrarily might not provide the results you are looking for ;-)
What stack are you running? Perhaps we can help find some recommendations or best-practices.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
