内存泄漏,同时绘制许多新的图像到画布 [英] memory leak while drawing many new images to canvas

查看:275
本文介绍了内存泄漏,同时绘制许多新的图像到画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个幻灯片放映,顺序和快速地显示图像,很像一个停止帧电影。



一旦显示图像,我就没有进一步使用它,并希望从内存中清除它,以释放新图像的空间。但是,在活动监视器中监视Google Chrome帮助程序时,我发现内存持续增加,直到浏览器崩溃。



我注意到一个chrome垃圾收集问题,作为一个bug提交,我想知道,如果也许我遇到这个? p>

否则,这里是一个例子,我试图基于此帖,Chrome会删除旧的图片数据。

  function ClearChunk()
{
imageSet1 = null; //垃圾收集吗?
imageSet1 = [[];
}

function LoadNewChunk()
{
for(i = start_of_chunk; i {
imageSet1 [i -start_of_chunk] = new Image();
imageSet1 [i-start_of_chunk] .src = img [i];
}
}

先清除,然后在后台加载同时显示另一个图像阵列。这似乎是一个好主意,但在我的机器上,它仍然稳步上升到大约3Gb和... Aw,Snap。



如何减少这种猖獗的内存消耗?



任何会话式或基于代码的反馈

$ p

解决方案

尝试只做一个新Image(),并重用它,而不是在循环中创建多个。这帮助我解决了同样的问题。


I have written a "slide show" that displays images sequentially and quickly, much like a stop-frame movie.

Once an image is displayed, I have no further use for it and would like to clear it from memory to free space for new images. However, while monitoring Google Chrome Helper in Activity Monitor, I see that the memory continues to increase steadily until the browser crashes.

I noticed a chrome garbage collection issue that was submitted as a bug and I'm wondering if maybe I'm experiencing this?

Otherwise, here is one example of a trick I tried based on this post to get Chrome to trash my old image data.

  function ClearChunk()
  {      
        imageSet1 = null; // garbage collect please?
        imageSet1 = [];
  }

  function LoadNewChunk()
  {
        for (i=start_of_chunk;i<end_of_chunk;i++)
        {
              imageSet1[i-start_of_chunk] = new Image();
              imageSet1[i-start_of_chunk].src = img[i]; 
        }
  }

This clears first and then loads in the background, all while another array of images are being displayed. It seemed like a good idea at the time, but on my machine it still climbs steadily to about 3Gb and... Aw, Snap.

How to mitigate this rampant memory consumption in the first place?

Any conversational or code-based feedback would be appreciated.

解决方案

Try doing only one 'new Image()', and reuse that, instead of creating many in the loop. This helped me solve the same issue.

这篇关于内存泄漏,同时绘制许多新的图像到画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆