Memory=free memory+buffers+cached

Source: Internet
Author: User

Available for

Memory=free memory+buffers+cached

    1. Clear page Cache only (Pagecache)

      1. # sync; echo 1 > /proc/sys/vm/drop_caches
    2. Clear Catalog Items and Inode

      1. # sync; echo 2 > /proc/sys/vm/drop_caches
    3. Clear page cache, directory entries, and inode

      1. # 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:

    1. #!/bin/bash
    2. # 注意,我们这里使用了 "echo 3",但是不推荐使用在产品环境中,应该使用 "echo 1"
    3. echo "echo 3 > /proc/sys/vm/drop_caches"

To set execute permissions for a clearcache.sh file

    1. # 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.

    1. # crontab -e

Add the following line, save and exit.

    1. 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:

    1. # 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.

    1. # echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf ‘\n%s\n‘ ‘Ram-cache and Swap Cleared‘

Or

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.