Clear RAM cache and swap space on Linux


linux-logo

Clear RAM Cache on Linux

1. Clear PageCache only.

sync; echo 1 > /proc/sys/vm/drop_caches

2. Clear dentries and inodes.

sync; echo 2 > /proc/sys/vm/drop_caches

3. Clear PageCache, dentries and inodes.

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

Clear swap space on Linux

swapoff -a && swapon -a

Clear RAM cache and swap space via cronjob

Step 1: Create file cronclear.sh:
Clear RAM cache only

#!/bin/bash
# Note, we are using "echo 3", but it is not recommended in production instead use "echo 1"
echo 3 > /proc/sys/vm/drop_caches

Clear RAM cache and swap space

#!/bin/bash
# Note, we are using "echo 3", but it is not recommended in production instead use "echo 1"
echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a

Step 2: Then chmod this file

chmod 755 cronclear.sh

Step 3: crontab -e. Example, you run cronjob daily at 10am

0  10  *  *  *  /path/to/cronclear.sh

Leave a Reply