introduction
When I was a student, I often heard teachers and classmates talk about linux
the importance of learning. But when I saw this command line interface, I had a headache, so I just learned it hastily. Alas, the game is still good!
But when I was tinkering with the server in the past two days, I found that a service I deployed always crashed, and I finally found that the server had insufficient memory.
Now it’s a headache to find out the cause of the problem! It’s harmful. It’s just that I didn’t study and practice well before. I will still get headaches when I should. It’s just a matter of time. (Ahem, I also advise latecomers, if you want to engage in related fields, you still need to have a solid foundation of basic knowledge, and it is always right to learn more about internal skills and mental methods).
Today's article briefly introduces linux
how to analyze memory in the system.
Introduction to common memory analysis tools
/proc/meminfo
cat /proc/meminfo
word | explain |
---|---|
MemTotal | The total system memory is usually smaller than the hardware memory because the system BIOS memory, kernel, etc. also occupy a certain amount of memory. |
MemFree | System free memory, physical memory remaining. |
MemAvailable | The difference between application available memory and MemFree is: MemFree it is the remaining space of the memory stick, which is at the system level. And MemAvailable is at the application level. Although some memory in the system is used, part of it can be recycled, such as: , Buffers etc. Cached When the application needs to use memory, the system will actively release this part of the memory. |
Buffers | Buffer memory, used to cache content that has not yet been "written" to disk. |
Cached | Cache what is "read" from disk. |
buffer
Used as io
a cache for device writes. And cache
is used as io
a read cache for the device. The devices here io
mainly refer to block device files and ordinary files on the file system.
MemAvailable = MemFree + Buffers + Cached
Please note that this is only an ideal calculation method, and actual data often have larger errors.
free
free -h
word | explain |
---|---|
Mem | row (second row) memory usage |
Swap | row (third row) swap space usage. You can think of it as windows virtual memory. When the memory is insufficient, part of the hard disk space is virtualized into memory. |
total | Total available physical memory. |
used | Physical memory and swap space used. |
free | Physical memory that is not yet used by the system. |
shared | Shared memory space. |
buff/cache | The column displays the size of the physical memory used by buffer and cache . This part of memory can be actively released by the system when the application needs to use it. Files and block device files that are frequently used in the system are usually cached. |
available | Available memory for the application. |
MemTotal = used + free + buff/cache
vmstat
vmstat 2 3
The first parameter represents how many seconds it will be executed, and the second parameter represents how many times it will be executed and exited. If the second parameter is not given, the content will continue to be output, and you need Ctrl+C
to exit.
word | explain |
---|---|
r | Procs (Process), the number of tasks waiting to be executed, that is, how many processes are waiting to cpu execute tasks, can be used for analysis cpu . Generally, this value will not be greater than cpu the number. If it exceeds, cpu a bottleneck will occur. |
b | How many processes are waitingio |
swpd | The size of the virtual memory being used, unitk |
free | size of free memory |
buff | The size used to buff buffer reads and writes to the block device |
cache | Used cache size, file systemcache |
and | The size of memory written from the swap area per second (unit: kb/s) |
so | The size written from memory to swap area per second |
bi | io Profiling, blocks read per second (read from disk) |
bo | io Profiling, blocks written per second (write to disk) |
in | Number of interrupts per second, including clock interrupts (lower is better) |
cs | Number of context switches per second (the smaller the better), if the value is too large, consider reducing the number of processes or threads |
us | User process execution consumes cpu time ( user time ). If it is exceeded for a long time 50% , you need to consider optimizing the algorithm or implement other measures. |
and | The system process consumes cpu time ( system time ). This value is too high. You need to check what causes the kernel to consume cpu a lot of resources and need to be optimized. Normally us + sy the reference value is80% |
id | Idle time (including io waiting time), generally speakingus+sy+id=100 |
of | When the waiting io time is too high, it means that the waiting is serious. This may be caused by a large number of random accesses to the disk, or it may be a bottleneck in the bandwidth of the disk.wa io |
top
top
You can see the process PID
. If you find an out-of-control process, you can find the corresponding process for troubleshooting ( kill
).
Command line interaction, real-time refresh of display data, q
and exit.
Based on what has been said above, everyone can basically understand the meaning. Here are a few that I have never seen before.
word | explain |
---|---|
VIRT | virtual memory usage Virtual memory usage |
RES | resident memory usage Actual memory usage |
SHR | shared memory Shared memory usage |
ps
ps aux --sort=rss
Sort in rss
positive order.
It’s top
almost the same as , so I won’t explain it in detail.
word | explain |
---|---|
%MEM | The percentage of physical memory occupied by the process |
VSZ | virtual memory size used |
RSS | The physical memory size used by the process, focus on this value |
pmap
pmap -x pid
Used to view the memory image information of the process and see where and how much memory is used.
word | explain |
---|---|
Address | The memory starting address of the file occupying memory |
Kbytes | Number of bytes occupied in memory |
RSS | Actual memory size occupied |
Dirty | dirty page size |
Mapping | The files occupying memory [anon] are allocated memory [stack] and the program stack |
The last total
is the total value of statistics. Usually when you want to see the memory consumption of this program, you can just look at the last line, as follows:
while true; do pmap -x pid | tail -1; sleep 1; done
Summarize
The above tools are all linux
built-in, and of course there are many more advanced tools. In actual work, just choose what you use and use it easily. Basically, you can locate the problem if you use it smoothly. What I usually use:
- Only view the memory usage of the system:
free -h
, very intuitive. - Analyze process memory usage:
top
/ps
are all good. - In-depth analysis:
pmap
Of course, you can also install an operation and maintenance management panel on the server. There are currently many open source projects, and it is more convenient to view them directly through the graphical interface.
All tools are used to solve problems. Don’t use tools just for the sake of using them. This article records the commonly used tools so that you can review them later. You can first have a rough idea of what the tools can do and when you really need to use them. Just learn more about how to use it.
reference
- Linux memory management (detailed explanation)
- Master Linux performance analysis - memory in one article
- How many methods do you know about Linux memory usage analysis?
- Super fully organized! Summary of Linux performance analysis tools
- Linux memory mechanism Swap mechanism and how to manually release swap and memory
- Buff/cache memory in Linux
- Reasons and solutions for excessive Linux buffer/cache memory usage