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 the Linux system clears the cache

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

Share

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

This article mainly introduces how to clear the cache of Linux system, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

1) Introduction to cache mechanism

In Linux system, in order to improve the file system performance, the kernel uses a part of physical memory to allocate a buffer area for caching system operations and data files. When the kernel receives a read/write request, the kernel first goes to the buffer area to find whether there is the requested data. If there is, it returns directly. If not, it directly operates the disk through the driver program.

Cache mechanism advantages: reduce the number of system calls, reduce CPU context switches and disk access frequency.

CPU context switch: CPU gives each process a certain service time, when the time slice runs out, the kernel withdraws 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 switch. In essence, it is a process switch between the terminated running process and the process to be run.

2) View cache and memory usage

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

From the above command results display can be seen: a total of 8G memory, 7725M has been used, the remaining 141M, many people are so.

However, this is not the actual usage 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 calculation, the free memory is 7112M, and the used memory is 754M. This is the real utilization rate. You can also refer to the-/+ buffers/cache line information, which is also the correct memory utilization rate.

3) cache distinguish between buffers and cached

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

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 caching. For example, dd if=/dev/zero of=/tmp/test count=1 bs=1G Test writes a file, which will be cached in the buffer. The next time you execute this test command, the write speed will be significantly faster.

4) Swap use

Swap means swap partition, usually called virtual memory, is a partition divided from the hard disk. When physical memory is insufficient, the kernel releases programs that have not been used for a long time in buffers/cache, and then temporarily places these programs in Swap, that is, Swap is only used when physical memory and buffer memory are insufficient.

Swap cleaning:

swapoff -a && swapon -a

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

5) Method of releasing buffer memory

a) Clean up the page cache

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

b) Clean up entries and inodes

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

c) Clean up pagecache, entries and inodes

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

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

Additionally, you can use the sync command to clean up the file system cache, as well as zombie objects and the memory they consume

# sync

In most cases, the above will not harm the system and will only help free up unused memory.

But if data is being written while these operations are being performed, then it is effectively purged from the file cache before it reaches disk, which can have a very bad effect. So what if we avoid that?

Therefore, I have to mention the file/proc/sys/vm/vfs_cache_pressure, which tells the kernel what priority to use when cleaning inoe/dentry cache.

vfs_cache_pressure=100 This is the default value, and the kernel tries to re-declare entries and inodes and adopt a "reasonable" ratio relative to page cache and swap cache.

Decreasing the value of vfs_cache_pressure causes the kernel to prefer to keep dentry and inode caches.

Increasing the value of vfs_cache_pressure (i.e., above 100) causes the kernel to tend to redeclare entries and inodes

In summary, the value of vfs_cache_pressure:

Values less than 100 do not result in a significant reduction in cache

A value above 100 tells the kernel that you want cache cleanup to be a high priority.

Regardless of the value of vfs_cache_pressure, the kernel cleans the cache slower.

If you set this value to 10000, the cache will be reduced to a reasonable level.

Sync before freeing memory to ensure file system integrity, writing all unwritten system buffers to disk, including modified i-nodes, deferred 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 read and written to as a means of communicating with kernel entities. This means that the behavior of the current kernel can be adjusted by modifying the files in/proc. This means that we can free 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)

1: Free page cache

2: Release the entries and inodes

3: Release all caches

Linux is a free-to-use and freely distributed UNIX-like operating system, is a POSIX-based multi-user, multitasking, multi-threaded and multi-CPU operating system, using Linux to run major Unix tools, applications and network protocols.

Thank you for reading this article carefully. I hope that the article "How to Clear Cache in Linux System" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support it a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

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