Summary of methods to clear cache in Linux system

Summary of methods to clear cache in Linux system

1) Introduction to cache mechanism

In the Linux system, in order to improve the performance of the file system, the kernel uses a portion of physical memory to allocate a buffer for caching system operations and data files. When the kernel receives a read or write request, it first goes to the cache to see if there is the requested data. If there is, it returns directly. If not, it directly operates the disk through the driver.
Advantages of the cache mechanism: reduce the number of system calls, reduce CPU context switching and disk access frequency.

CPU context switching: The CPU gives each process a certain service time. When the time slice is used up, the kernel reclaims the processor from the running process, saves the current running state of the process, and then loads the next task. This process is called context switching. In essence, it is the process switching between the terminated process and the process to be run.

2) Check the cache and memory usage

[root@localhost ~]# free -m
       total used free shared buffers cached
Mem: 7866 7725 141 19 74 6897
-/+ buffers/cache: 752 7113
Swap: 16382 32 16350

From the command results above, we can see that the total memory is 8G, 7725M is used, and 141M is remaining. Many people see it this way.
But this cannot be used as the actual utilization rate. Because of the cache mechanism, the specific algorithm is as follows:

Free memory = free (141) + buffers (74) + cached (6897)

Used memory = total (7866) - free memory

From this, we can calculate that the free memory is 7112M and the used memory is 754M. This is the actual usage rate. You can also refer to the -/+ buffers/cache line information, which is also the correct memory usage rate.

3) Cache distinction between buffers and cached

The kernel allocates the buffer size while ensuring that the system can use physical memory and read and write data normally.

Buffers are used to cache metadata and pages, which can be understood as system cache, for example, vi opens a file.

cached is used to cache files, which can be understood as data block cache. For example, when the dd if=/dev/zero of=/tmp/test count=1 bs=1G test writes a file, it will be cached in the buffer. The next time this test command is executed, the writing speed will be significantly faster.

4) Swap usage

Swap means swap partition. Usually, the virtual memory we talk about is a partition divided from the hard disk. When the physical memory is not enough, the kernel will release some programs in the buffers (cache) that have not been used for a long time, and then temporarily put these programs into Swap. That is to say, Swap will only be used when the physical memory and cache memory are not enough.

Swap cleanup:

swapoff -a && swapon -a

Note: There is a prerequisite for this cleanup. The free memory must be larger than the swap space already used.

5) How to release cache memory

a) Clean up pagecache

# echo 1 > /proc/sys/vm/drop_caches or # sysctl -w vm.drop_caches=1

b) Clean up dentries (directory cache) and inodes

# echo 2 > /proc/sys/vm/drop_caches or # sysctl -w vm.drop_caches=2

c) Clean pagecache, dentries and inodes

# echo 3 > /proc/sys/vm/drop_caches or # sysctl -w vm.drop_caches=3

The above three methods are all temporary ways to release the cache. To release the cache permanently, you need to configure in the /etc/sysctl.conf file: vm.drop_caches=1/2/3, and then sysctl -p will take effect!

In addition, you can use the sync command to clean up the file system cache, which will also clean up zombie objects and the memory they occupy.

# sync

The above operations will not harm the system in most cases, but will only help free up unused memory.

But if data is being written while these operations are being performed, then the data is actually being cleared from the file cache before it reaches the disk, which can have adverse effects. So how can we avoid this from happening?

Therefore, we have to mention the file /proc/sys/vm/vfs_cache_pressure here, which tells the kernel what priority should be used when cleaning the inoe/dentry cache.

vfs_cache_pressure=100 This is the default value. The kernel will try to re-declare dentries and inodes and adopt a "reasonable" ratio relative to the page cache and swap cache.
Reducing the value of vfs_cache_pressure will cause the kernel to tend to retain dentry and inode caches.
Increasing the value of vfs_cache_pressure (i.e., exceeding 100) will cause the kernel to tend to re-declare dentries and inodes

In summary, the value of vfs_cache_pressure:
Values ​​less than 100 will not result in a significant reduction in the cache, but values ​​greater than 100 will tell the kernel that you want to clear the cache with high priority.

In fact, no matter what value vfs_cache_pressure is used, the kernel clears the cache at a relatively low speed.
If you set this value to 10000, the system will reduce the cache to a reasonable level.

Before releasing the memory, use the sync command to synchronize to ensure the integrity of the file system and write all unwritten system buffers to disk, including modified i-nodes, delayed block I/O, and read-write mapping files. Otherwise, unsaved files may be lost during the cache release process.

/proc is a virtual file system that can be used as a means of communication with the kernel entity through reading and writing operations. In other words, the behavior of the current kernel can be adjusted by modifying the files in /proc. That is to say, we can free up memory by adjusting /proc/sys/vm/drop_caches.

The value of drop_caches can be a number between 0 and 3, representing different meanings:

0: Do not release (system default value)
1: Release the page cache
2: Release dentries and inodes
3: Release all caches

The above is all the knowledge about clearing cache in Linux system. Thank you for your learning and support for 123WORDPRESS.COM.

You may also be interested in:
  • PHP programmers play Linux series nginx beginner guide
  • Nasm implements the boot code of running a self-made Linux boot disk with vmware
  • Analysis of Linux boot process
  • Linux Administrator's Guide (5) -- Booting and Shutting Down
  • Linux system command notes
  • Summary of Linux system user management commands
  • Detailed explanation of sudo command in Linux system
  • Linux system command to delete folders and files
  • How to use the dd command under Linux system
  • Summary of ten tips for sudo command in Linux system
  • Analysis of Linux boot system methods

<<:  How to set utf-8 encoding in mysql database

>>:  JavaScript css3 to implement simple video barrage function

Recommend

js code that associates the button with the enter key

Copy code The code is as follows: <html> &l...

The implementation process of extracting oracle data to mysql database

In the migration of Oracle database to MySQL data...

Vue batch update dom implementation steps

Table of contents Scene Introduction Deep respons...

Example of how to adapt the Vue project to the large screen

A brief analysis of rem First of all, rem is a CS...

In-depth analysis of the Identifier Case Sensitivity problem in MySQL

In MySQL, you may encounter the problem of case s...

A brief analysis of the count tracking of a request in nginx

First, let me explain the application method. The...

Vue shuttle box realizes up and down movement

This article example shares the specific code for...

Summary of the advantages of Vue3 vs. Vue2

Table of contents 1. Why do we need vue3? 2. Adva...

Do you know how to use Vue to take screenshots of web pages?

Table of contents 1. Install html2Canvas 2. Intro...

How to use CSS attribute value regular matching selector (tips)

There are three types of attribute value regular ...

Docker container monitoring and log management implementation process analysis

When the scale of Docker deployment becomes large...

MySQL 5.5.27 winx64 installation and configuration method graphic tutorial

1. Installation Package MYSQL service download ad...

Pessimistic locking and optimistic locking in MySQL

In relational databases, pessimistic locking and ...

Detailed explanation of scheduled tasks for ordinary users in Linux

Preface Ordinary users define crontab scheduled t...

In-depth explanation of currying of JS functions

Table of contents 1. Supplementary knowledge poin...