Why is performance monitoring needed? This article will take you through Node.js performance 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. We cannot perceive the status of the runtime, and it is difficult to reproduce online problems.
Therefore Performance monitoring is 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:
agenthub is 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 through v8.getHeapStatistics()
and v8.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 is semi space
): from space
, to space
Old Space: Old generation space, used to store New 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 generation Scavenge
algorithm for object recycling in the new generation Premise: New space
is divided into two object spaces: from
and to
Trigger timing: when New space
space Full
Steps:
In from space
, perform a breadth-first traversal
Discover The surviving (reachable) object
Old space
to space
中When the copy ends, there are only surviving objects in to space
, and from space
is cleared.
from space and
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 space is full
(explicit stack), and these objects are marked gray
pop and is marked black
Go to
marking queue, and repeat
Old space
, so that it can be cleared out The space is continuous and complete Stop-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
node provides 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
node is 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 method file:
##Use
v8-profiler-nextAnalysis method
The default view is the Summary
view. Here we want to focus on the two rightmost columns: Shallow Size
and Retained Size
Shallow Size
: Indicates the size of the object itself allocated in v8 heap memory Retained Size
: Indicates the Shallow Size# of all referenced objects of the object ##sum
Retained Size is 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
Delta column can be used to filter out the objects with the largest memory changes
CPU of the running program can be used to analyze the CPU time consumption and proportion
.cpuprofile files:
.cpuprofile file can be displayed in
Javascript Profiler of 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:
Heavy view. Here we see two columns:
Self Time and
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 Time and
Self Time is found to be large , this function may be time-consuming and CPU-intensive calculations, you can also conduct further troubleshooting
.core Three 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
.core file, 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
newThing object that has always maintained a relatively large memory
unused method is not called,
newThingThe object is referenced from
theThing, causing it to always exist in the execution context of
replaceThing and 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 to null
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!