Skip to content

Instantly share code, notes, and snippets.

@doron2402
Created May 4, 2017 19:12
Show Gist options
  • Save doron2402/2bdb0765a6e0169b867ca7cbc0d81482 to your computer and use it in GitHub Desktop.
Save doron2402/2bdb0765a6e0169b867ca7cbc0d81482 to your computer and use it in GitHub Desktop.
Using memwatch-next in order to detect memory leak node 6.10.x
'use strict';
const Memwatch = require('memwatch-next');
const Util = require('util');
if (Config.env === 'production') {
/**
* Check for memory leaks
*/
let hd = null;
Memwatch.on('leak', (info) => {
console.log('memwatch::leak');
console.error(info);
if (!hd) {
hd = new Memwatch.HeapDiff();
}
else {
const diff = hd.end();
console.error(Util.inspect(diff, true, null));
trace.report('memwatch::leak', {
HeapDiff: hd
});
hd = null;
}
});
Memwatch.on('stats', (stats) => {
console.log('memwatch::stats');
console.error(Util.inspect(stats, true, null));
trace.report('memwatch::stats', {
Stats: stats
});
});
}
@iGitScor
Copy link

trace is not defined, do you use an external lib for the trace ?

@manisuec
Copy link

@iGitScor: Add these lines after installing npm install @risingstack/trace --save
const trace = require('@risingstack/trace');

@ishmam999
Copy link

ishmam999 commented Mar 11, 2020

@manisuec: @risingstack/trace package has stopped supporting Node. Can you suggest any good , stable package instead of this one ?

@manisuec
Copy link

@ishmam999 What is the purpose for which you were using '@risingstack/trace'?

@ishmam999
Copy link

ishmam999 commented Mar 11, 2020

@manisuec for checking the memory leak. As the code does so. I am also looking for the memory leak in my app. But can't find any.

@manisuec
Copy link

@ishmam999: trace is mainly for reporting. You can remove its dependency from the code. memwatch stats is json object; you can have your own reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment