【问题标题】:FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed — process out of memory致命错误:CALL_AND_RETRY_LAST 分配失败 — 进程内存不足
【发布时间】:2019-06-28 22:32:10
【问题描述】:

要下载为 csv 文件,我不知道是什么问题,当用户有 40,000 条记录时,它工作正常,当我试图点击 api(80,000 条记录)时,它的显示过程内存不足。请帮我解决这个问题

  app.get('/excelData', function (req, result) {
 var fields = ['firstName','lastName','catalina_cardStatus','deviceType','appVerison','phoneNumber','emailAddress','language','state','lastLogin','dateOfBirth']
 var data = [];
   var status = true;
   User.find({accountType:'user',isDeleted: {$ne:{status}}}).sort({created: -1}).exec((err, res) => {
     var resultData = [];
         var len = res.length;
         res.map((user,key) =>{
       var resData = {};
       if(user) {
         resData.firstName = user.firstName;
         resData.lastName = user.lastName;
         resData.catalina_cardStatus = user.catalina_cardStatus;
         resData.language = user.language;
         resData.phoneNumber = user.phoneNumber;
         resData.deviceType = user.deviceType;
         resData.appVerison = user.appVerison;
         resData.emailAddress = user.emailAddress;
         resData.state = user.state;
         resData.dateOfBirth = user.dateOfBirth;
         if(user.lastLogin){
           var date = new Date(user.lastLogin);
           var utcDate = new Date(date.toISOString());
           var utc = utcDate.setHours(utcDate.getHours()-8);
           var usDate = utc;
             var time = moment(utc).format("DD-MM-YYYY");
             resData.lastLogin = time;
         }
 }
          resultData.push(resData);
          len--;
          if(len==0){
                var csv = json2csv({ data:resultData , fields: fields });
                fs.writeFile('path', csv, function(err,data) {
                  setTimeout(function () {
                      fs.unlinkSync('path'.csv');
                    }, 30000);
                    result.send({msg: "saved sucessfully",path :'path.csv'});
                  if (err)
                  {
                    throw err;
                    console.log("Error: "+err);
                  }
                })
          }

     })
 })

 });

【问题讨论】:

    标签: javascript node.js mongodb forever


    【解决方案1】:

    当为执行应用程序分配的内存小于运行应用程序时所需的内存时,会出现此错误。

    Node.js默认内存限制为512mb,要解决这个问题需要增加内存限制使用命令—- max-old-space-size,可以避免内存限制问题。

    node --max-old-space-size=1024 index.js #increase to 1gb
    node --max-old-space-size=2048 index.js #increase to 2gb
    node --max-old-space-size=3072 index.js #increase to 3gb
    node --max-old-space-size=4096 index.js #increase to 4gb
    node --max-old-space-size=5120 index.js #increase to 5gb
    node --max-old-space-size=6144 index.js #increase to 6gb
    node --max-old-space-size=7168 index.js #increase to 7gb
    node --max-old-space-size=8192 index.js #increase to 8gb
    

    【讨论】:

    • 尝试使用此库逐行读取 xlsx 文件:github.com/ffalt/xlsx-extract 如果上述失败,请尝试github.com/extrabacon/xlrd-parser
    • @susmithapanda 确保您将--max-old-space-size 标志放在之前 index.js,否则该标志将传递给您的程序而不是node.js 运行时。也就是说,即使使用正确的标志,我也会遇到类似的问题。如果我找到解决方案会报告。
    猜你喜欢
    • 2014-11-23
    • 2016-12-19
    • 1970-01-01
    • 2018-11-10
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    相关资源
    最近更新 更多