html5画布内存泄漏

时间:2015-06-10 09:25:43

标签: javascript html5 canvas html5-canvas

我正在开发基于相机的SPA。当我试图调整图像大小时,会导致内存泄漏。

,不执行调整大小

memory snapshot at beginning

首先执行调整大小

enter image description here

第二次执行调整大小

enter image description here

在我刷新之前,记忆永远不会消失。

奇怪的是,相同的应用http://shitu.baidu.com确实释放了内存。 这是我的调整大小功能。

var useRightPen = function (img, pre_img_wid, pre_img_hei, aft_img_wid, aft_img_hei, callback) {
    var canvas = document.createElement('canvas');
    var ctx=canvas.getContext('2d');
    canvas.setAttribute("height", aft_img_hei);
    canvas.setAttribute("width", aft_img_wid);

    var orientation = 1;
    if (!!(/http/.test(img.src))) {
          //memory keeps the same before the drawimage
        canvas.getContext('2d').drawImage(img, 0, 0, aft_img_wid, aft_img_hei);
         //memory goes up after the draimage
        callback(canvas.toDataURL('image/jpeg'), aft_img_wid, aft_img_hei);
    }
    canvas = null;
}

感谢所有人。

---更新 - 06-11--2015

我制作了一个fildde http://jsfiddle.net/onTheRoad/dc17guba/

演示将在每次选择后重置图像src.Memory在路上走高。 enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

好吧,如果永远不会释放内存,那就是内存泄漏。

我也在使用动画画布元素。

使用requestAnimationFrame()极大地减少了为canvas元素设置动画所需的内存量,但我仍然无法弄清楚如何释放内存。