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

Example Analysis of caching Mechanism free and cleaning buffer/cache/swap under Linux

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

Share

Shulou(Shulou.com)05/31 Report--

The purpose of this article is to share with you the contents of the sample analysis of the caching mechanism free and cleaning up buffer/cache/swap under Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

(1) caching mechanism

In order to improve the performance of the file system, the kernel uses part of the physical memory to allocate buffers for caching system operations and data files. When the kernel receives a request for read and write, the kernel first goes to the cache to see if there is any requested data, and returns it directly. If not, directly manipulate the disk through the driver.

Advantages of caching mechanism: reduce the number of system calls, reduce the frequency of CPU context switching and disk access.

CPU context switching: CPU gives each process a certain service time. When the time slice runs out, the kernel takes back 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 running process and the running process.

(2) check the cache area 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

You can see that a total of 8 gigabytes of memory, 7725m has been used, the remaining 141m, many people see it this way, this can not be used as the actual utilization rate. Because of the caching mechanism, how should it be calculated?

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

Used memory = total (7866)-Free memory

As a result, it is calculated that the free memory is 7112m and the used memory is 754m, which is the real utilization. You can also refer to-/ + buffers/cache this line of information is also the correct memory utilization.

(3) the visible cache area is divided into buffers and cached, what is the difference between them?

The kernel allocates buffer size while ensuring that the system can normally use physical memory and the amount of data to read and write. Buffers is 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 block caching. For example, if a dd if=/dev/zero of=/tmp/test count=1 bs=1G test writes a file, it will be cached in a buffer, and the next time the test command is executed, the write speed will be significantly faster.

(4) by the way, what does Swap do?

Swap means swapping partitions, and what we usually call virtual memory is a partition that is partitioned from a hard disk. When there is not enough physical memory, the kernel releases programs that have not been used for a long time in the cache (buffers/cache), and then temporarily places these programs in Swap, that is, Swap is used only if there is insufficient physical memory and cache memory.

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 free the cache memory?

A) directly change the kernel running parameters

Linux command to free memory:

Sync

Echo 1 > / proc/sys/vm/drop_caches

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

0: no release (system default)

1: release the page cache

2: release dentries and inodes

3: release all caches

After releasing the memory, change it back and let the system reallocate the memory automatically.

Echo 0 > / proc/sys/vm/drop_caches

Free-m # to see if memory has been freed.

If we need to release all caches, enter the following command:

Echo 3 > / proc/sys/vm/drop_caches

B) you can also use sysctl to reset kernel running parameters

Sysctl-w vm.drop_caches=3

Note: both of these methods are temporary. Permanent validity needs to be added to the sysctl.conf file, which is generally written as a script to clean up manually. It is recommended not to clean up.

Modify / etc/sysctl.conf to add the following options, memory will not continue to increase

Vm.dirty_ratio = 1

Vm.dirty_background_ratio=1

Vm.dirty_writeback_centisecs=2

Vm.dirty_expire_centisecs=3

Vm.drop_caches=3

Vm.swappiness = 100

Vm.vfs_cache_pressure=163

Vm.overcommit_memory=2

Vm.lowmem_reserve_ratio=32 32 8

Kern.maxvnodes=3

The above settings are rough, making it almost impossible for cache to play its role. It is necessary to make appropriate adjustments according to the condition of the machine to find the best compromise.

Thank you for reading! This is the end of this article on "example analysis of caching mechanism free and cleaning buffer/cache/swap under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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

Database

Wechat

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

12
Report