Shih-Min Lee's Personal website

dating, chating, food, games, search

Follow me on GitHub

Checking memory leaks in node.js applications

Checking Memory leaks:

When your application gets larger it will be more difficult to monitor the memory leaks. Luckily we have a lot of tools to assist us finding the cause of the memory leaks.

Things that I (try to) do

  • Chrome dev tools to monitor client side memory usage
  • server side Heapdump using node-heapdump module to take memory snapshots

Test your memory leak on the client side

first write a simple script that causes memory leak as follows

<script>
  var array = []
  var leak = function(){
      if (!array || !array.length) array = new Array(10000);
      else array = new Array(10000 + array.length);
  }
  var release = function(){
      array = null;
  }
</script>
<button onclick='leak()'>leak</button>
<button onclick='release()'>release</button>

As you click on the leak button you will see array gets larger and larger. It takes up a lot of memory but you’re not actually using anything with it. You can record this memory allocation by doing

Chrome dev tools -> Profiles -> Record heap allocations -> Start

When you cilck on the leak button you will see some blue bars and gray bars on the screen. The blue bars are memories in use and the gray bars are released memory. It’s apparent the array is getting larger and the blue bar is getting larger too.

once you click on the release button. The memory will be released and the blue bar will disappear. That means youh have release the unused memory… yay.

Also you can take heap snapshots and compare the memory allocation between 2 snapshots and possibly locate the cause of the memory leak.

Use the flame chart to check your profile your CPU usage.

Basically what you do is look for large spikes in the chart. It means the function call is taking so much CPU time to execute and unusual things might happen there.

When you hover your cursor on the chart you will see the function name and some attributes. Total time is this function execution time in this initialization. Aggregated total time is the total time this function is executed in the lifecycle of the recording.

Test your memory leak on the server side

we can use the node-heapdump module to monitor the memory usage of the node.js application. You can open the recorded file using Chrome dev tools similar to the steps described above.

var heapdump = require('heapdump')
...
heapdump.writeSnapshot()

and then you can open chrome dev tools to load this profile.

Some knowledges about the heap dump profile

in situations you will see some highlighted nodes. Some of them in yellow and some of them in red. Yellow nodes are DOM nodes that are referenced to the source code and red nodes are not. And the size it will free up when you destroy the entire node is called ‘retained size’.

Common errors

  • Error: spawn ENOENT
  • FATAL ERROR: JS Allocation failed - process out of memory

references:

22 Jul 2015