JavaScript leaking memory (Node.js/Restify/MongoDB) JavaScript leaking memory (Node.js/Restify/MongoDB) mongoose mongoose

JavaScript leaking memory (Node.js/Restify/MongoDB)


Might be a bug in getters, I got it when using virtuals or getters for mongoose schema https://github.com/LearnBoost/mongoose/issues/1565


It's actually normal to only see string and arrays, as most programs are largely based on them. The profiler that allow sorting by total object count are therefore not of much use as they many times give the same results for many different programs.

A better way to use the memory profiling of chrome is to take one snapshot for example after one user calls an API, and then a second heap snapshot after a second user called the API.

The profiler gives the possibility to compare two snapshots and see what is the difference between one and the other (see this tutorial), this will help understand why the memory grew in an unexpected way.

Objects are retained in memory because there is still a reference to them that prevents the object from being garbage collected.

So another way to try to use the profiler to find memory leaks is to look for an object that you believe should not be there and see what is it's retaining paths, and see if there are any unexpected paths.


Not sure whether this helps, but could you try to remove unnecessary returns?

api/message.js

        // Send a message using external API        messageController.sendMessage(token, account.email, function() {            res.send(201, {});            next(); // remove 'return'        });

controllers/accounts.js

module.exports = function(db) {    // Gets account by a token    function getAccount(token, callback) {        var ObjectID = require('mongodb').ObjectID;        var collection = db.collection('accounts');        collection.findOne({            token: token        }, function(error, account) {            if (error) {                callback(error); // remove 'return'            } else if (account) {                callback('', account); // remove 'return'            } else {                callback('Account not found'); // remove 'return'            }        });    }    return { // I guess you missed to copy this onto the question.        getAccount: getAccount    };};

controllers/messages.js

            // Do POST            client.post('/external_api', values, function(err, req, res, obj) {                callback(); // remove 'return'            });