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 the RAM memory cache on Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "how to clear the RAM memory cache on Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Like any other operating system, GNU / Linux has effectively implemented memory management and more. However, if any process is eating away at your memory and you need to clear it, Linux provides a way to flush or clear the RAM cache.

How do I clear the cache in Linux?

Each Linux system has three options to clear the cache without interrupting any processes or services. 1, clear only cache pages

Sync; echo 1 > / proc/sys/vm/drop_cachessync; echo 1 > / proc/sys/vm/drop_caches

2. Clear directory entries and inodes

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

3, clear, cache pages, directory entries and inodes

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

Description of the above command: sync flushes the file system cache, commands are separated by ";", executed sequentially, and shell waits to be terminated before each command in the sequence is executed. As mentioned in the kernel documentation, writing to drop_cache clears the cache without killing any application / service, and the echo command does the job of writing to the file. 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 is cleared. The third option above is not recommended in production environments such as "… echo 3 >" unless you know exactly what you are doing, as it clears cache pages, directory entries, and inodes. Is it a good idea to use the kernel to release Buffer and Cache on Linux? When you ask for many settings to check, you may need to clear the cache if it is actually dedicated to implementing an extensive benchmark for Imax O. You can delete the cache as shown above without restarting the system or downtime. Linux is designed to look in the disk cache before looking for the disk. If it finds that the resource is in the cache, the request does not reach the disk. If we clean up the cache, the disk cache will be useless and the system will look for resources on the disk. In addition, when the cache is cleared, it will also slow down the system, and the system will reload each requested resource into the disk cache again. Now, through a cron task scheduler, we will create a shell script that automatically clears the RAM cache at 2 p.m. Every day. Create a shell script, clearcache.sh, and add the following line to it:

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

Set execution permissions for clearcache.sh files

# chmod 755 clearcache.sh

Now, you just need to call the script when you need to clear the RAM cache. Now set up a scheduled task to clear the RAM cache every day at 2 p.m., 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 see our article 11 Cron Scheduling Jobs. Is it a good idea to automatically clear RAM on a server in a production environment? No, no! It's not. Think of a situation when you have booked a script to clear the RAM cache every day at 2pm. The script executes and flushes your RAM cache at 2pm every day. At any time of the day, your site may have more users online than expected and request resources from your server. At the same time, the scheduler runs the script and clears everything in the cache. When all users read data from disk, this will cause the server to crash and damage the database. Therefore, clear the cache only when necessary and as you expect, otherwise you are a Cargo Cult System Administrator.

How do I clear swap space for Linux?

If you want to clear swap space, you can run the following command:

# swapoff-a & & swapon-a

In addition, once you know about the risks, you may add the above command to cron. Now, let's combine the above two commands into a single command to write the correct script while clearing the RAM cache and 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, let's run "free-m" and then execute a script to check the cache.

That's all for "how to clear the RAM memory cache on Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report