Discussion on cache, buffer and swap caused by free

Keywords: Linux less Anaconda

Discussion on cache, buffer and swap caused by free

$ free
             total       used       free     shared    buffers     cached
Mem:       8062624    7910056     152568          0       5232     964468
-/+ buffers/cache:    6940356    1122268
Swap:      4194296    2592660    1601636

total: the actual amount of physical memory
used: physical memory usage (including buffers and cached)
Free: amount of free physical memory (excluding buffers and cached)
Shared: shared memory
buffers: the remaining amount of unused buffer s allocated by the system.
cached: the amount of unused cache allocated by the system.
Mem: overall memory usage

  • -buffers/cache: actual memory space used - buffers - cached (6940356 =7910056 - 5232-964468)
  • +buffers/cache: actual free memory space free + buffers + cached(1122268 =152568 + 5232 + 964468)
  • Swap: swap partition usage (disk space usage as part of virtual memory usage)

When there are applications that need to read and write disk data, the system reads the relevant data from disk to memory. If enough memory is allocated to all applications, and the physical memory remains, linux will try to reuse the free memory to improve the overall I/O efficiency. The method is to divide the remaining memory into cache and buffer to use. After the data read from the disk to the memory is read by the relevant application program, if there is any remaining memory, this part of the data will be stored in the cache for the second reading to avoid re reading the disk. When an application program has modified data in memory, because the speed of writing to disk is relatively low, when there is free memory, the data is first stored in buffer, and then written to disk at a later time, so that the application program can continue the subsequent operations without waiting for the completion of the operation of writing these data to disk. If the system needs more memory at a certain time, it will erase the cache part and write the contents of the buffer to the disk, so as to release the two parts of memory to the system for use, so when reading the contents of the cache again, it needs to read from the disk again. If the physical memory is not enough, part of the data in the memory is imported to the disk, so that part of the disk space is used as virtual memory, also known as Swap.

cache
cache Often used on disk I/O On the request, if more than one process wants to access a file, the file is made cache To facilitate the next visit, which can provide system performance. We have cache It is to reduce the data cached by reading to the disk. It needs to be the same as cache cache To make a distinction, the cache is located in CPU A small but high-speed memory between and main memory. Because CPU It's much faster than main memory, CPU To access data directly from memory, you need to wait for a certain period of time, Cache Kept in CPU Part of the data just used or recycled, when CPU When using this part of data again, you can Cache Direct call in, which reduces CPU The waiting time improves the efficiency of the system. Cache It's divided into two levels Cache(L1 Cache)And level two Cache(L2 Cache),L1 Cache Integration in CPU Inside, L2 Cache In the early days, it was usually welded on the main board, and now it is also integrated in the CPU Internal, common capacity is 256 KB Or 512 KB L2 Cache.

buffer
Buffer It is mainly designed for the write operation between memory and hard disk. The purpose is to centralize the write operation, reduce the disk fragments and the hard disk repeated addressing process, and improve the performance. When the application has modified the data in memory, due to the low speed of writing to the disk, the data will be saved when there is free memory buffer,Write to the disk at a later time, so that the application can continue the subsequent operations without waiting for the data to be written to the disk. stay Linux There is a daemons in the system that will be emptied periodically Buffer Write the contents of to the hard disk, when executed manually sync The above operations are also triggered on command.

swap
Linux In order to improve the efficiency and speed of reading and writing, the kernel will cache files in memory, which is Cache Memory(Cache memory). Even after your program runs, Cache Memory It will not release automatically. This will cause you to Linux After the program in the system reads and writes files frequently, you will find that the available physical memory becomes less. When the physical memory of the system is not enough, a part of the space in the physical memory needs to be released for the current running program. The released space may come from programs that have not been operated for a long time, and the released space is temporarily saved to Swap Space, wait until those programs are ready to run, and then Swap Restore the saved data in the partition to memory. In this way, the system always runs out of physical memory Swap Exchange.

From the above analysis, we can see that there is not much free physical memory, which does not necessarily mean that the system is in poor running state, because the cache and buffer parts of memory can be reused at any time, in a sense, these two parts of memory can also be regarded as additional free memory. If swap is called frequently and bi and bo are not 0 for a long time, the memory resources may be tight.

swap development

View the status of swap partition

$ free
             total       used       free     shared    buffers     cached
Mem:       8062624    7910224     152400          0       3720     771432
-/+ buffers/cache:    7135072     927552
Swap:      4194296    2963372    1230924

$ cat /proc/swaps
Filename				Type		Size	Used	Priority
/dev/sda3                               partition	4194296	2963372	-1

$ swapon -s
Filename				Type		Size	Used	Priority
/dev/sda3                               partition	4194296	2963372	-1

Swap off, swap on plus - a option, turn off or on all devices set to swap in etc/fstab file. Because the disk is used as virtual memory in the swap partition, before the swap partition is closed, the swap partition data will be transferred to memory first, and then the swap partition will be closed.

$ cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Aug 21 06:35:07 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel6.5-rhel_root /                       ext4    defaults        1 1
UUID=579f15d3-0cc5-4d59-bea8-a92f0d5f5ba1 /boot                   ext4    defaults        1 2
/dev/mapper/rhel6.5-rhel_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/var/swap     swap      swap    defaults 0 0

$ free
             total       used       free     shared    buffers     cached
Mem:       8193500    4971636    3221864          0      12352    2105788
-/+ buffers/cache:    2853496    5340004
Swap:      8388592    2660280    5728312

$ swapoff -a -v
swapoff on /dev/mapper/rhel6.5-rhel_swap
swapoff on /var/swap

$ free
             total       used       free     shared    buffers     cached
Mem:       8193500    7426224     767276          0      13840    3051452
-/+ buffers/cache:    4360932    3832568
Swap:            0          0          0

$ swapon -a -v
swapon on /dev/mapper/rhel6.5-rhel_swap
swapon: /dev/mapper/rhel6.5-rhel_swap: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/rhel6.5-rhel_swap: pagesize=4096, swapsize=4294967296, devsize=4294967296
swapon on /var/swap
swapon: /var/swap: insecure permissions 0644, 0600 suggested.
swapon: /var/swap: found swap signature: version 1, page-size 4, same byte order
swapon: /var/swap: pagesize=4096, swapsize=4294967296, devsize=4294967296

$ free
             total       used       free     shared    buffers     cached
Mem:       8193500    7433176     760324          0      13936    3051704
-/+ buffers/cache:    4367536    3825964
Swap:      8388592          0    8388592

swapoff turns off the specified switch partition, and swapon turns on the specified switch partition

$ free
             total       used       free     shared    buffers     cached
Mem:       8193500    7453156     740344          0      14552    3052848
-/+ buffers/cache:    4385756    3807744
Swap:      8388592          0    8388592

$ swapoff /var/swap
$ free
             total       used       free     shared    buffers     cached
Mem:       8193500    7449280     744220          0      14776    3052984
-/+ buffers/cache:    4381520    3811980
Swap:      4194296          0    4194296

$ swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-0                               partition	4194296	0	-1

$ swapon /var/swap
$ swapon -s
Filename				Type		Size	Used	Priority
/dev/dm-0                               partition	4194296	0	-1
/var/swap                               file		4194296	0	-2

$ free
             total       used       free     shared    buffers     cached
Mem:       8193500    7458464     735036          0      15008    3053548
-/+ buffers/cache:    4389908    3803592
Swap:      8388592          0    8388592

Two methods of temporarily modifying the swappiness parameter fail after system restart. If you want to make permanent changes, change the value of vm.swappiness in the configuration file / etc/sysctl.conf, and then restart the system. Swap uses part of the disk space as virtual memory. The purpose of the design is to write part of the data in the memory to the disk after the new read and write requests come in, so that part of the disk space can be used as virtual memory. The swap partition of Linux system is not used until all the physical memory is exhausted, but is controlled by the value of the swap parameter. The value of this parameter can be 0-100 to control the usage of swap in the system. The higher the value, the more active the kernel will be in using swap space. The default is 60. Note: This is only a weight value, not a percentage value, which involves the complex algorithm of the system kernel.

$ sysctl -q vm.swappiness
vm.swappiness = 60

$ sysctl vm.swappiness=10
vm.swappiness = 10

$ sysctl -q vm.swappiness
vm.swappiness = 10

$ echo 60 > /proc/sys/vm/swappiness
$ cat /proc/sys/vm/swappiness
60
$ sysctl -q vm.swappiness
vm.swappiness = 60
78 original articles published, praised 17, visited 50000+
Private letter follow

Posted by abx_2112 on Sun, 19 Jan 2020 00:40:57 -0800