The free command displays the free and used physical memory, swap memory, and buffer used by the kernel in Linux. The free command is one of the most frequently used Linux system monitoring tools.

The command format

Free [parameter]

Command function

The free command displays the used and free memory, including physical memory, interactive area memory (SWAP), and kernel buffer memory. Shared memory will be ignored.

The command parameter

  • -b Displays the memory usage in bytes.
  • -k Displays the memory usage in KB.
  • -m Displays the memory usage in MB.
  • -g Displays the memory usage in GB.
  • -o Does not display the buffer adjustment column.
  • -s< interval seconds > Continuously observe memory usage.
  • -t Displays the total memory column.
  • -v Displays the version information.

Display memory usage

The output profile

  • The Mem line (second line) is the memory usage.
  • The Swap line (third line) is the Swap space usage.
  • The total column shows the total available physical memory and swap space on the system.
  • The Used column shows the physical memory and swap space that has been used.
  • The free column shows how much physical memory and swap space is available.
  • The shared column shows how much physical memory is being shared.
  • The buff/cache column shows how much physical memory is used by buffer and cache.
  • The AVAILABLE column shows the amount of physical memory that can still be used by the application.

Buffers and cached are both caches. What is the difference

Here’s a question: Buffer and cache are two different types of memory, but why does the free command put them together? Answering this question requires some preparation. Let’s first clarify what buffer and cache mean.

In addition to caching dentries (used in the VFS to speed up file path name to inode conversion), Linux uses two main Cache methods to improve disk access efficiency: Buffer Cache and Page Cache. The former is for reading and writing disk blocks, while the latter is for reading and writing file inodes. These caches effectively shorten the time it takes to make I/O system calls (like read,write, and getdents).

A buffer is a buffer cache in an operating system. To understand buffers, two other concepts must be clarified: “sectors” and “blocks”. A sector is the smallest addressing unit of a device, also known as a “hard sector” or “device block”. A block is the smallest addressing unit of a file system in an operating system. It is also called a “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 called into memory, it is stored in a buffer. Each buffer corresponds to a block, which is equivalent to an in-memory representation of a disk block:

Note that a buffer cache has only the concept of a block, not a file. It simply moves blocks from disk into memory, regardless of the format of the file in the block.

Cache refers to page cache. Page caching is disk caching implemented by the kernel. It is mainly used to reduce I/O operations on disks. Specifically, access to disk is changed to access to physical memory by caching data on disk into physical memory. The page cache caches in-memory pages. Pages in the cache come from reading and writing to normal files, block device files, and memory-mapped files.

When the kernel reads a file (such as /etc/hosts), it checks to see if the file’s data is already in the page cache. If yes, access to disk is abandoned and read directly from memory. This behavior is called a cache hit. If the data is not in the cache, the cache is not hit, and the kernel schedules block I/O operations to read the data from disk. The kernel then puts the read data into the page cache. The target of this cache is files that the file system can recognize (such as /etc/hosts).

The page cache cache for block device files is the buffer CAHCE we described earlier. Because individual disk blocks are also stored in the page cache through the buffer (which is ultimately hosted by the page cache). A buffer is nothing more than a conceptually special type of page cache

So why isn’t the free command called cache instead of buff/cache? This is because buffer and page caching implementations are not inherently uniform. They were unified in Linux kernel 2.4. Older kernels had two separate disk caches: the page cache and the buffer cache. The former caches pages and the latter buffers. By the time you know the story, the names of the columns in the output may not matter anymore.

Free and available

In the output of the free command, there is a free column along with an available column. What’s the difference?

Free is the amount of physical memory that is truly unused. The interesting thing is available, which is the amount of memory available from an application’s perspective. To improve disk operation performance, the Linux kernel uses a portion of memory to cache disk data, namely buffer and cache. So for the kernel, both buffer and cache are used memory. When an application needs memory, if there is not enough free memory available, the kernel reclaims memory from buffer and cache to satisfy the application’s request. So from an application perspective, available = free + buffer + cache. Please note that this is only an ideal calculation method, and the actual data often have a large error.

Swap space

A swap space is an area on a disk. It can be a partition or a file. So the implementation can be a swap partition or a swap file. When the physical memory of the system is tight, Linux will save the infrequently accessed data in the memory to swap. In this way, the system has more physical memory to serve each process. When the system needs to access the contents stored in swap, the data in swap will be loaded into the memory. Swap space can alleviate the memory shortage to some extent, but it requires reading and writing disk data, so performance is not very high.

Today’s machines are generally not short of memory, if the system still uses swap by default, will it drag down the system performance? In theory, yes, but not in practice. And the kernel provides a parameter called swappiness, which configudes how urgent it is to move infrequently used data from memory to swap. This parameter ranges from 0 to 100. 0 tells the kernel not to move memory data to swap if possible, that is, only when it has to, and 100 tells the kernel to move infrequently accessed memory data to swap whenever possible. In Ubuntu, the default value of swappiness is 60. If we feel we have enough memory, we can set swappiness in /etc/sysctl.conf:

> vm.swappiness=10
Copy the code

Displays memory usage information as a sum

Periodically query the memory usage

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station