Recently, long connection was used in the project. Yesterday, WE discussed a problem with the backend: to establish long connection between the front-end program and the backend. If two long connections of the same account will lead to mutual kicking, will two tabs open in one browser kick each other?
In the browser, a TAB is actually a process, and the stack and heap of each process are independent. Therefore, when establishing a long connection, only one object is allocated in the heap to call the network resources of the operating system for communication, and the two objects controlling the long connection in two independent processes can be regarded as independent. Opening two tabs in a browser is theoretically the same as logging in from two computers. Except, of course, for some mechanism that the browser might provide for interprocess communication.
This thought led me to be interested in understanding the internal storage of processes, so let’s take a node back-end process as an example. Before read his book large-scale Web services development technology mentioned, business background system design should focus on two bottlenecks, one is a reverse proxy server to request is sent to the CPU load, when the application server and application server to fetch the data from the database server, produce the load in the I/O requests. I/O access efficiency can be improved by appropriately increasing the cache of the database server. But what about troubleshooting as a standalone process?
Of course, the two parameters that can cause a process to crash or stagnate: memory usage and CPU usage. Memory is the main analysis of space, CPU is the main analysis of time. The main focus of this tutorial is memory usage.
When analyzing the memory, we mainly analyze the heap memory snapshot. V8 engine provides an internal interface to export JS objects directly from the heap for developers to analyze. We use the module heapdump, execute the following command to install:
npm install heapdump --save
Copy the code
Then add the following statement to the code to execute the module:
const heapdump = require('heapdump');
heapdump.writeSnapshot('./test' + '.heapsnapshot');
Copy the code
For the sake of analysis we wrote a Node service demo for Express,
var app = require('express') (); var http = require('http').Server(app);
var heapdump = require('heapdump');
var leakobjs = [];
function LeakClass(){
this.x = 1;
}
app.get('/'.function(req, res){
console.log('get /');
for(var i = 0; i < 1000; i++){
leakobjs.push(new LeakClass());
}
res.send('<h1>Hello world</h1>');
});
setInterval(function(){
heapdump.writeSnapshot('/' + Date.now() + '.heapsnapshot');
}, 3000);
http.listen(3000, function(){
console.log('listening on port 3000');
});
Copy the code
Leakobjs array and LeakClass will be added to the leakOBJs array when each request comes in. Since leakOBJs is in the global leakobjs array, the request will not be automatically released, causing a memory leak.
We have requested this service for about 10 times, and we can see that there are several HeapSnapshot files in the project directory. If we use the text editor to check, we can see that the content in the project directory is mainly a large JSON, and the main fields include Snapshot, Nodes and edges. Where node represents the points and edges represent the join points. The node_fields field in the snapshot mainly describes the meanings of the six digits of each node in nodes, and the edge_fields field also indicates the meanings of each edge in the edges field. The string field describes the names of the points and edges, including the “GC root” of the node.
A heap snapshot describes the relationship between points and the edges between them. So what does the memory heap have to do with the points and edges described here?
We can use the whole region in node as the GC root and node. From the whole region, if 2 and 3 objects can be accessed from the whole region, both 2 and 3 can access 4 objects. 3 can access 5,4 can access 6. In the left figure, if node 2 is removed, 4 can still be accessed from the GC and the node, so 2 is not the dominant node of 4. According to the algorithm in graph theory, the left reference graph is converted into a dominance tree to obtain the dominance relationship from GC to each node.
So what is it that we know about the dominance diagram? Retained Size: Shallow Size and Retained Size. Retained Size is the amount of memory required for the Retained object itself to be created. Retained Size is the amount of memory that can be freed by the object and its subordinate nodes when removed from the domination tree.
Since the runtime environment of javascript in Node and browser is V8, we put the heapSnapshot file we obtained previously into the browser for analysis: Open debugger mode in Chrome, select Heap Snapshot on the Memory TAB, click Load to upload the HeapSnapshot file, and see the dominance tree that the browser analyzed.
When the browser is done, we switch to Summary mode, and we can see that the dominance tree looks like this. Retained Size allows memory to be Retained and Retained in descending order. Across memory leaks, we can ensure that the Retained object Retained under the top node consumes a large amount of memory. Then step by step to determine the object of the memory leak, you can locate the memory leak. Observe that the objects in the three red boxes under closure are much larger than the other nodes:
If we open the app() object, we can see that the size of the _router object is also unusual.
The leakobjs array consumes too much memory, so the Retained Size of the parent node is too large:
The same result can be obtained if we continue analyzing the Routers () and ().
Thinking here, suddenly read the operating system before the book has “suddenly look back, that person is in the lights dim” feeling. May we have learned the theoretical knowledge if not in the work of application and thinking, are just dead knowledge points, after thinking and application, in order to live.
If you are interested in this article and want to pay attention to more technical articles, please pay attention to the author’s official account, more good articles for you to present ~