Memory analysis of Linux performance analysis (introduction to the use of free, vmstat, top, ps, pmap and other tools)

introduction

When I was a student, I often heard teachers and classmates talk about linuxthe 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 linuxhow to analyze memory in the system.

Introduction to common memory analysis tools

/proc/meminfo

cat /proc/meminfo

Insert image description here

word explain
MemTotal The total system memory is usually smaller than the hardware memory because the system BIOSmemory, kernel, etc. also occupy a certain amount of memory.
MemFree System free memory, physical memory remaining.
MemAvailable The difference between application available memory and MemFreeis: MemFreeit is the remaining space of the memory stick, which is at the system level. And MemAvailableis at the application level. Although some memory in the system is used, part of it can be recycled, such as: , Buffersetc. CachedWhen 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.

bufferUsed as ioa cache for device writes. And cacheis used as ioa read cache for the device. The devices here iomainly 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

free

word explain
Mem row (second row) memory usage
Swap row (third row) swap space usage. You can think of it as windowsvirtual 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 bufferand 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 3The 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+Cto exit.

Insert image description here

word explain
r Procs(Process), the number of tasks waiting to be executed, that is, how many processes are waiting to cpuexecute tasks, can be used for analysis cpu. Generally, this value will not be greater than cputhe number. If it exceeds, cpua 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 buffbuffer reads and writes to the block device
cache Used cachesize, 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 ioProfiling, blocks read per second (read from disk)
bo ioProfiling, 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 cputime ( 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 cputime ( system time). This value is too high. You need to check what causes the kernel to consume cpua lot of resources and need to be optimized. Normally us + sythe reference value is80%
id Idle time (including iowaiting time), generally speakingus+sy+id=100
of When the waiting iotime 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.waio

top

topYou 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, qand exit.

Insert image description here

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 usageVirtual memory usage
RES resident memory usageActual memory usage
SHR shared memoryShared memory usage

ps

ps aux --sort=rssSort in rsspositive order.

Insert image description here

It’s topalmost 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 pidUsed to view the memory image information of the process and see where and how much memory is used.

Insert image description here
Insert image description here

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 totalis 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

Insert image description here

Summarize

The above tools are all linuxbuilt-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/ psare 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

Guess you like

Origin blog.csdn.net/DisMisPres/article/details/132418391