简体   繁体   English

使用getImageData时发生内存泄漏

[英]Memory leak when using getImageData

Hello all and thanks for your time. 大家好,谢谢您的宝贵时间。 I'm having an issue using getImageData. 我在使用getImageData时遇到问题。 Here is some example code. 这是一些示例代码。

function readImage()
{
    var test_image = new Image();
    test_image.src = "images/testImage.png";
    test_image.onload = function() 
    {
        var testCanvas = document.createElement( 'canvas' );
        if ( testCanvas.getContext ) 
        {
            testCanvas.width = test_image.width;
            testCanvas.height = test_image.height;
            var image_ctx = testCanvas.getContext( "2d" );
            image_ctx.drawImage( test_image, 0, 0 );
            var pix = image_ctx.getImageData( 0, 0, test_image.width, test_image.height ).data;

            pix = null;
            image_ctx = null;
            test_image = null;
            testCanvas = null;
        }
    }
}

updateMDPixelArray = setInterval(
    function()
    {
        readImage();
    }, 1000
);

When I run this code in Chrome and open the Chrome task manager( Shift + Esc ). 当我在Chrome中运行此代码并打开Chrome任务管理器(Shift + Esc)时。 It shows that both the browser tab and GPU memory increase constantly. 它表明浏览器选项卡和GPU内存都在不断增加。 The same happens in Firefox. 在Firefox中也是如此。 From what I know about javaScript, objects should be garbage collected when they are no longer being referenced. 根据我对javaScript的了解,当不再引用对象时,应该对其进行垃圾回收。 All variables are local. 所有变量都是局部的。 I don't think it's helping but just to be on the safe side I set all the variables to null. 我认为这没有帮助,但是为了安全起见,我将所有变量都设置为null。 There is no leak if I remove the line that uses getImageData. 如果删除使用getImageData的行,则不会泄漏。 SO I guess there must be a reference that I'm failing to clean up. 因此,我想一定有一个我无法清理的参考。 Right? 对?

Chrome's task manager probably isn't the most reliable tool you can use. Chrome的任务管理器可能不是您可以使用的最可靠的工具。 They even integrated a message "Note: This page will show memory use for all running browsers, not just Chrome. (Bug: We seriously overcount our own memory usage: Issue 25454.)" 他们甚至集成了一条消息“注意:此页面将显示所有正在运行的浏览器的内存使用情况,而不仅仅是Chrome。(错误:我们严重夸大了自己的内存使用情况:问题25454。”)

Try using the Chrome dev tools (F12 on Windows; cmd+opt+i on mac). 尝试使用Chrome开发者工具 (在Windows上为F12;在Mac上为cmd + opt + i)。 Open the timeline tab and record for ~10-20 seconds. 打开时间轴标签,并记录约10-20秒。 It should help you narrow down any leaks. 它应该可以帮助您缩小泄漏范围。 FWIW, when I do this I do not observe a leak. FWIW,当我这样做时,我没有发现泄漏。

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

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