Detailed explanation and usage example of linux free command (check memory usage)

Reprinted: http://www.jb51.net/LINUXjishu/152017.html

1. Command format:

free [parameter]

2. Command function:

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

3. Command parameters:

-b Display memory usage in Bytes. 

-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 in seconds> Continuously observe memory usage. 

-t Displays the memory sum column. 

-V Display version information. 

4. Example of use:

Example 1: Displaying memory usage

Order:

 

copy code
code show as below:

free
free -g
free -m

 

output:

 

copy code
code show as below:

[root@SF1150 service]# free
total used free shared buffers cached
Mem: 32940112 30841684 2098428 0 4545340 11363424
-/+ buffers/cache: 14932920 18007192
Swap: 32764556 1944984 30819572</p> <p>
[root@SF1150 service]# free -g
total used free shared buffers cached
Mem: 31 29 2 0 4 10
-/+ buffers/cache: 14 17
Swap: 31 1 29</p> <p>
[root@SF1150 service]# free -m
total used free shared buffers cached
Mem: 32168 30119 2048 0 4438 11097
-/+ buffers/cache: 14583 17584
Swap: 31996 1899 30097

 

Detailed description of the output of the free command:

Here is an explanation of these values:

total: The total size of physical memory.

used: how much has been used.

free: how much is available.

Shared: The total amount of memory shared by multiple processes.

Buffers/cached: The size of the disk cache.

Line 3 (-/+ buffers/cached):

used: how much has been used.

free: how much is available.

The fourth line is the swap partition SWAP, which is what we usually call virtual memory.

Difference: The difference between the used/free of the second line (mem) and the used/free of the third line (-/+ buffers/cache). The difference between the two is from the point of view of use. The first line is from the point of view of the OS. Because for the OS, buffers/cached are all used, so his available memory is 2098428KB, and the used memory is 30841684KB. Among them, the kernel (OS) uses +buffers+cached used by +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 At times, buffer/cached will be reclaimed very quickly.

So from the application point of view, available memory=system free memory+buffers+cached.

For example, the available memory of this machine is:

18007156=2098428KB+4545340KB+11363424KB

The following explains when memory is swapped, and in what way. 

When the available memory is less than the rated value, it will meet to swap. How to see the rated value: 

Command: cat /proc/meminfo 

output:

 

copy code
code show as below:

[root@SF1150 service]# cat /proc/meminfo
MemTotal: 32940112 kB
MemFree: 2096700 kB
Buffers: 4545340 kB
Cached: 11364056 kB
SwapCached: 1896080 kB
Active: 22739776 kB
Inactive: 7427836 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 32940112 kB
LowFree: 2096700 kB
SwapTotal: 32764556 kB
SwapFree: 30819572 kB
Dirty: 164 kB
Writeback: 0 kB
AnonPages: 14153592 kB
Mapped: 20748 kB
Slab: 590232 kB
PageTables: 34200 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 49234612 kB
Committed_AS: 23247544 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 278840 kB
VmallocChunk: 34359459371 kB
HugePages_Total: 0HugePages_Free: 0HugePages_Rsvd: 0Hugepagesize: 2048 kB

 

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

1. Reduce the size of the buffer and page cache, 

2. Swap out system V type memory pages,  

3. Swap out or discard pages. (The memory page occupied by Application, that is, insufficient physical memory). 

In fact, does a small amount of swap use affect system performance?

Then buffers and cached are both caches, what is the difference between the two?

In order to improve the efficiency of disk access, Linux has made some careful designs. In addition to caching dentry (for VFS, to speed up the conversion of file pathnames to inodes), it also adopts two main Cache methods: Buffer Cache and Page Cache. The former is for reading and writing of disk blocks, and the latter is for reading and writing of file inodes. These caches effectively shorten the time of I/O system calls (such as read, write, getdents).

The operation of the disk has the logical level (file system) and the physical level (disk block). These two kinds of Cache cache logical and physical level data respectively.

Page cache is actually for the file system, which is the cache of files, and the data at the file level will be cached to the page cache. The logical layer of the file needs to be mapped to the actual physical disk, and this mapping relationship is done 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. But this kind of 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, the data that is directly operated 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. In the case of a file system, if you operate a file, the data will be cached in the page cache. If you directly use tools such as dd to read and write to the disk, the data will be cached in the buffer cache.

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

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

Example 2: Display memory usage information as a sum

Command: free -t 

output:

 

copy code
code show as below:

[root@SF1150 service]# free -t 
total used free shared buffers cached
Mem: 32940112 30845024 2095088 0 4545340 11364324
-/+ buffers/cache: 14935360 18004752Swap: 32764556 1944984 30819572Total: 65704668 32790008 32914660[root@SF1150 service]#

 

illustrate:

Example 3: Periodically query memory usage information

Command: free -s 10

output:

 

copy code
code show as below:

[root@SF1150 service]# free -s 10
total used free shared buffers cached
Mem: 32940112 30844528 2095584 0 4545340 11364380
-/+ buffers/cache: 14934808 18005304Swap: 32764556 1944984 30819572
total used free shared buffers cached
Mem: 32940112 30843932 2096180 0 4545340 11364388
-/+ buffers/cache: 14934204 18005908Swap: 32764556 1944984 30819572

 

illustrate:

Execute a command every 10s

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324893228&siteId=291194637