Linux manual release system memory

Let's look at CentOS 8 system free command output. Of course, it is only used to view the system memory usage, not to release.

# free -mh
              total        used        free      shared  buff/cache   available
Mem:          821Mi       204Mi        97Mi        10Mi       519Mi       465Mi
Swap:            0B          0B          0B

free command displays the idle system and the use of all physical and swap (the swap) memory, including cache buffers and kernel are used.
For more information stored in the / proc / meminfo in.
free command output meaning of each column is as follows:

total     安装的内存总量 (MemTotal and SwapTotal in /proc/meminfo)
used      使用掉的内存量 (total - free - buffers - cache)
free      没有使用的内存量 (MemFree + SwapFree in /proc/meminfo)
shared    Memory used (mostly) by tmpfs (Shmem in /proc/meminfo)
buffers   Memory used by kernel buffers (Buffers in /proc/meminfo)
cache     Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)
buff/cache
       Sum of buffers and cache
available
       如果新起应用程序,那么有多少内存可供使用,不包括交换内存(swap)。
       Unlike the data provided  by  the  cache  or free  fields,  
       this  field  takes  into account page cache and also that not all reclaimable memory slabs 
       will be reclaimed due to items being in use 
       (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)

From the above:

  1. total = used + free + buff/cache
  2. buff/cache = kernel buffers + page cache + slabs
  3. total - used = free + buff/cache > available

In order to improve the efficiency of disk access, Kernel do some design: In addition to the dentry cache (for VFS, accelerated file pathname to inode conversion), Cache also taken two major ways:. Buffer Cache Cache and Page
Buffer Cache for reading and writing disk blocks; page cache for read and write the file inode. These Cache effectively shortening the time I / O system calls (such as read, write, getdents) of.

So, if you want to manually release the buffer cache and page cache memory occupied, how to do it?

The first step, run the sync command.
The sync command all unwritten buffers written to disk system, comprising a modified i-node, the delayed block I / O read and write, and mapping file.

The second step, write memory file / proc / sys / vm / drop_caches

# 释放 page cache
echo 1 > /proc/sys/vm/drop_caches

# 释放 dentries and inodes
echo 2 > /proc/sys/vm/drop_caches

# 释放 page cache, dentries and inodes
echo 3 > /proc/sys/vm/drop_caches

The contents of the file defaults to 0. 3 or more writing method, you can choose one.
Immediately after, and then run the free command, you can see the buff / caches becomes much smaller than the original.

# free -mh
              total        used        free      shared  buff/cache   available
Mem:          821Mi       161Mi       534Mi        20Mi       125Mi       524Mi
Swap:            0B          0B          0B

(Finish)

Published 169 original articles · won praise 332 · views 480 000 +

Guess you like

Origin blog.csdn.net/nirendao/article/details/104097617