In this lesson, I introduce a memory leak into our node. js application and show how to identify it using the FORMIDABL E Nodejs-dashboard. Once identified, we'll add garbage collection stats to the error console allowing us to correlate garbage collection wit h The increased memory usage.
Install:
NPM i-d Gc-profiler Nodejs-dashboard
Let Express = require ('Express'); let router=Express. Router (); Let Faker= Require ('Faker'); let words=[];let Profiler= Require ('Gc-profiler'); Profiler.on ( 'gc', (info) = = {console.error (info);}); Router.Get('/', Function (req, res, next) {Let num= Math.floor (Math.random () * +) +1; Let Searchterm=faker.lorem.words (num); Let arr= Searchterm.split (' '); Arr.foreach (Word={ Words.push (word);//Cause memory leak }); Console.log (' Generating ${num} words '); Res.send (searchterm);}); Module.exports= Router;
In the code, we can use the ' gc-profiler ' to monitor the GB collection in order to see the memory leak in dashboard.
[node. js] Identify memory leaks with Nodejs-dashboard