When using the free -h command, you notice that your server is always using a large amount of swap space:
# free -h
total used free shared buff/cache available
Mem: 7.6G 894M 4.2G 361M 2.6G 6.1G
Swap: 2G 1.9G 0.1G
This swap usage is always high and almost never decreases.
The key thing to keep in mind in this situation is that swap usage does not indicate a problem.
Swap usage is often associated with system performance problems because the majority of the time high swap usage occurs during and after the server has run low on memory.
It's important to remember that it is not the usage of swap its self that is causing a problem. Quite to the contrary, the usage of swap is actually a good thing and can help to speed your server up.
Swap acts as a place to cache less frequently used data that would otherwise take up RAM. This makes it so that your server can keep the most highly trafficked data in RAM, but still have less frequently used data cached and ready to use from the swap space.
Why doesn't the amount of swap space ever go down?
Moving data to swap is a relatively slow and intensive task. The kernel will only move data to swap if it determines that the benefit outweighs the slow operation.
Moving data back out of swap (reducing swap usage) is similarly slow. For that reason, the kernel is hesitant to move data out of swap. Taking time and resources to move data out of swap if there is no competition for the swap space is not in the best interest of the kernel which is why you rarely if ever see swap usage reduced. If the data is no longer used, that is not a good enough reason to spend resources to remove it from swap.
How did my server end up with so much swap being used?
This is highly dependant on the types of applications your server is running, the usage patterns, and the configuration of your specific server. Ultimately, you'd need to work with a systems administrator that has the skills, training, and expertise required to track that down for you. Although, this should be considered a very low priority unless you are experiencing a problem with memory usage on the server.
One typical reason that your server's swap usage got to be large is that at one point in time, the server was running low on memory. At that point, the server would have swapped out the less important data to the swap space to allow the more important data to be accessible via RAM.
Although keep in mind that just because swap usage is high right now, it does not mean that your server is running low on memory.
When to be concerned about swap usage
The amount of swap used should not be something to be concerned about.
Instead, you should be monitoring the rate at which the server is moving pages in and out of swap space.
There are many tools for monitoring this, although one common tool is vmstat.
To monitor the swap rate, look at the si and so columns which represent the Swap In and Swap Out rates.
You should regularly monitor these rates so that you know the normal patterns for your specific setup.
You should note what the rate normally is while your server is behaving properly without any problems, and then if your server starts having issues, you'll have something to compare the swap rates to.
How can I make it so that my server goes back to 0 swap usage?
You can reset your swap usage by turning swap off and then turning it back on.
This process will flush all of the used swap pages back into RAM, and set your swap space back to 0.
To do this issue the following command:
Then turn swap back on with the following command:
It is perfectly safe to run these commands as long as your server is not currently experiencing a low memory condition.