Howto: Clear Swap Cache

Recently I’ve wrote a script to calculate the swap usage and triggered the alert when Swap usage hit the certain level.

And I found out the actual memory usage is not using as it is where swap still caching the usage. To clear the swap cache and make the memory usage only, you may turn of swap by selecting the swap drive.

First of all, you need to identify the swap disk. Check the blkid for more information.


Look at the entry of swap. Assume swap is in sda5.

Turn the swap cache off,

swapoff /dev/sda5

Once it is done, turn on again,

swapon /dev/sda5

Understand Linux-based RAM and it’s cache

Linux Operating System that powered by Linux kernel have an advanced disk cache algorithm that used to store frequently access data from Hard disk.

This is to explain why some Linux server’s RAM still getting high memory usage even you upgrade your RAM.

To find out how much memory using by the Linux-based server, you may use the linux command “free” to track it. To get the detailed/actual memory usage, you may run the following command from your Linux box.

free -m

You should get some result as below,

             total       used       free     shared    buffers     cached
Mem:         15905       2879      13026          0        384        900
-/+ buffers/cache:       1593      14311
Swap:         3999          0       3999

With the result above, you may see that the used memory is 2879MB and free memory is 13026MB. However, the cached memory is 1593MB while the actual free memory will be 14311MB.

Jailed Shell memory issue

If you are using Secure Shell (SSH), you might get these error when you running your php script.

PHP Fatal error:  Out of memory (allocated 20447232) (tried to allocate 4096 bytes) in email.php

If you are the programmer or system administrator, you might tried to increase the memory_limit from the php.ini or using ini_set to increase the memory_limit but you will find out no matter how much memory_limit you set will not solve the problem.

The reason of the error occurs is not because of the PHP memory_limit usage of your script, but it is because the jailed shell limitation.

With cPanel jailed shell, the problem could caused by the forkbomb setting of your cPanel limited the PHP memory.

You may easily get this fix by modify the file as detail below where you should change the user to the cPanel user of your account.

Search for,
if [ “$LIMITUSER” != “root” ]; then

Then modify the ulimit “-m 200000” to “-m unlimited” and save it.