E
E
Egor Developerov2015-10-28 17:00:34
Node.js
Egor Developerov, 2015-10-28 17:00:34

Memory leak when using Sequelize, ORM solution for NodeJS - what to do?

Good afternoon, I recently connected Sequelize following the example from https://github.com/sequelize/express-example. With the only difference that I give all models for export, and do not shove them into one object. Something like:

fs.readdirSync(__dirname+"/../orm").forEach(function(model) {
            if (model.substr(-3) == ".js")
                module.exports[model] = connections[db].import('../orm/' + model);
        }
    );

During a test run, to insert ~ 4000 lines, NodeJs starts eating memory and does not give back. Upon reaching 500 MB, it completely refuses to cooperate.
Where could be the problem? All following the example from github`a
PS To start using the PM2 module

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pomeo, 2015-10-30
@spacecucumber

There is no leak, the node has a lazy collector that starts working after it takes 1.5GB of RAM. Play around with the keys node --max_old_space_size=256

T
Timur Shemsedinov, 2015-10-28
@MarcusAurelius

This is because the model gets into require.cache and even under different names. And most likely you do this several times, not only at startup, and clog the cache, you need to either deliver memory or reduce the volume of the model or load it in parts and then delete it from the cache via delete require.cache[require.resolve('moduleName')] or not to shove into the cache, but into their data structures.

E
Egor Developerov, 2015-10-29
@spacecucumber

router.get('/test', hutils.authChecker, function(req, res, next) {
    Project.findById(1,{ include : [Player]}).then(function(project) {
        return Promise.denodeify(async.map)(project.Players, function(player, callback) {
            Player.create({
                project_id : 1,
                name       : 'iter_'+Math.floor(Math.random() * 1000000)+Math.floor(Math.random() * 1000000)
            }).then(function(gamer) {
                callback(null, gamer)
            });
        });
    }).then(function(plrs) {
        return Promise.denodeify(async.map)(plrs, function(guy, callback) {
            guy.update({name : sqlRequest+'zzzzz'+Math.random()}).then(function(number) {
                callback(null, number);
            });
        });
    }).then(function(numbers) {
        return Player.findAll({where : {name : {$like : '%zzzzz%'}}});
    }).then(function(zets) {
        return Promise.denodeify(async.map)(zets, function(zet, callback) {
            zet.destroy().then(function(number) {
                callback(null, number);
            });
        });
    }).catch(function(err) {
        next(err);
    });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question