NODE.JS:致命错误 - JS 分配失败 - 处理内存不足,同时解析大型 excel 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21927377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NODE.JS: FATAL ERROR- JS Allocation failed - process out of memory, while parsing large excel files
提问by bijin-ab
I am using nodejs to parse xlsx files with module "jsxlsx_async" and values will be stored in mongodb. My code:
我正在使用 nodejs 解析带有模块“jsxlsx_async”的 xlsx 文件,并且值将存储在 mongodb 中。我的代码:
xlsx(file, function(err,wb){
if (err){
//handling err
}
//get data array
wb.getSheetDataByName('Sheet1', function(err,data){
if (err){
//handling err
}
//handling data
console.log(data);
});
});
Using: Nodejs: v0.10.25, MongoDB: v2.2.6, OS: win8, RAM:6GB
使用:Nodejs:v0.10.25,MongoDB:v2.2.6,操作系统:win8,RAM:6GB
My steps: 1.read uploaded xlsx file and saving those read values into an JS object. 2.Save the read values into mongodb collections by iterating the values on the JS object.
我的步骤: 1.读取上传的 xlsx 文件并将这些读取值保存到 JS 对象中。2.通过迭代JS对象上的值将读取的值保存到mongodb集合中。
This works fine with smaller xlsx files but I wanted to parse xlsx files larger than 50MB.
这适用于较小的 xlsx 文件,但我想解析大于 50MB 的 xlsx 文件。
My problem is where I am storing the entire xlsx values in a single JS object. Please provide some better ideas for a solution. Is there any better way to read xlsx by row and saving the values at once a row is read?
我的问题是我将整个 xlsx 值存储在单个 JS 对象中。请为解决方案提供一些更好的想法。有没有更好的方法来逐行读取 xlsx 并在读取一行时立即保存值?
回答by Vince Yuan
I had a similar problem before. I need to read a huge JSON object from a txt file, but the process was killed because it ran out of memory. Regarding this problem, my solution was to split this huge file into 2 files.
我之前也遇到过类似的问题。我需要从 txt 文件中读取一个巨大的 JSON 对象,但该进程因内存不足而被终止。关于这个问题,我的解决方案是将这个巨大的文件分成2个文件。
Regarding your problem, my suggestions are:
针对你的问题,我的建议是:
Try increasing memory limit of v8 engine. https://github.com/joyent/node/wiki/FAQExample (8192 means 8GB):
node --max-old-space-size=8192 server.jsIf #1 does not work, try reading xlsx file row by row with this lib: https://github.com/ffalt/xlsx-extract
If #1, #2 do not work, try https://github.com/extrabacon/xlrd-parser
尝试增加 v8 引擎的内存限制。https://github.com/joyent/node/wiki/FAQ示例(8192 表示 8GB):
node --max-old-space-size=8192 server.js如果 #1 不起作用,请尝试使用此库逐行读取 xlsx 文件:https: //github.com/ffalt/xlsx-extract
如果 #1、#2 不起作用,请尝试https://github.com/extrabacon/xlrd-parser

