Home>Article>Web Front-end> Why do you need performance monitoring? Let’s talk about Node.js performance monitoring
Why is performance monitoring needed? This article will take you throughNode.jsperformance monitoring. I hope it will be helpful to you!
NodeAs a runtime (Runtime) of Javascript on the server side, It greatly enriches the application scenarios of Javascript.
But Node.js Runtime itself is a black box. Wecannot perceivethe status of the runtime, and it isdifficult to reproduceonline problems.
ThereforePerformance monitoringis the cornerstone of the "normal operation" of Node.js applications. Not only can various runtime indicators be monitored at any time, but it can also help troubleshoot abnormal scenario problems.
Performance monitoring can be divided into two parts:
Collection and display of performance indicators
Capture and analysis of performance data
From the above figure you can see the advantages and disadvantages of the three current mainstream Node.js performance monitoring solutions. The following is a brief introduction to the composition of these three solutions:
Prometheus
AliNode
alinode is an extended runtime compatible with official nodejs, providing some additional features:
agenthubis a resident process used to collect performance indicators and report
The whole system forms a closed loop from monitoring, display, snapshot, and analysis. The access is convenient and simple, but there are still risks in expanding the runtime
process.cpuUsage()you can obtain the CPU consumption data of the current process. The unit of the return value is microseconds
##Pass
process.memoryUsage()You can get the memory allocation data of the current process. The unit of the return value is bytes
#
As can be seen from the above figure,rss
includes code segment (Code Segment
), stack memory (Stack
), and heap memory (Heap
)
can be obtained from v8 throughv8.getHeapStatistics()
andv8.getHeapSpaceStatistics()
Analysis data of heap memory and heap space. The following figure shows the distribution of heap memory composition of v8:
The heap memory space is first divided into spaces, and the space is divided into Page, memory is paged according to 1MB alignment.
New Space: New generation space, used to store some object data with a relatively short life cycle, divided into two spaces (space type issemi space
):from space
,to space
Old Space: Old generation space, used to storeNew Space
Promoted objects
Code Space: Store v8 JIT compiled executable code
Map Space: Stores the pointer object of the hidden class pointed to by Object. The hidden class pointer is the object layout structure recorded by v8 at runtime and is used to quickly access object members
Large Object Space: used to store objects larger than 1MB that cannot be allocated to pages
v8 Garbage collection algorithms are divided into two categories:
Mark-Sweep-Compact
algorithm for object recycling in the old generationScavenge
algorithm for object recycling in the new generationPremise:New space
is divided into two object spaces:from
andto
Trigger timing: whenNew space
space Full
Steps:
Infrom space
, perform a breadth-first traversal
Discover The surviving (reachable) object
Old space
to space
中When the copy ends, there are only surviving objects into space
, andfrom space
is cleared.
from spaceand
to space, and start the next round
Scavenge
Suitable for objects with frequent recycling and small memory. It is a typical space-for-time strategy. The disadvantage is that twice as much space is wasted
Old spaceis full
(explicit stack), and these objects are marked gray
popand is marked black
Go to
marking queue, and repeat
Old space
, so that it can be cleared out The space is continuous and completeStop-The-World)
Default limit: 32M for 64-bit system, 16M for 32-bit system
Default limit: The 64-bit system is 1400M, and the 32-bit system is 700M
nodeprovides two parameters for adjusting the upper limit of the space of the new and old generations
: Set the maximum value of
New Space
: Set the maximum value of
Old Space
nodeis also provided Three ways to view GC logs:
: One line of log briefly describes the time, type, heap size changes and causes of each GC
: Display the detailed status of each V8 heap space after each GC
: Detailed key-value pair information of each GC , including GC type, pause time, memory changes, etc.
v8-gc-log-parser## developed by the AliNode team.
#Snapshot Toolof the running program, Can be used to analyze memory consumption and changes
Generation methodfile:
##Use
v8-profiler-nextAnalysis method
The default view is theSummary
view. Here we want to focus on the two rightmost columns:Shallow Size
andRetained Size
Shallow Size
: Indicates the size of the object itself allocated in v8 heap memoryRetained Size
: Indicates theShallow Size# of all referenced objects of the object ##sum
Retained Sizeis found to be particularly large, there may be a memory leak inside the object, and you can further expand to locate the problem
ComparisonThe view is used to compare and analyze heap snapshots of two different periods. The
Deltacolumn can be used to filter out the objects with the largest memory changes
CPUof the running program can be used to analyze the CPU time consumption and proportion
.cpuprofilefiles:
.cpuprofilefile can be displayed in
Javascript Profilerof the Chrome devtools toolbar (not in the default tab, you need to open it in More on the right side of the toolbar). After selecting the upload file, the results will be displayed. As shown below:
Heavyview. Here we see two columns:
Self Timeand
Total Time
: Represents the execution time of this function itself (excluding other calls)
: Represents the total execution time of this function (including other calling functions)
Total Timeand
Self Timeis found to be large , this function may be time-consuming and CPU-intensive calculations, you can also conduct further troubleshooting
.coreThree methods for files:
Open kernel limit
Add this parameter when node starts, you can generate a core file when an uncaught exception occurs in the application
Manually generate the core file
.corefile, you can use mdb, gdb, lldb and other tools to analyze and diagnose the cause of the actual process crash
heapsnapshot, we can analyze and find out that there is a
newThingobject that has always maintained a relatively large memory
unusedmethod is not called,
newThingThe object is referenced from
theThing, causing it to always exist in the execution context of
replaceThingand has not been released. This is a typical case of memory leak caused by closure
So in In the above situations, you must carefully consider whether the object will be automatically recycled in the memory. If it will not be automatically recycled, you need to manually recycle it, such as manually setting the object tonull
and removing the timing. Device, unbinding event monitoring, etc.
So far, this article has given a detailed introduction to the entire Node.js performance monitoring system.
First of all, it introduces the problems solved by performance monitoring, its components and a comparison of the advantages and disadvantages of mainstream solutions.
Then, the two major performance indicators and snapshot tools are introduced in detail.
Finally, reproduce a simple from observation, analysis and troubleshooting memory leak cases, and summarizes common memory leak situations and solutions.
I hope this article can help everyone understand the entire Node.js performance monitoring system.
For more node-related knowledge, please visit:nodejs tutorial!
The above is the detailed content of Why do you need performance monitoring? Let’s talk about Node.js performance monitoring. For more information, please follow other related articles on the PHP Chinese website!