Home  >  Article  >  What is free in linux

What is free in linux

青灯夜游
青灯夜游Original
2023-03-02 09:59:146923browse

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.

What is free in linux

#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 [选项]

Table 1 lists the commonly used options of this command and their respective meanings.

##- 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. -gDisplays memory usage in GB. -tIn the final result of the output, output the total amount of memory and swap partition. -oDo 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

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.

    free 与 available

    在 free 命令的输出中,有一个 free 列,同时还有一个 available 列。这二者到底有何区别?
    free 是真正尚未被使用的物理内存数量。至于 available 就比较有意思了,它是从应用程序的角度看到的可用内存数量。Linux 内核为了提升磁盘操作的性能,会消耗一部分内存去缓存磁盘数据,就是我们介绍的 buffer 和 cache。所以对于内核来说,buffer 和 cache 都属于已经被使用的内存。当应用程序需要内存时,如果没有足够的 free 内存可以用,内核就会从 buffer 和 cache 中回收内存来满足应用程序的请求。所以从应用程序的角度来说,available  = free + buffer + cache。请注意,这只是一个很理想的计算方式,实际中的数据往往有较大的误差。

    交换空间(swap space)

    swap space 是磁盘上的一块区域,可以是一个分区,也可以是一个文件。所以具体的实现可以是 swap 分区也可以是 swap 文件。当系统物理内存吃紧时,Linux 会将内存中不常访问的数据保存到 swap 上,这样系统就有更多的物理内存为各个进程服务,而当系统需要访问 swap 上存储的内容时,再将 swap 上的数据加载到内存中,这就是常说的换出和换入。交换空间可以在一定程度上缓解内存不足的情况,但是它需要读写磁盘数据,所以性能不是很高。

    现在的机器一般都不太缺内存,如果系统默认还是使用了 swap 是不是会拖累系统的性能?理论上是的,但实际上可能性并不是很大。并且内核提供了一个叫做 swappiness 的参数,用于配置需要将内存中不常用的数据移到 swap 中去的紧迫程度。这个参数的取值范围是 0~100,0 告诉内核尽可能的不要将内存数据移到 swap 中,也即只有在迫不得已的情况下才这么做,而 100 告诉内核只要有可能,尽量的将内存中不常访问的数据移到 swap 中。在 ubuntu 系统中,swappiness 的默认值是 60。如果我们觉着内存充足,可以在 /etc/sysctl.conf 文件中设置 swappiness:

    vm.swappiness=10

    如果系统的内存不足,则需要根据物理内存的大小来设置交换空间的大小。具体的策略网上有很丰富的资料,这里笔者不再赘述。

    /proc/meminfo 文件

    其实 free 命令中的信息都来自于 /proc/meminfo 文件。/proc/meminfo 文件包含了更多更原始的信息,只是看起来不太直观:

    $ cat /proc/meminfo

    有兴趣的同学可以直接查看这个文件。

    总结

    free 命令是一个既简单又复杂的命令。简单是因为这个命令的参数少,输出结果清晰。说它复杂则是因为它背后是比较晦涩的操作系统中的概念,如果不清楚这些概念,即便看了 free 命令的输出也 get 不到多少有价值的信息。

    相关推荐:《Linux视频教程

Table 1 Free command common options and their meanings
Options Meaning

The above is the detailed content of What is free in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:what is linux bashrcNext article:what is linux bashrc