Fatal error hitting Node.js | FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - out of memory process

I wanted to know if there is a way to catch this error:

FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory

      

I tried:

process.on('uncaughtException', function(err){
        //do something
 })

      

But that didn't catch the error.

Any help would be greatly appreciated

PS This happens when generating MD5 hashes for a string of eighteen files and I use the md5 module like this:

for(i=0;i<array.length;i++){
    fs.readFile(array[i], function(err,buf){
        console.log(mdf(buf))
    })

}

      

+3


source to share


1 answer


You should avoid buffering entire files in memory. Compute the md5 hash chunk in one go. Example:



var fs = require('fs'),
    crypto = require('crypto');

var array = [ 'foo.txt' ];

array.forEach(function(filename) {
  var hasher = crypto.createHash('md5', { encoding: 'hex' });
  fs.createReadStream(filename).pipe(hasher).on('finish', function() {
    process.nextTick(function() {
      var md5sum = hasher.read();
      console.log(filename + ': ' + md5sum);
    });
  });
});

      

+1


source







All Articles