在Node.js |中捕获致命错误 致命错误:CALL_AND_RETRY_0分配失败 – 进程内存不足

我想知道是否有办法赶上这个错误:

FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory 

我努力了:

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

但是这并没有发现错误。

任何帮助将不胜感激

PS这发生在生成一个约18个文件的string的MD5散列时,我正在使用这样的md5模块:

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

你应该避免在内存中缓冲整个文件。 一次计算一个块的MD5散列。 例:

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