Linux clean up Cache, Buffer and Swap that occupy RAM

(Light bulb is on)Due to the high price of memory, various operating systems need to efficiently manage the use of memory. GUN/Linux memory management also has a set of memory management allocation strategies.

If a process occupies too much memory and you want to clean up this part of the memory, Linux provides corresponding commands to clean up the use of ram cache

How to clear the cache in Linux?

  • Only clean PageCache (page cache)
# sync; echo 1 > /proc/sys/vm/drop_caches 
  • Only clean dentries and inodes
sync; echo 2 > /proc/sys/vm/drop_caches
  • Clean up all PageCache, dentries and inodes
sync; echo 3 > /proc/sys/vm/drop_caches

Explain the command run above

(Light bulb is on)Sync will refresh the buffer of the system. The command separator ";" separates the commands. The shell will wait for the completion of the previous command before executing the next command.

(Light bulb is on)Editing of the drop_caches file will clean up the cache without killing the process. As mentioned in the kernel documentation.

(Light bulb is on)The echo command writes the drop_cache file

 

If you just clean the disk cache, echo 1 is the safest, and this command will only clean up the page cache

Echo 3 is not recommended for production, unless you clearly understand what you are doing, echo 3 will clean up PageCache, dentries and inodes

 

The design philosophy of Linux is to access the disk cache before a read request to access the disk. If the content to be read is found in the disk cache,

This read request will no longer request to read the disk. If we clear the disk cache, all read requests will be read from the disk.

The data is then cached to the disk cache. Because the first request after cleaning is not found in the disk cache. In addition disk cache

The cleanup will cause the system to slow down for a few seconds, because all requests will request disk for the first time(caveat)

 

Regular cleaning

We can write a shell script to clean up the cache at two o'clock in the morning through crontab.

script

Create the file clearcache.sh, the script content is as follows

#!/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"

Authorization

# chmod 755 clearcache.sh

Timed execution

Set crontab to execute at 2 o'clock

# crontab -e

Add content to the text that appears in crontab -e

0  2  *  *  *  /path/to/clearcache.sh

For detailed use of crontab, please refer to 11 Cron Scheduling Jobs .

 

Should I clean up the cache regularly?(caveat)

It is not a good idea to clean the cache regularly. It may be due to unknown reasons one day, after the cache is cleaned at two in the morning,

The user requests your webpage static resources to be loaded from the disk, resulting in a poor user experience

 

If after clearing the cache, a large number of users read data from the disk, it will also cause excessive server pressure and cause software crash

Do not clean up cache servers such as redis and mongodb memcache

 

How to clean up Swap in Linux?

If you want to clean up the swap space, you can run the following command

# swapoff -a && swapon -a

After you fully understand the meaning of the above command, you can add the above script to your crontab task for regular cleaning

 

We can combine the scripts for cleaning swap and buffer/cache and write them into scripts for regular cleaning or convenient cleaning operations.

Guess you like

Origin blog.csdn.net/VoiceRoom/article/details/108869887