开发者

Will this JavaScript script result in a memory leak?

This question depends my previous ques开发者_如何转开发tion: Browser crashes after 10-15 mins On that question I got answer saying my code is doing memory leak. So I'm trying to find the point where memory leak is going on.

Will this script will result in memory leak?

var j = function (i, q, r, a) {
        return function (s) {
            var p = r.annotation;
            if (p.hasOwnProperty(i)) {
                p[i](p, r, a.dygraph_, s)
            } else {
                if (a.dygraph_.attr_(q)) {
                    a.dygraph_.attr_(q)(p, r, a.dygraph_, s)
                }
            }
        }
    };

If yes then is there any solution that I can do to prevent memory leak?


It's not apparent if there are memory leaks, but if this were my code, I would change it like so (not counting the bad variable naming):

var j = function (i, q, r, a) {
    var p = r.annotation,
        dygraph = a.dygraph_;

    return function (s) {
        if (p.hasOwnProperty(i)) {
            p[i](p, r, dygraph, s);
        } else {
            var aqExpression = dygraph.attr_(q);

            if (aqExpression) {
                aqExpression(p, r, dygraph, s);
            }
        }
    };
};


It doesn't look like you're leaking memory with that code.

Leaking memory usually happens when DOM elements are involved.

I wrote this guide to memory leak patterns in JavaScript and how to debug them: http://www.vladalexandruionescu.com/2012/08/javascript-memory-leaks.html. Hope you'll find it useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜