How to clear swap usage on Linux

How to force clear Swap usage on Linux

Swap is disk memory when physical Random Access Memory (RAM) is full.

We know that disk write and read speed is very slow compared to RAM.

But it not safe and not recommended to disable swap on Linux.

When your Linux does not have enough phisical RAM used by your applications, your Linux may use Swap space.

But the problem is Swap usage usually not released, and it’s problem for your server and performance.

If you want to force clear Swap usage, please check that your physical RAM is bigger than Swap used.

Use this command to check your RAM and Swap usage

# free -m

If available RAM smaller than Swap usage, it’s not safe to force clear Swap, and the safest way is reboot your server.

How to force clear Swap on Linux

If you sure that your RAM size is bigger than Swap usage, first clear PageCache, dentries and inodes.

Do the following command:

# sync; echo 3 > /proc/sys/vm/drop_caches

Then force turn off Swap and turn on again with the following command.

# swapoff -a && swapon -a

Please note that above command need several time, usually 1-3 minutes.

My suggestion is open new terminal and run Htop command or free -m to monitor the process.

When you run swap off command, swap usage will decrease, please wait the process.

Swap usage decrease when we run command swap off
Swap usage decrease when we run command swap off
Swap usage successfully cleared

ServerDiary

ServerDiary

Leave a Reply

Your email address will not be published. Required fields are marked *