Table of Contents
Linux free command: Check memory usage status
Introduction to output" >Introduction to output
buff/cache" >buff/cache
free 与 available" >free 与 available
交换空间(swap space)" >交换空间(swap space)
/proc/meminfo 文件" >/proc/meminfo 文件
总结" >总结
Home Common Problem What is free in linux

What is free in linux

Mar 02, 2023 am 09:59 AM
linux

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 [选项]
Copy after login

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
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.

    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
    Copy after login

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

    /proc/meminfo 文件

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

    $ cat /proc/meminfo
    Copy after login

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

    总结

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

    相关推荐:《Linux视频教程

    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 of this Website
    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1662
    14
    PHP Tutorial
    1261
    29
    C# Tutorial
    1234
    24
    Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

    The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

    vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

    vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

    How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

    To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

    vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

    VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

    What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

    The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

    How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

    There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

    How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

    Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

    laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

    To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Table 1 Free command common options and their meanings
OptionsMeaning