Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js memory leaks?

I have some code below that I'm using in my Express.js app to centralize some acl logic. If the function returns true or false explicitly the middleware can handle the next call. But if it doesn't return it's up to the authorize logic to execute next() whenever it's finished doing it's thing.

To avoid having to write out the error data, I want to just pass in an error() function that can be called, which just calls the next function internally.

Someone told me that this can lead to some kind of memory leaks since the next function is in it's own closure and referencing it from outside. I see similar techniques being used in a lot of examples online, but I'm still quite new to Node.js so wondering if there is any truth to this?

this.router.use(function (req, res, next) {
    var err = {
            code: 403,
            exception: 'UnauthorizedException',
            data: {}
        },
        error = function () {
            next(err);
        },
        authorize = app.route.authorize(req, res, next, error);

    if (authorize === false) {
        next(err);
    }
    else if (authorize === true) {
        next();
    }
});

EDIT:Remove variables

this.router.use(function (req, res, next) {
    var authorize = app.route.authorize(req, res, next, function () {
        next({
            code: 403,
            exception: 'UnauthorizedException',
            data: {}
        });
    });

    if (authorize === false) {
        next({
            code: 403,
            exception: 'UnauthorizedException',
            data: {}
        });
    }
    else if (authorize === true) {
        next();
    }
});
like image 641
Rob Avatar asked Oct 05 '15 09:10

Rob


People also ask

How do I see memory leaks in Node js application?

Chrome DevTools is a great tool that can be used to diagnose memory leaks in Node. js applications via remote debugging. Other tools exist and they will give you the similar. This blog post relies on one of those different tools in order to give you a clear clear understanding of what is happening.

What are memory leaks in js?

What are memory leaks? In simple words, a memory leak is an allocated piece of memory that the JavaScript engine is unable to reclaim. The JavaScript engine allocates memory when you create objects and variables in your application, and it is smart enough to clear out the memory when you no longer need the objects.


1 Answers

When you set up the middleware, the .use() method is called there once, the anonymous handler/middleware is written to memory once, and it's that same middleware function that gets called for each new request.

The err variable is instantiated every time the middleware runs, and it's a different object. Had you put it outside and into the closure scope of .use(), it would then be the same object.

It is then passed to next and next is very likely another middleware function that gets instantiated once and stays the same in memory which persists and grabs onto its closure access.

But then, when the next function finishes running, the object that err points to would lose its references -- it should be garbage-collected.

like image 176
David Rosson Avatar answered Oct 12 '22 10:10

David Rosson