问题
i am trying to preprocess my js files for each platform and beautify it using my js-preprocess.js .i am able to process upto 153 files.if the number of files increase,the console is showing the following error! FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory Please help me to resolve this! here is my one code snippet for the main job!
fs.readdir(inputdir,function(err,files) {
console.time('PreProcessingJStime')
files.forEach(function(file) {
if (file.indexOf(".js") != -1) {
console.log('PreProcessing JS File : '+file);
fs.writeFile(outputdir+"/"+file,beautify.js_beautify(preprocess(fs.readFileSync(inputdir+"/"+file,'utf-8'),defines).join('\n'),{preserve_newlines:false}));
}
},this);
console.timeEnd('PreProcessingJStime')
})
Thanks in advance :)
回答1:
You're using fs.writeFile
, which is asynchronous. This means you're writing to all files at the same time, and all the data needed for this is buffered in memory. If you change it to fs.writeFileSync
, then it finishes with one file before moving on to the next one, and all the data concerning previous files becomes garbage, so the garbage collector will get it when it needs to.
来源:https://stackoverflow.com/questions/25154423/fatal-error-call-and-retry-2-allocation-failed-process-out-of-memory-in-prepr