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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Chat Commands and How to Use Them
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    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)

    Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

    The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

    Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

    Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

    Centos stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

    CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

    How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

    How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

    How to install centos How to install centos Apr 14, 2025 pm 09:03 PM

    CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

    What are the backup methods for GitLab on CentOS What are the backup methods for GitLab on CentOS Apr 14, 2025 pm 05:33 PM

    Backup and Recovery Policy of GitLab under CentOS System In order to ensure data security and recoverability, GitLab on CentOS provides a variety of backup methods. This article will introduce several common backup methods, configuration parameters and recovery processes in detail to help you establish a complete GitLab backup and recovery strategy. 1. Manual backup Use the gitlab-rakegitlab:backup:create command to execute manual backup. This command backs up key information such as GitLab repository, database, users, user groups, keys, and permissions. The default backup file is stored in the /var/opt/gitlab/backups directory. You can modify /etc/gitlab

    How to mount hard disk in centos How to mount hard disk in centos Apr 14, 2025 pm 08:15 PM

    CentOS hard disk mount is divided into the following steps: determine the hard disk device name (/dev/sdX); create a mount point (it is recommended to use /mnt/newdisk); execute the mount command (mount /dev/sdX1 /mnt/newdisk); edit the /etc/fstab file to add a permanent mount configuration; use the umount command to uninstall the device to ensure that no process uses the device.

    What to do after centos stops maintenance What to do after centos stops maintenance Apr 14, 2025 pm 08:48 PM

    After CentOS is stopped, users can take the following measures to deal with it: Select a compatible distribution: such as AlmaLinux, Rocky Linux, and CentOS Stream. Migrate to commercial distributions: such as Red Hat Enterprise Linux, Oracle Linux. Upgrade to CentOS 9 Stream: Rolling distribution, providing the latest technology. Select other Linux distributions: such as Ubuntu, Debian. Evaluate other options such as containers, virtual machines, or cloud platforms.

Table 1 Free command common options and their meanings
OptionsMeaning