简体   繁体   English

setInterval JavaScript内存泄漏

[英]setInterval javascript memory leak

I can't figure out why the memory is increasing and it stays there each time I run this code: 我不知道为什么内存增加了,并且每次运行此代码时它都停留在那儿:

easingFunction = function (t, b, c, d) {
    if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
    return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
}
processFrame = function () {
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent += 4;
        if (tile.percent > 0) {
            var TH = Math.max(0, Math.min(TILE_HEIGHT, targetObj.height - tile.imageY));
            var TW = Math.max(0, Math.min(TILE_WIDTH, targetObj.width - tile.imageX));
            var SW, SH, SX, SY, amount;
            draw.save();
            draw.translate(tile.imageX, tile.imageY);
            if (direction == "tb" || direction == "bt") {
                amount = easingFunction(tile.percent, 0, TW, 100);
                SW = Math.min(TW, amount);
                SH = TH;
                SX = 0;
                SY = 0;
            } else {
                amount = easingFunction(tile.percent, 0, TH, 100);
                SW = TW;
                SH = Math.min(TH, amount);
                SX = 0;
                SY = 0;
            }
            draw.drawImage(copycanvas, tile.imageX, tile.imageY, SW, SH, SX, SY, SW, SH);
            draw.restore();
        }
    }
    var ok = true;
    for (i = 0; i < tiles.length; i++) {
        if (tiles[i].percent < 100) {
            ok = false;
            break;
        }
    }
    if (ok) {
        clearInterval(interval);
        showComplete();
    }
};
this.show = function (target, hideTarget) {
    createTiles();
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent = 0 - i * 10;
    }
}
var intervalDelay = (config.duration * 1000) / (tiles.length * 3 + 25);
interval = setInterval(function () {
    processFrame();
}, intervalDelay);
};

function Tile() {
    this.imageX = 0;
    this.imageY = 0;
    this.percent = 0;
};
};

I left out some unimportant code. 我遗漏了一些不重要的代码。 The ideea is that I call externally the show() function. 想法是我在外部调用show()函数。 The setInterval is initialized and runs processFrame() about 100 times. 初始化setInterval并运行processFrame()约100次。

I've tried to leave some code outside from processFrame, and I got to : 我试图将一些代码留在processFrame之外,然后我去了:

processFrame = function () {
    for (var i = 0; i < tiles.length; i++) {
        var tile = tiles[i];
        tile.percent += 4;
    }
    var ok = true;
    for (i = 0; i < tiles.length; i++) {
        if (tiles[i].percent < 100) {
            ok = false;
            break;
        }
    }
    if (ok) {
        clearInterval(interval);
        showComplete();
    }
};

But the memory still increases. 但是内存仍然增加。

Try validating your code with JSLint. 尝试使用JSLint验证您的代码。 http://www.jslint.com/ http://www.jslint.com/

Right now your adding easingFunction & processFrame to the Global object (which isn't a good thing). 现在,您将easingFunction和processFrame添加到Global对象(这不是一件好事)。 Not that this is the cause of the problem, but I've found that mismanagement of my objects is the usual cause of memory leaks. 并不是这是问题的原因,但是我发现对象管理不当是内存泄漏的常见原因。

You'll want to do something like: 您将需要执行以下操作:

var MyObject = {}; 

MyObject.easingFunction = function(){};
MyObject.processFrame = function(){};

In short make sure you declare all objects with var before using them. 简而言之,请确保在使用所有对象之前先声明它们为var

I found the problem. 我发现了问题。 I was continuously redrawing the canvas. 我一直在不断重画画布。 To resolve this problem I had to erase the canvas each time before modifying it. 为了解决这个问题,我每次都必须先删除画布,然后再对其进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM