Available for
Memory=free memory+buffers+cached
Clear page Cache only (Pagecache)
# sync; echo 1 > /proc/sys/vm/drop_caches
Clear Catalog Items and Inode
# sync; echo 2 > /proc/sys/vm/drop_caches
Clear page cache, directory entries, and inode
# sync; echo 3 > /proc/sys/vm/drop_caches
You must clear the disk cache, the first command is the safest in the enterprise and production environment, and "...echo 1> ..."
only clears the page cache. The third option above is not recommended in a production environment "...echo 3 > ..."
unless you are clear about what you are doing because it clears cached pages, directory entries, and inodes.
Build a shell script clearcache.sh and add the following lines to it:
#!/bin/bash
# 注意,我们这里使用了 "echo 3",但是不推荐使用在产品环境中,应该使用 "echo 1"
echo "echo 3 > /proc/sys/vm/drop_caches"
To set execute permissions for a clearcache.sh file
# chmod 755 clearcache.sh
Now, when you need to clear the memory cache, you only need to invoke the script.
Now set a daily 2 o'clock in the afternoon scheduled task to clear the RAM cache and open crontab for editing.
# crontab -e
Add the following line, save and exit.
03***/path/to/clearcache.sh
How do I clear the swap space for Linux?
If you want to clear out the space, you can run the following command:
# swapoff -a && swapon -a
Also, once you know about the risks, you can add the above commands to cron.
Now, we combine the above two commands into one command and write the correct script to erase both the RAM cache and the swap space.
# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf ‘\n%s\n‘ ‘Ram-cache and Swap Cleared‘
Or
su -c ‘echo 3 > /proc/sys/vm/drop_caches‘&& swapoff -a && swapon -a && printf‘\n%s\n‘‘Ram-cache and Swap Cleared‘
Before testing the above command, we ran "free-m" before and after executing the script to check the cache.
Memory=free memory+buffers+cached