In Linux, free is a built-in command to check the memory usage status. It can display the usage status of system physical memory, virtual memory (swap swap partition), shared memory and system cache. The syntax is "free [option] "; The output of the free command is very similar to the memory part of the top command.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Linux free command: Check memory usage status
The free command is used to display the system memory status, including system physical memory, virtual memory (swap swap partition), and shared memory And the system cache usage, the output is very similar to the memory part of the top command.
The basic format of the free command is as follows:
# free [选项]
Copy after login
Table 1 lists the commonly used options of this command and their respective meanings.
Table 1 Free command common options and their meanings
Options
Meaning
##- b
Displays memory usage in Byte (byte).
-k
Displays memory usage in KB. This option is the default option of the free command.
-m
Displays memory usage in MB.
-g
Displays memory usage in GB.
-t
In the final result of the output, output the total amount of memory and swap partition.
-o
Do not display the system buffer column.
-s Interval seconds
Continue to display memory usage according to the specified interval.
free 命令可以显示系统中剩余及已用的物理内存和交换内存,以及共享内存和被核心使用的缓冲区。
如果加上 -h 选项,输出的结果会友好很多:
有时我们需要持续的观察内存的状况,此时可以使用 -s 选项并指定间隔的秒数:
$ free -h -s 3
Copy after login
The above command outputs memory usage every 3 seconds until you press ctrl c.
Since the free command itself is relatively simple, the focus of this article will be on how to understand the current memory usage of the system through the free command.
Introduction to output
Let’s first explain the output content:
# The
##Mem line (second line) is the memory usage.
Swap line (the third line) is the usage of swap space.
##total column displays the total available physical memory and swap of the system size of space.
##used column shows that it has been Physical memory and swap space used.
##free Columns show how much physical memory and swap space are available for use.
The #shared column shows the amount of physical memory used by sharing.
#buff/cache column displays the physical memory size used by buffer and cache.
The
#available column displays the amount of physical memory that is still available to the application. # I think only after understanding some basic After understanding the concept, the above output can help us understand the memory status of the system.
buff/cache
Let me ask a question first: buffer and cache should be two types of memory, but why does the free command change What about putting them together? Answering this question requires some preparation. Let us first clarify the meaning of buffer and cache.
buffer
In the operating system, it refers to buffer cache, which is generally translated as "buffer" in Chinese. To understand buffers, two other concepts must be understood: "sector" and
"piece". A sector is the smallest addressing unit of a device, also called a "hard sector" or "device block". Block is the smallest addressing unit of the file system in the operating system, also called "file block" or "I/O
"block". Each block contains one or more sectors, but cannot be larger than a page, so a page can hold one or more blocks in memory. When a block is loaded into memory, it is stored in a buffer area. Each buffer corresponds to a block, which is equivalent to the representation of a disk block in memory (the picture below is from the Internet):
Note that the buffer cache only has the concept of blocks and not files. It just moves the blocks on the disk directly into the memory without caring about what format of files are stored in the blocks.
cache refers to page cache in the operating system, and Chinese is generally translated as "page cache". The page cache is a disk cache implemented by the kernel. It is mainly used to reduce the impact on the disk. I/O operations. Specifically, by caching the data in the disk into physical memory, access to the disk is changed into access to physical memory. The page cache caches memory pages. In the cache Pages come from reading and writing ordinary files, block device files (this refers to the buffer cache) and memory mapped files. We can understand the caching of ordinary files by the page cache in this way: when the kernel To read a file (such as
/etc/hosts), it will first check whether the data of this file is already in the page cache. If so, give up accessing the disk and read directly from memory. This behavior is called a cache hit. If the data is not in the cache, it means a cache miss, and the kernel will schedule the block.
I/O operations read data from disk. The kernel then places the read data into the page cache. This cache targets files recognized by the file system (such as /etc/hosts). The page cache's cache of block device files is the buffer cahce we introduced earlier. Because individual disk blocks are also stored in the page cache through the buffer (the buffer is ultimately hosted by the page cache).
We should be clear at this point: whether it is a buffer or a page cache, they are implemented in the same way. The buffer is just a conceptually special page cache. Then why
Isn't the free command directly called cache instead of buff/cache? This is because buffer and page cache implementations are not inherently unified. in linux
Kernel 2.4
Only then can they be unified. There were two separate disk caches in earlier kernels: the page cache and the buffer cache. The former caches pages and the latter caches buffers. Once you know the story, the names of the columns in the output may no longer matter.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn