2014-12-02 742 views
0

尝试了很多方法来强制GC清除内存而没有成功。 最重要的是:是否有成功的方法销毁Buffer实例并释放NodeJS Canvas中使用的内存?

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); 
    }     
}); 
+1

你是什么意思“没有成功”?你能告诉更多关于你如何测试的内容吗? – 2014-12-02 15:29:08

+0

我有一个小功能,可以将来自URL的图像加载到缓冲区中。在画布上绘制图像对象后,我尝试销毁图像对象并释放缓冲区内存。 – swogger 2014-12-02 15:33:20

+0

你如何跟踪内存堆? – Shmiddty 2014-12-02 15:40:05

回答

0

我得到2演出的存储回路150个中创建的图像对象之后。甚至在渲染过程完成之后为它分配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; 
相关问题