One linux command per day (45): free command

The free command can display the free and used physical memory and swap memory in the Linux system, as well as the buffer used by the kernel. Among the tools for Linux system monitoring, the free command is one of the most frequently used commands.
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.
#p#Pagination header#e#-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: Display memory usage
Command :
free
free -g
free -m
Output:
[root@SF1150 service]# free#p#page title#e#
             total used free shared buffers cached
Mem:      32940112   30841684    2098428          0    4545340   11363424
-/+ buffers/cache:   14932920   18007192
Swap:     32764556    1944984   30819572
[root@SF1150 service]# free -g
             total       used       free     shared    buffers     cached
Mem:            31         29          2          0          4         10#p#分页标题#e#
-/+ buffers/cache:         14         17
Swap:           31          1         29
[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
#p#Page title#e#Description:
The following is the explanation of these values:
total: 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.
The third line (-/+ 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
) , buffers/cached is equal to available, because buffer/cached is to improve the performance of file reading, when the application needs to use memory, buffer/cached will be quickly reclaimed.
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

Next, explain when the memory will be swapped and by what method.
When the available memory is less than the rated value, it will meet for swap. How to see the rated value:
command:
cat /proc/meminfo
output:
[root@SF1150 service]# cat /proc/meminfo
MemTotal: 32940112 #p#page title #e#kB
MemFree: 2096700 kB
Buffers: 4545340 kB
Cached: 11364056 kB
SwapCached: 1896080 kB
Active: 22739776 kB
Inactive: 7427836 kB
HighTotal: 0 kB
#p#Pagination Title k#e#HighFree: 0 kB
LowTotal:
3294012 2096700 kB
SwapTotal: 32764556 kB
SwapFree: 30819572 kB
Dirty: 164 kB
Writeback: 0 kB
AnonPages: #p#Page Title#e#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#p#Page Title# e#
VmallocTotal: 34359738367 kB
VmallocUsed: 278840 kB
VmallocChunk: 34359459371 kB
HugePages_Total: 0HugePages_Free: 0HugePages_Rsvd: 0Hugepagesize: 2048 kB

Swapping will reduce 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, 
#p#page title#e#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 path names 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, the data will be cached in the page cache when the file is operated. If the disk is directly read and written by tools such as dd, the data will be cached in the buffer cache.
#p#Page title#e# So let's look at Linux, as long as you don't need swap space, you don't have to worry about your own memory being too small. If you often use swap a lot, you may have to consider adding physical memory. This is also how Linux sees whether the memory is Sufficient standard.
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 in the form of a sum
Command :
  free -t
output:
[root@SF1150 service]# free -t
             total used free shared buffers cached
Mem: 32940112 30845024 2095088 0 4545340 11364324#p#Page title#e#
-/+ buffers/cache: 14935360 18004752Swap: 32764556 1944984 30819572Total: 65704668 32790008 32914660[root@SF1150 service]#Description

:

Example 3: Periodically query memory usage information
Command :
free -s 10
Output:
[root@SF1150 service] # free -s 10
# p # tab title # E # Total Used Free Shared buffers cached
Mem: 32,940,112 30,844,528 2095584 0 4.54534 million 11.36438 million
- / + buffers / Cache: 14,934,808 18005304Swap: 32,764,556 1,944,984 30,819,572 Total Used
             Free Shared buffers cached
Mem: 32,940,112 30,843,932 2.09618 million 0 4.54534 million 11,364,388
- / + buffers/cache: 14934204 18005908Swap: 32764556 1944984 30819572

Description:
#p#page title#e#Execute a command every 10s
Reprinted from: http://www.itxuexiwang.com/a/liunxjishu/2016/0306/249.html? 1457360628 One linux command per day (45): free command

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326896089&siteId=291194637