free Commands can be displayed Linux
Idle in the system , Used physical memory and swap Memory , And used by the kernel buffer. stay Linux System monitoring tools ,free Command is one of the most frequently used commands .

1. Command format :

free [ parameter ]

2. Command function :

free  Command to display system usage and free memory , Include physical memory , Interactive area memory (swap) And kernel buffer memory . Shared memory will be ignored

3. Command parameters :

-b   with Byte Display memory usage in units . 

-k   with KB Display memory usage in units . 

-m   with MB Display memory usage in units .

-g    with GB Display memory usage in units . 

-o   Do not show buffer adjustment columns . 

-s< Interval seconds >   Continuously observe memory usage . 

-t   Display memory sum column . 

-V   display version information . 

4. Use case :

example 1: Display memory usage

command :


free -g

free -m

output :

[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

[root@SF1150 service]# free -g

 total       used       free     shared    buffers     cached

Mem:            31         29          2          0              4            

-/+ 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      

-/+ buffers/cache:      14583      17584

Swap:        31996      1899         30097

explain :

Here is an explanation of these values :

total: Total physical memory size .

used: How large has been used .

free: How many are available .

Shared: Total memory shared by multiple processes .

Buffers/cached: Size of disk cache .

(-buffers/cache) used Number of memory : The first part of Mem In row used - buffers - cached
(+buffers/cache) free Number of memory : The first part of Mem In row free + buffers + cached
so -buffers/cache It reflects the memory actually eaten by the program , and +buffers/cache Reflects the total amount of memory that can be misappropriated .

The fourth line is swap partition SWAP Of , That's what we usually call virtual memory .

difference : Second line (mem)
Of used/free And the third line (-/+ buffers/cache) used/free The difference between .  The difference between the two lies in the perspective of use , The first line is from OS From the perspective of , Because for OS,buffers/cached  All belong to be used , So his available memory is 2098428KB, Used memory is 30841684KB, These include , kernel (OS) use +Application(X, oracle,etc) Used +buffers+cached.

The third line refers to an application perspective , For applications ,buffers/cached 
Is equal to available , because buffer/cached To improve the performance of file reading , When the application needs to use memory ,buffer/cached It will be recycled soon .

So from an application perspective , Available memory = system free memory+buffers+cached.

If the available memory is :


Next explain when memory will be swapped , And by whom . 

When available memory is less than rated , We'll have a meeting to exchange . How to see rating : 

command :

cat /proc/meminfo 

output :

[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

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

1. Reduce buffering and pages cache Size of , 

2. Put the system V Type of memory page swapped out ,  

3. Change out or discard pages .(Application  Memory pages occupied , I.e. insufficient physical memory ). 

in fact , Use sparingly swap Does it affect the system performance .

that buffers and cached It's all caching , What's the difference between the two ?

To improve disk access efficiency , Linux
Made some elaborate designs ,  Except for the right dentry Cache ( be used for VFS, Acceleration file pathname to inode Conversion of ),  There are also two main Cache mode :Buffer Cache and Page Cache. The former is for disk block reading and writing , The latter is for documents inode Reading and writing of . these ones here Cache Effectively shortens  I/O system call ( such as read,write,getdents) Time of .

The operation of disk has logic level ( file system ) And physical ( Disk block ), These two Cache That is to cache logical and physical data separately .

Page cache It's actually for the file system , Is the cache of files , Data at the file level is cached to page cache
. The logical layer of the file needs to be mapped to the actual physical disk , This mapping is done by the file system . When page cache When the data of needs to be refreshed ,page cache Data in to buffer cache, because Buffer Cache Cache disk block . But this treatment 2.6 It's very simple after the kernel version , There's no real sense cache operation .

Buffer cache Cache for disk blocks , That is, without a file system , Data directly operating on the disk will be cached to buffer cache
in , for example , The metadata of the file system will be cached to buffer cache in .

To put it simply ,page cache
Used to cache file data ,buffer cache Used to cache disk data . With a file system , File operation , Then the data will be cached to page cache, If directly adopted dd And other tools to read and write to the disk , Then the data will be cached to buffer cache.

So let's see linux,
As long as you don't swap Exchange space of , Don't worry about your memory . If often swap Use a lot , Maybe you should consider adding physical memory . This is also linux Standard to see if memory is enough .

If it's an application server , Generally, I only look at the second line ,+buffers/cache, For applications free Too little memory , It's time to optimize the program or add memory .

example 2: Display memory usage information in the form of sum

command :

 free -t 

output :

[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]

explain :

example 3: Periodic query of memory usage information

command :

free -s 10

output :

[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

explain :

each 10s  Execute one command


©2019-2020 Toolsou All rights reserved,
Unity Scene loading asynchronously ( Implementation of loading interface )ESP8266/ESP32 System : Optimize system startup time vue Of v-if And v-show The difference between JS How to operate college examination for the self-taught An overview of Marxism Faster RCNN Explanation of series algorithm principle ( note )CSS architecture design NOI2019 travels IAR Installation and use tutorial sort ( one ) bubble sort