Share On Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit
Question

process out of memory error on node.js

Tags: memory md5 node.js
Date:
Status:Unresolved
Question Id:30

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

FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory

I have tried:

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

But this did not catch the error.

Any help would be greatly appreciated

P.S This is happening when generating MD5 hashes for a string of about eighteen files and I am using the md5 module like this:

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

}
Answer
Date:
Correct:No

You should look at this article about "nodejs memory overflow FATAL ERROR: CALL_AND_RETRY_0 Allocation failed – process out of memory (reproduced)" from https://www.programmersought.com/article/74236143011/

Maybe you can fix it.

Answer
Date:
Correct:No

You should avoid buffering entire files in memory. Calculate the md5 hash a chunk at a time instead. 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);
    });
  });
});

Your Answer

Review Your Answer