View memory leakage in Node. js

Source: Internet
Author: User

Enternode-memwatch we need a cross-platform debugging library. When our program may have memory leakage, it does not need to be told by the device and will help us find out where there is a leak. So we implemented node-memwatch. It provides us with three things: A 'leak 'event transmitter memwatch. on ('leak', function (info) {// look at info to find out about what might be leaking }); one 'status' event transmitter var memwatch = require ('memwatch '); memwatch. on ('stats', function (stats) {// do something with post-gc memory usage stats}); one heap memory partition classification var hd = new memwatch. heapDiff (); // your code here... var diff = hd. end (); in addition, it is useful during testing and can trigger the Garbage Collector function. Okay, there are four points in total. 1var stats = memwatch. gc (); memwatch. on ('stats ',...): Post-GC heap statistics node-memwatch can generate a memory usage sample with a complete garbage collection and memory compression before any JS object is allocated. (It uses the V8 post-gc hook, V8: AddGCEpilogueCallback, to collect heap usage information for each garbage collection trigger) Statistical data includes: usage_trend (usage trend) current_base (current base) estimated_base (expected base) num_full_gc (total garbage collection times) num_inc_gc (increased garbage collection times) heap_compactions (memory compression times) min (minimum) max (maximum) here is an example of how the data of an application with Memory leakage looks like. The following chart tracks memory usage over time. The crazy green line shows the content of the process. memoryUsage () report. The red line shows the current_base of the node_memwatch report. The lower-left box shows additional information. Note that the Incr GCs is very high. That means V8 is desperately trying to clean up the memory. Memwatch. on ('leak ',...): Heap distribution trend we define a simple detection algorithm to remind you that your application may have memory leakage. That is, if the memory is continuously allocated and not released after five consecutive GC operations, node-memwatch will issue a leak event. The specific event information format is clear and easy to read, like this: {start: Fri, 29 Jun 2012 14:12:13 GMT, end: Fri, 29 Jun 2012 14:12:33 GMT, growth: 67984, reason: 'heap growth over 5 consecutive GCs (20 s)-11.67 Mb/s '} memwatch. heapDiff (): Find the leak culprit. At last, node-memwatch can compare the names of objects on the stack with the allocated quantity of snapshots. The differences before and after the comparison can help find out the culprit that causes memory leakage. Var hd = new memwatch. heapDiff (); // Your code here... var diff = hd. end (); The comparison result is as follows: {"before": {"nodes": 11625, "size_bytes": 1869904, "size": "1.78 mb "}, "after": {"nodes": 21435, "size_bytes": 2119136, "size": "2.02 mb"}, "change": {"size_bytes": 249232, "size": "243.39 kb", "freed_nodes": 197, "allocated_nodes": 10007, "details": [{"what": "Array", "size_bytes ": 66688, "size": "65. 13 kb "," + ": 4,"-": 78 },{" what ":" Code "," size_bytes ":-55296," size ": "-54 kb", "+": 1, "-": 57 },{ "what": "LeakingClass", "size_bytes": 239952, "size ": "234.33 kb", "+": 9998, "-": 0 },{ "what": "String", "size_bytes":-2120, "size ": "-2.07 kb", "+": 3, "-": 62}]} The HeapDiff method performs a complete garbage collection before sampling data, in this way, the data will not be filled with too much useless information. Memwatch event processing ignores the garbage collection events triggered by HeapDiff. Therefore, you can safely call the HeapDiff method in the listener callback function of the stats event. In the middle, we added the heap memory object allocation ranking: How to Do node-memwatch in the next step: accurate memory usage tracking suspected Memory leakage notification heap difference comparison this is cross-platform and does not require any additional devices

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.