As long as the work involves Linux machine , Basically, there's a need , View memory usage , But what's right ? Previously used top command , There has always been a misunderstanding .

Why? top There will be mistakes in memory from the perspective of command ?

top It is a very useful system analysis tool , Process can be viewed in real time ,cpu Utilization rate , Memory usage , It's kind of like windows Task manager under . I always thought top What you see is real memory usage , later baidugoogle long time , I found my own design .=
First look top Memory usage shown after command , I use my own bricklayer vps Make a demonstration :
Cpu(s): 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem:
73728k total, 70048k used, 3680k free, 0k buffers Swap: 16384k total, 4696k
used, 11688k free, 64716k cached
You can see Mem: 73728k total, 70048k used, 3680k free, 0k
buffers This line , Memory usage . At first, I only looked at this line . Later, when I looked at the company's production machine , I feel confused , As long as it's a machine that's been running for a while , Memory total and
used Always very close ,free Few values , in other words “ Memory usage ” Very high , Even if I've turned off all kinds of services . But even so , There's nothing wrong with the server , Restart
tomcat service , Do some small pressure measurement , The performance is normal . So that's the question : Obviously, the memory is so tight , How can we run so many services , Even do stress tests ?—>
The answer is simple , These look like used A lot of memory , Most of it is caching , That's all Linux The memory management mechanism of .

Linux In Cache Memory

What is? Cache Memory( Cache memory ):

When you read and write ,Linux In order to improve the read and write performance and speed of kernel , Cache files in memory , This part of memory is Cache
Memory( Cache memory ). Even after your program runs ,Cache
Memory It will not release automatically . This will cause you to Linux After the program in the system reads and writes files frequently , You will find that there is little physical memory available . 
In fact, this cache memory (Cache Memory) Automatically release when you need to use memory , So you don't have to worry about not having memory available . 
Only when free subtract cached When the rest of the memory is tight , It is possible that the application does not have enough memory .

So in fact, just now top Memory usage seen , Part of it is caching , How much is the cache ? Look at the last one mem There's one at the back of that line buffers
,swap There's one at the back of that line cached, These two are cache sizes . So if you want to calculate the actual use of physical memory by an application , Should be used-cached-buffers
That's right , So just now top The physical memory usage you see is 70048k-64716k=5332k. That's why it doesn't work !

in addition , If you just want to see the memory usage , use free Commands are actually more intuitive :
total used free shared buffers cached Mem: 73728 70940 2788 0 0 64840 -/+
buffers/cache: 6100 67628 Swap: 16384 4500 11884
The information is described as follows : 
The first line describes the memory used by the system from a global perspective : 
total—— Total physical memory  
used—— Memory used , Generally, this value will be larger , Because this value includes cache+ Memory used by the application  
free—— Completely unused memory  
shared—— Application shared memory  
buffers—— cache , Mainly used for catalogue ,inode Value, etc (ls You can see the value increase in the large directory ) 
cached—— cache , For open files  
be careful -/+ buffers/cache: 6100 67628 This line . 
Previous value representation -buffers/cache—–> Do not include cache , Application physical memory usage , Namely -buffers/cache=used-buffers-cached
, So that's when the application comes in 6100k Memory . 

The last value represents +buffers/cache—–> All available memory sizes for the application ,free Plus cache value , Namely +buffers/cache=free+buffers+cached
, So there are 67628k Memory available for program use . 
in addition ,free Commands can also be used ”-m” parameter , The memory information displayed in this way is MB count , instead of KB, Large memory , This is more intuitive .
free -m ----------- total used free shared buffers cached Mem: 72 69 2 0 0 63
-/+ buffers/cache: 5 66 Swap: 16 4 11

use top Command or free Memory usage seen by command used in , Contains cache , If you want to see the real memory usage of the application , Should be used-cached-buffers
, Or directly free Command result -/+ buffers/cache Line information .

©2019-2020 Toolsou All rights reserved,
C Language programming to find a student's grade java Realize the function of grabbing red packets Random forest R Language implementation QCustomPlot series (5)- Real time dynamic curve Software testing BUG describe use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor )JS How to operate China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour Zimi apologizes :33W Gan charger delayed due to force majeure 2021 year 2 Chinese programming language ranking