Detecting memory leaks in nodejs

后端 未结 7 1471
情话喂你
情话喂你 2020-12-23 17:14

Are there some tools for detecting memory leaks in nodejs? And tell me about your experience in testing nodejs applications.

相关标签:
7条回答
  • 2020-12-23 17:15

    In tracking down a memory leak I tried the above node-inspector.
    As of April 2012 it was not updated to work with the contemporary node release v0.6.12 As such I found: https://github.com/c4milo/node-webkit-agent.
    It was able to show heap snapshots for the newer V8 engine which wasn't supported by node-inspector. In short order I was able to detect the leaking module (in may case loggly), I hope you have similar success!

    0 讨论(0)
  • 2020-12-23 17:18

    I use Chrome dev tools and heapsnapshot file directly, not node-inspector or node-webkit-agent.

    require() the heapdump module.

    Send usr2 signal to the running nodejs process to get heapsnapshot file.

    Load the heapsnapshot file on the profiles tab of Chrome dev tools.

    0 讨论(0)
  • 2020-12-23 17:23

    I could also recommend following sources:

    1. following episodes of NodeUp, where profiling and memory leaks detection tools are discussed:

      • http://nodeup.com/twentyone
      • http://nodeup.com/fortyfive
    2. This article - Tracking Down Memory Leaks in Node.js – A Node.JS Holiday Season, which basically aggregates all widely known modules and techniques to track down memory leaks

    3. For Mac OS - Instruments tool could be helpful to debug native (C++) modules. It is not so sophisticated as SmartOS tools, but very easy to setup.
    0 讨论(0)
  • 2020-12-23 17:25

    Method 1 -
    Node allows us to manually trigger Garbage Collection, and it is the first thing that we should do when trying confirm a memory leak. This can be accomplished by running Node with --expose-gc flag (i.e. node --expose-gc index.js). Once node is running in that mode, you can programmatically trigger a Garbage Collection at any time by calling global.gc() from your program.

    You can also check the amount of memory used by your process by calling process.memoryUsage().heapUsed.

    By manually triggering garbage collection and checking the heap used, you can determine if you in fact observing a memory leak in your program. Memory growth becomes even more evident as you do this test.

    Method 2 -

    3 Heap Dumps Method

    Node’s --inspect flag landed in node version 6. This feature lets you debug and inspect your node process from within Chrome’s DevTools.

    Simply start the application passing the --inspect flag:

    $ node --inspect index.js

    Follow the URL to the inspector and navigate to the memory tab.

    DevTools Memory Tab

    Here’s what we will do:

    Hit the application with autocannon -c 1 -d 60 http://localhost:PORT
    [ https://www.npmjs.com/package/autocannon ]
    Take a heap snapshot after roughly 10 seconds, and again after 30 seconds.

    Heap Snapshot Comparison
    The comparison view shows us what has happened between snapshots. You can see a huge number of objects have been created or not being collected by the GC.

    Please read these amazing articles to learn more.
    They have been of great help to find & fix a leak :-

    https://www.alexkras.com/simple-guide-to-finding-a-javascript-memory-leak-in-node-js/

    https://www.nearform.com/blog/self-detect-memory-leak-node/

    https://addyosmani.com/blog/taming-the-unicorn-easing-javascript-memory-profiling-in-devtools/

    0 讨论(0)
  • 2020-12-23 17:27

    The following tool should be useful for spotting memory leaks:

    node-inspector

    And there's also a tutorial to help you find memory leaks here:

    https://github.com/felixge/node-memory-leak-tutorial

    0 讨论(0)
  • 2020-12-23 17:39

    One good thing is https://github.com/bnoordhuis/node-heapdump very simple and you can view results in Chrome (because of same V8 engine in browser javascript and nodejs) and compare object sizes in memory at every moment you want.

    And one more tip how to detect memory leak "manualy" or help to prevent it, is force garbage collector in some places of your code and see what happens.

    Start your application with "node --expose-gc file.js" and anywhere in code you can use function gc(); to call garbage collection.

    0 讨论(0)
提交回复
热议问题