SWAP
====================================================================================
SWAP
What is SWAP?
Swap memory is space that is reserved via the hard disk and should only be utilized when necessary and in cases where the servers dedicated memory has been mostly used upup.
The inswappiness yourvalue case,is becauseactually a kernel tunable - see here for more information
How does a system decide how often to use SWAP over physical memory?
In Linux, the /proc/sys/vm/swappiness file is a kernel parameter that controls the swappiness levelbehavior of yourthe servervirtual wasmemory setsystem. soSwappiness is a setting that determines how aggressively the kernel will use swap space.
Understanding Swappiness
Swappiness Value: The value of swappiness can range from 0 to 100.
0: The kernel will avoid swapping out processes as much as possible, preferring to keep data in RAM.
100: The kernel will aggressively swap processes out of physical memory and move them to swap space, keeping more RAM free.
Swappiness at 0:
The kernel will prioritize keeping processes in RAM, only swapping out data when absolutely necessary.
This setting is useful for systems where you want to minimize latency and keep performance high (60)for itactive meant that the running processes on your serverapplications, such as thoseon froma Nginxdesktop or server where response time is critical.
Swappiness at 100:
The kernel will swap out data more readily, even if there is still available RAM.
This setting can be useful in certain scenarios where you want to maximize the use of RAM for disk caches and Apachereduce werethe runningamount onof swapcached memorydata ratherbeing thancleared dedicatedto memorymake whichroom for process memory.
------------------------------------------------------------------------------------------------------------------------------------------------
Managing SWAP configuration
Check swappiness value
cat /proc/sys/vm/swappiness
Changing swappiness
Since swappiness is a lotkernel slower.tunable, the only method to make a swappiness value change persist a reboot is to create a custom configuration in /etc/sysctl.d. I'll also detail the steps below:
Change swappiness value (persistent)
root@serverbds:Create a new custom configuration file in /var/log/atop#etc/sysctl.d
vim 00-custom-swap.conf
Within this file, place the following string (with your desired value added)
vm.swappiness=10
Once created, apply the settings using the below command:
sysctl -p
Change swappiness value (not persistent)
To change the swappiness value on a machine for the current session (doesn't persist reboot), we need to manually update the swappiness file:
vim /proc/sys/vm/swappiness
The file should contain only a number - this being the swappiness value. Change this number to your desired value. For example, to set a swap value of 60:
root@test:~# cat /proc/sys/vm/swappiness
60
As you can see fromOnce the above,swappiness priorvalue has been changed, we then need to theswitch changeoff/switch thaton I made, your server's swappiness level was setswap to 60pull which meant that once 40% ofin the server'snew dedicated memory would be in use, the server would then start moving processes on to swap memory causing performance issues on the server.value:
I have since changed this value to 5 so that the server will not resort to swap memory as often and will utilize more of the dedicated memory when available which is crucial in impacting site performance which is what I suspect is what was causing the timeout issues that Nginx and Apache were logging.
root@serverbds:/var/log/atop# echo 5 > /proc/sys/vm/swappinessroot@serverbds:/var/log/atop# cat /proc/sys/vm/swappiness5
root@serverbds:/var/log/atop#
swapoff -a
root@serverbds:/var/log/atop#
swapon -a
------------------------------------------------------------------------------------------------------------------------------------------------
SWAP partitions
For SWAP to work in Linux, there has to be sections of a disk partitioned specifically for this purpose. By default, most Linux distros will come with a swap file/partition pre-created.
Check the current swap partition configuration
swapon --show
Create and enable a swap partition:
mkswap /dev/devicename
swapon /dev/devicename
====================================================================================