2

尝试了许多方法来强制 GC 清理内存但没有成功。最重要的是:

buf.remove(); // does not exist   
delete buf; // does not deallocate the memory 
buf = null; // removing references - no result

此例程中出现问题:

 function loadImage(url, finish){
     var Image = Canvas.Image;
     request.get({url:url, encoding:null}, responseImage);
     function responseImage(err,res,body) {
         if(err){
             return finish();
         }
         var image = new Image();
         image.onerror = function(e) {
             finish();
         };
         image.onload = function(){
             finish(image);
         };
         image.src = new Buffer(body, 'binary');
    }
}

loadImage("http://...", function(image){
    if(image){    
        canvasContext.drawImage(image,0,0,100,100 );
    }                
});
4

1 回答 1

1

循环创建 150 个图像对象后,我获得了 2 gig 的内存。即使在渲染过程完成后将所有内容分配给 null 仍然给了我相同的内存泄漏结果。所以我深入研究了image.cc,发现解决方案很简单。只需将图像源重新分配给null,图像对象将自行清理然后我恢复记忆:)(它可能会触发onerror事件,因为图像加载时什么都没有)

var img = new Image;
img.onload = function() {

  //do anything you want ...

  //clean it by change src to null
  img.src = null;
};
img.src = data;
于 2016-08-25T07:26:49.350 回答