Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to clear Cache and Buffer under Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to clear Cache and Buffer under Linux. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Buffer and cache are two terms that are used indiscriminately in computer technology. In Linux memory management, buffer refers to Linux memory: Buffer cache. Here cache refers to Linux memory: Page cache, translated into Chinese can be called buffer cache and page cache.

How to clear cache in Linux? Each Linux system has three options to clear the cache without interrupting any processes or services.

(LCTT: Cache, translated as "cache", refers to the cache between CPU and memory. Buffer, translated as "buffer," refers to the contents of storage and memory before writing to disk. Buffer and Cache are sometimes referred to together in this article.)

Clear page cache only (PageCache)

# sync; echo 1 > /proc/sys/vm/drop_caches Clear directory entries and inodes

# sync; echo 2 > /proc/sys/vm/drop_caches Clear page cache, directory entries, and inodes

# sync; echo 3 > /proc/sys/vm/drop_caches Description of the above command:

sync flushes the file system buffer, commands are separated by ";" and executed sequentially, and the shell waits for the command to terminate before executing the next command in the sequence. As mentioned in the kernel documentation, writing to drop_cache empties the cache without killing any applications/services, and the echo command does the work of writing to files.

If you have to clear the disk cache, the first command is the safest in enterprise and production environments,"... echo 1> ... "Only the page cache will be cleared. The third option above is not recommended in production environments "... echo 3 > ... Unless you know exactly what you're doing, because it clears cache pages, directory entries, and inodes.

Is it a good idea to free buffers and caches on Linux that might be used by the kernel?

When you set up many settings to check the effect, if it is actually a benchmark specifically for I/O ranges, then you may need to clear buffers and caches. You can delete the cache as shown above without restarting the system (i.e. without downtime).

Linux is designed in such a way that it looks to disk cache before looking to disk. If it finds the resource in cache, the request is not sent to disk. If we clean the cache, the disk cache will not work and the system will look for resources on disk.

In addition, it slows down the system when the cache is cleared, and the system loads each requested resource back into the disk cache.

Now, we'll create a shell script that automatically clears the RAM cache every day at 2 p.m. via a cron scheduling task. Create a shell script clearcache.sh as follows and add the following lines to it:

#!/ bin/bash#Note that we used "echo 3" here, but it is not recommended. In a production environment, you should use "echo 1"echo "echo 3 > /proc/sys/vm/drop_caches" to set execution permissions for the clearcache.sh file.

# chmod 755 clearcache.sh Now you only need to call scripts when you need to clear the memory cache.

Now set up a timed task at 2 p.m. each day to clear the RAM cache and open crontab for editing.

# crontab -e Add the following line, save and exit.

0 3 * * * /path/to/clearcache.sh For more details on how to create a scheduled task, you can check out our article 11 examples of scheduled tasks.

Is it a good idea to automatically clear RAM on servers in a production environment?

No! It's not. Think of a situation where you have scheduled scripts to clear the memory cache at 2 p.m. every day. Then the script will execute and flush your memory cache. For some reason, online users of your website may one day request resources from your server more than expected.

At this point, the scheduled script runs and clears the cache. When all users read data from disk, this will cause the server to crash and corrupt the database. Therefore, clear the cache only when necessary and as you expect, otherwise you are a nerd system administrator.

How to clear swap space in Linux? If you want to clear the space, you can run the following command:

# swapoff -a && swapon -a Also, once you know about risk, you can add the above command to cron.

Now, let's combine the two commands above into one command and write the correct script to clear both RAM cache and swap space.

# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swap-a && printf '\n%s\n' 'Ram-cache and Swap Cleared' or

su -c 'echo 3 > /proc/sys/vm/drop_caches' && swapoff -a && swap-a && printf '\n%s\n'Ram-cache and Swap Cleared'Before testing the above command, we run' free -m 'before and after executing the script to check the cache.

Clear RAM Cache and Swap Space

Thank you for reading! About "how to clear Cache and Buffer under Linux" This article is shared here, I hope the above content can have some help for everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report