free

Show memory usage

Supplementary instructions

free command can display the amount of unused and used memory in the current system, and can also display the memory buffer used by the kernel.

grammar

free(option)

Options

-b # Display memory usage in Byte;
-k # Display memory usage in KB;
-m # Display memory usage in MB;
-g # Display memory usage in GB.
-o # Do not display the buffer adjustment column;
-s<interval seconds> #Continuously observe memory usage;
-t #Display memory sum column;
-V # Display version information.

Example

free -t # Display memory usage information in the form of sum
free -s 10 # Periodically query memory usage information and execute the command every 10 seconds

Show memory usage

free -m
              total used free shared buffers cached
Mem: 2016 1973 42 0 163 1497
-/+ buffers/cache: 312 1703
Swap: 4094 0 4094

Explanation of the Mem line in the first part:

total: total memory;
used: the amount of memory that has been used;
free: the number of free memory;
shared: currently deprecated;
buffers Buffer: number of cache memory;
cached Page: Number of cache memory.

Relationship: total = used + free

Explanation of the second part (-/+ buffers/cache):

(-buffers/cache) used memory number: used – buffers – cached in the Mem line of the first part
(+buffers/cache) Number of free memory: free + buffers + cached in the Mem line of the first part

It can be seen that -buffers/cache reflects the memory actually eaten by the program, while +buffers/cache reflects the total amount of memory that can be appropriated.

The third part refers to the swap partition.

The fourth line of the output result is the swap partition SWAP, which is what we usually call virtual memory. Difference: The difference between used/free in the second line (mem) and used/free in the third line (-/+ buffers/cache). The difference between the two lies in the perspective of usage. The first line is from the perspective of the OS. Because for the OS, buffers/cached are all used, so its available memory is 2098428KB and the used memory is 30841684KB. These include +buffers+cached used by the kernel (OS) and +Application (X, oracle, etc).

The third line refers to from the application point of view, for the application, buffers/cached is equal to available, because buffer/cached is to improve the performance of file reading, when the application needs to use memory time, buffer/cached will be recycled quickly.

So from the perspective of the application, available memory = system free memory + buffers + cached. For example, the available memory of this machine is:

18007156=2098428KB+4545340KB+11363424KB

Next, we explain when memory will be swapped, and in what way.

When the available memory is less than the rated value, a swap will occur. How to see the rating:

cat /proc/meminfo

MemTotal: 16140816 kB
MemFree: 816004 kB
MemAvailable: 2913824 kB
Buffers: 17912 kB
Cached: 2239076 kB
SwapCached: 0 kB
Active: 12774804 kB
Inactive: 1594328 kB
Active(anon): 12085544 kB
Inactive(anon): 94572 kB
Active(file): 689260 kB
Inactive(file): 1499756 kB
Unevictable: 116888 kB
Mlocked: 116888 kB
SwapTotal: 8191996 kB
SwapFree: 8191996 kB
Dirty: 56 kB
Writeback: 0 kB
AnonPages: 12229228 kB
Mapped: 117136 kB
Shmem: 58736 kB
Slab: 395568 kB
SReclaimable: 246700 kB
SUnreclaim: 148868 kB
KernelStack: 30496 kB
PageTables: 165104 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 16262404 kB
Committed_AS: 27698600 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 311072 kB
VmallocChunk: 34350899200 kB
HardwareCorrupted: 0 kB
AnonHugePages: 3104768 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 225536 kB
DirectMap2M: 13279232 kB
DirectMap1G: 5242880 kB

Swapping will reduce the number of physical pages used in the system in three ways:

In fact, using swap sparingly does not affect system performance.

So buffers and cached are both caches. What is the difference between them?

In order to improve the efficiency of disk access, Linux has made some elaborate designs. In addition to caching dentry (used in VFS to accelerate the conversion of file path names to inodes), it also adopts two main Cache methods:

Buffer Cache and Page Cache. The former is for reading and writing disk blocks, and the latter is for reading and writing file inodes. These caches effectively shorten the time of I/O system calls (such as read, write, getdents). Disk operations are divided into logical level (file system) and physical level (disk block). These two caches cache logical and physical level data respectively.

Page cache is actually for the file system and is a cache of files. Data at the file level will be cached in the page cache. The logical layer of the file needs to be mapped to the actual physical disk, and this mapping relationship is completed by the file system. When the data in the page cache needs to be refreshed, the data in the page cache is handed over to the buffer cache, because the buffer cache caches disk blocks. However, this processing has become very simple after the 2.6 version of the kernel, and there is no real cache operation.

Buffer cache is a cache for disk blocks. That is, in the absence of a file system, data that directly operates on the disk will be cached in the buffer cache. For example, the metadata of the file system will be cached in the buffer cache.

Simply put, the page cache is used to cache file data, and the buffer cache is used to cache disk data. When there is a file system, when operating on a file, the data will be cached in the page cache. If you directly use tools such as dd to read and write from the disk, the data will be cached in the buffer cache.

So when we look at Linux, as long as you don't use swap space, you don't have to worry about having too little memory. If you often use a lot of swap, you may have to consider adding physical memory. This is also the standard for Linux to see if the memory is enough.

If it is an application server, you usually only look at the second line, +buffers/cache, which means that the free memory is too little for the application, and it is time to consider optimizing the program or adding memory.