First, case introduction

For example, let’s look at the free output. The source of the free data collection is the /proc/meminfo file, which will be used throughout this series.

root@szdc-calic-2-6:~# free
              total        used        free      shared  buff/cache   available
Mem:       32895096     1698396     8197904      307688    22998796    30343448
Swap:      31250428      114992    31135436
Copy the code

What do we get from the output above? Is the system memory sufficient? What is a buffer, what is a cache, and how much memory is currently available for allocation? With these questions in mind, let’s move on.

Cache and buffer

2.1 the cache

2.1.1 definition

To be clear, in memory management, cache refers to page cache. Page cache is a page cache for file systems and stores file data (metadata and file data) of files.

2.1.2 Which Caches cannot be Reclaimed

We run the reclaim cache command

[root@k8s-dbg-master-1 ~]# echo 3 > /proc/sys/vm/drop_caches
[root@k8s-dbg-master-1 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:          32117        8622         720       16056       22773         949
Swap:         16383         216       16167
Copy the code

From the above we know that even if we run the cache clearing command, there are still some parts of the cache that cannot be reclaimed, including TMPFS, shared memory, and MMAP memory with MAP_SHARED flag. This part does not explain in depth, interested to try their own.

2.2, the buffer

2.2.1 definition

(1) In memory management, buffer refers to: buffer cache. A buffer cache is the read/write buffer of a block device. Moving on, an operating system operates on a disk as a block, a block containing one or more sectors but no larger than a page.

(2) Buffers are mainly used to cache metadata information (dentries, inodes) in a file system and other blocks that are not file data, such as metadata and RAW block I/O. Therefore, a separate buffer cache is required.

2.3 Relationship between the two

After kernel 2.6, the two structures were unified. Both are page cache, and page contains a buffer structure, so our free command also unified the two structures. When writing to a file, the contents of the page cache are changed, while the buffer cache marks the page as different buffers and records which blocks are modified. This way, when writing back dirty data, the whole page is not written back. And just write back the modified block.

Third, case analysis

3.1 Case Analysis

We’ll examine cache directly using cases to see how it might be used in an operating system. Prepare a large file and read it.

(1) Manually clear the cache

[root@k8s-dbg-master-1 ~]# echo 3 > /proc/sys/vm/drop_caches       

[root@k8s-dbg-master-1 ~]# cat /proc/meminfo
MemTotal:       32887860 kB
MemFree:        15764108 kB
MemAvailable:   17081976 kB
Buffers:            1792 kB
Cached:          1191456 kB
Copy the code

(2) Execution of commands and results

[root@k8s-dbg-master-1 ~]# cp hyperkube /tmp/hyperkube
[root@k8s-dbg-master-1 ~]# cat /proc/meminfo
MemTotal:       32887860 kB
MemFree:        15240656 kB
MemAvailable:   17092320 kB
Buffers:           18016 kB
Cached:          1708088 kB
Copy the code

As you can see, cached and buffer have increased

Four, the problem

1. Now try to answer the initial question: What is the current state of system memory

5. References

1, www.cnblogs.com/sparkdev/p/…

2,linuxperf.com/?p=142