How to empty the Swap memory of a Linux server
Occasionally we may receive resource usage alerts indicating that the server's Swap or Exchange Memory is at 100% usage.
SERVICE ALERT: ce00000000000.dnssw.net
Unix Memory Nou
WARNING
Usage: real 95% (11404/11995 MB), buffer: 3 MB, cache: 302 MB, swap: 100% (1907/1907 MB)
Commonly this can happen in two scenarios:
- The server has run out of RAM and is or has been writing to Swap.
- The software installed on the server, by its own configuration, is or has been writing to Swap.
danger ⚠️ The following actions can be dangerous if you do not have the necessary knowledge ⚠️
How to detect the alert
If you have Advanced Monitoring you will have received an email notifying you of the Unix Memory Nou alert.
SERVICE ALERT: ce00000000000.dnssw.net
Unix Memory Nou
WARNING
Usage: real 95% (11404/11995 MB), buffer: 3 MB, cache: 302 MB, swap: 100% (1907/1907 MB)
How to activate Advanced Monitoring in my Cloud
How to handle the alert
To handle the alert you will have to connect via console to your server. You can connect through SWPanel Console or via SSH.
To empty the Swap memory, we must dump the data stored in the RAM memory.
First of all, we must check the memory status of the server, using the following command:
free -m
With this one, we will be able to see the memory bytes you have allocated, available and used.
In this case, the server has 453B occupied and 1453B free:
We must make sure that the amount of Swap used is less than the amount of RAM available.
In this case, this rule is fulfilled, we have 453B of Swap in use and 3379B of RAM available:
danger ⚠️ Do not perform the next step if the previous rule is not met ⚠️
If the above rule is satisfied, we will proceed to dump the Swap into RAM and thus empty it. We will use the following command:
swapoff -a && swapon -a
When the command is executed, the console will be 'idle' until the process is finished:
The Swap flushing process may take several minutes depending on the state of the server and its resources.
You can watch the flushing process in real time by opening another console and using the command:
watch -n0 free -m