Is there a successful method to destroy Buffer instances and deallocate the memory used in NodeJS Canvas?

2.6k views Asked by At

Tried numerous ways to force the GC to cleanup the memory without success. On top of that:

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

The problem occurs in this routine:

 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

There are 1 answers

0
Ardencod3 On

I got 2 gig of memory after loop created 150 image objects. even assign null to all of it after render process finish still gave me same result of memory leak. so I dig deep to image.cc and found that the solution is simple. Just re assign source of image to null, image object will clean itself then I got my memory back :) (it might trigger onerror event because image load with nothing)

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

  //do anything you want ...

  //clean it by change src to null
  img.src = null;
};
img.src = data;