【nodejs】FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
当使用大批量(>100)的SQL进行MySql数据库插值任务时,会发生以下错误:
总计将有371579条数据将被插入数据库 开始插入DB <--- Last few GCs ---> [12416:000001BE7F7E63A0] 81278 ms: Mark-sweep 1410.0 (1443.8) -> 1410.0 (1443.8) MB, 1911.9 / 0.0 ms last resort <--- JS stacktrace ---> ==== JS stack trace ========================================= Security context: 000000C777FA8799 <JSObject> 2: createQuery [C:\Users\horn1\Desktop\node.js\66-??????1.09 - insertdb.js\node_modules\mysql\lib\Connection.js:64] [bytecode=000002482E476A59 offset=166](this=000000F8C033D3E1 <JSFunction Connection (sfi = 000002A3CFFB6579)>,sql=000002A3CFFADB71 <String[101]: insert into test.topic17(floor,author,tdate,ttime,addtime,url,title,content) values (?,?,?,?,?,?,?,?)>,values=00000245428FEC61 <JSA... FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory C:\Users\horn1\Desktop\node.js\66-理想论坛爬虫1.09 - insertdb.js>
因为插入的数据有37万条之多,无论使用下面哪种程序都会导致这样的结果:
插入代码1:
//====================================================== // 在理想论坛帖子下载爬虫生成数据文件后读取数据插入数据库 // 2018年5月6日 //====================================================== var fs=require('fs'); // 内置文件处理模块 var allfiles=[]; // 全部数据文件列表 var allInfos=[]; // 数据文件里获得的全部信息 //-------------------------------------- // 将数据插入数据库 //-------------------------------------- function insertDB(){ console.log("开始插入DB"); //console.log('总计将有'+allInfos.length+"条数据将被插入数据库"); var mysql=require('mysql'); // 连接mysql数据库的模块 var conn=mysql.createConnection({ host:'127.0.0.1', port:'3306', database:'test', user:'root', password:'12345678', }); conn.connect(function(err){ if(err){ console.log('与MySQL数据库建立连接失败'); }else{ console.time('插数据花费时间'); for(var i=0;i<allInfos.length;i++){ var info=allInfos[i]; sql='insert into test.topic17(floor,author,tdate,ttime,addtime,url,title,content) values (?,?,?,?,?,?,?,?)'; arr=[info['楼层'],info['作者'],info['日期'],info['时间'],currTime(),info['url'],info['title'],info['内容']]; conn.query(sql,arr,function(err,result){ if(err){ console.log("出现异常:"+err+",此时info="+info); } }) } //conn.end(); console.timeEnd('插数据花费时间'); } }); } //-------------------------------------- // 通用函数,返回当前日期时间 数据库记录时间用 //-------------------------------------- function currTime() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate =date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds()+ " "; return currentdate; } //------------------------------- // 得到带颜色(前景色)的文字,用于在控制台输出 // text:文字,color:前景色 //------------------------------- function coloredText(text,color){ var dic = new Array(); dic["white"] = ['\x1B[37m', '\x1B[39m']; dic["grey"] = ['\x1B[90m', '\x1B[39m']; dic["black"] = ['\x1B[30m', '\x1B[39m']; dic["blue"] = ['\x1B[34m', '\x1B[39m']; dic["cyan"] = ['\x1B[36m', '\x1B[39m']; dic["green"] = ['\x1B[32m', '\x1B[39m']; dic["magenta"] = ['\x1B[35m', '\x1B[39m']; dic["red"] = ['\x1B[31m', '\x1B[39m']; dic["yellow"] = ['\x1B[33m', '\x1B[39m']; return dic[color][0]+text+dic[color][1]; } function readFiles(){ if(allfiles.length>0){ var file=allfiles.pop(); fs.readFile(file,'utf8',function(err,data){ if(err){ console.log('读取文件失败,因为'+err); }else{ var infos=JSON.parse(data); allInfos=allInfos.concat(infos); } readFiles(); }); }else{ console.log('总计将有'+allInfos.length+"条数据将被插入数据库"); insertDB(); } } //-------------------------------------- // 入口函数 //-------------------------------------- function main() { folder='./test';// 数据文件所在目录 fs.readdir(folder,function(err,files){ if(err) throw err; // 预制查找数组 var arr=new Array(100-1);// 20563是总文件个数,从lixiang.js的输出可以找到,-1因为文件序号从0起 for(var i=0;i<arr.length;i++){ arr[i]=0; } // 只遍历一遍 for(var index in files){ var arrTmp=files[index].split("."); var sn=parseInt(arrTmp[0],10); arr[sn]=1; allfiles.push(folder+'/'+files[index]); } // 查哪个缺失 var missed=0; for(var i=0;i<arr.length;i++){ if(arr[i]==0){ // 为零的就是没找到该编号对应的文件的 console.log(coloredText('编号为'+i+'的文件缺失','yellow')); missed++; } } console.log(coloredText('总计有'+missed+'个文件缺失','red')); readFiles(); }); } // 开始 main();
插入代码2:
//====================================================== // 在理想论坛帖子下载爬虫生成数据文件后读取数据插入数据库 // 2018年5月6日 //====================================================== var fs=require('fs'); // 内置文件处理模块 var allfiles=[]; // 全部数据文件列表 var allInfos=[]; // 数据文件里获得的全部信息 //-------------------------------------- // 将数据插入数据库 //-------------------------------------- function insertDB(){ console.log("开始插入DB"); //console.log('总计将有'+allInfos.length+"条数据将被插入数据库"); var mysql=require('mysql'); // 连接mysql数据库的模块 var pool=mysql.createPool({ host:'127.0.0.1', port:'3306', database:'test', user:'root', password:'12345678', connectionLimit:10, }); pool.getConnection(function(err,conn){ console.time('插数据花费时间'); for(var i=0;i<allInfos.length;i++){ var info=allInfos[i]; sql='insert into test.topic17(floor,author,tdate,ttime,addtime,url,title,content) values (?,?,?,?,?,?,?,?)'; arr=[info['楼层'],info['作者'],info['日期'],info['时间'],currTime(),info['url'],info['title'],info['内容']]; conn.query(sql,arr,function(err,result){ if(err){ console.log("出现异常:"+err+",此时info="+info); } }) } console.timeEnd('插数据花费时间'); conn.close(); }); } //-------------------------------------- // 通用函数,返回当前日期时间 数据库记录时间用 //-------------------------------------- function currTime() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate =date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds()+ " "; return currentdate; } //------------------------------- // 得到带颜色(前景色)的文字,用于在控制台输出 // text:文字,color:前景色 //------------------------------- function coloredText(text,color){ var dic = new Array(); dic["white"] = ['\x1B[37m', '\x1B[39m']; dic["grey"] = ['\x1B[90m', '\x1B[39m']; dic["black"] = ['\x1B[30m', '\x1B[39m']; dic["blue"] = ['\x1B[34m', '\x1B[39m']; dic["cyan"] = ['\x1B[36m', '\x1B[39m']; dic["green"] = ['\x1B[32m', '\x1B[39m']; dic["magenta"] = ['\x1B[35m', '\x1B[39m']; dic["red"] = ['\x1B[31m', '\x1B[39m']; dic["yellow"] = ['\x1B[33m', '\x1B[39m']; return dic[color][0]+text+dic[color][1]; } function readFiles(){ if(allfiles.length>0){ var file=allfiles.pop(); fs.readFile(file,'utf8',function(err,data){ if(err){ console.log('读取文件失败,因为'+err); }else{ var infos=JSON.parse(data); allInfos=allInfos.concat(infos); } readFiles(); }); }else{ console.log('总计将有'+allInfos.length+"条数据将被插入数据库"); insertDB(); } } //-------------------------------------- // 入口函数 //-------------------------------------- function main() { folder='./2018-05-05 9_8_49';// 数据文件所在目录 fs.readdir(folder,function(err,files){ if(err) throw err; // 预制查找数组 var arr=new Array(23000-1);// 20563是总文件个数,从lixiang.js的输出可以找到,-1因为文件序号从0起 for(var i=0;i<arr.length;i++){ arr[i]=0; } // 只遍历一遍 for(var index in files){ var arrTmp=files[index].split("."); var sn=parseInt(arrTmp[0],10); arr[sn]=1; allfiles.push(folder+'/'+files[index]); } // 查哪个缺失 var missed=0; for(var i=0;i<arr.length;i++){ if(arr[i]==0){ // 为零的就是没找到该编号对应的文件的 console.log(coloredText('编号为'+i+'的文件缺失','yellow')); missed++; } } console.log(coloredText('总计有'+missed+'个文件缺失','red')); readFiles(); }); } // 开始 main();
究其原因,应该是数据库并发数有限,有太多的回调来不及执行而堆积起来造成的堆缺乏存储空间。
这是关于此问题的一些讨论 https://cnodejs.org/topic/500904be4764b72902d30f4d
2018年5月7日
【nodejs】FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory的更多相关文章
- 解决 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 问题
https://blog.csdn.net/weixin_41196185/article/details/81114226 今天在启动vue项目的时候报了这样一个错误 观察到关键词是 FATAL E ...
- FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
vue项目 npm run dev 报错 WAIT Compiling...16:36:21 95% emittingFATAL ERROR: CALL_AND_RETRY_LAST Allocati ...
- angular4 JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)
最近在写基于angular4的项目的时候,在build --prod的时候,突然措手不及的蹦出个报错,大致错误如下: 70% building modules 1345/1345 modules 0 ...
- Angular JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)
方法一和方法二参考:https://www.cnblogs.com/liugang-vip/p/6857595.html 方法一:my-project/node_modules/.bin 下增大内存( ...
- 解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 如上图所示:频繁出现此 ...
- Vue项目运行或打包时,频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
前端使用基于vue的Nuxt框架,但是随着项目功能增多,项目变大,频繁出现此种情况,原因是项目太大,导致内存溢出,排除代码问题外,可参照以下方式解决 解决方案 1.全局安装increase-memor ...
- node 打包内存溢出 FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
electron-vue加载了地图 openLayer后,打包就包内存溢出 解决办法: "build": "node --max_old_space_size=4096 ...
- webpack 内存溢出 Allocation failed - JavaScript heap out of memory
项目中,当组件文件过多,webpack-dev-server 编译时,容易内存溢出, 在 \node_modules\.bin\webpack-dev-server.cmd 加以下红色配置,暂可解决 ...
- nodejs内存溢出 FATAL ERROR: CALL_AND_RETRY_0 Allocation failed – process out of memory
spa项目整体迁移转为ssr后,改动之后部署一切还好,就是突然有一天访问人数太多,node进程很容易就挂了自动重启. 最后经过压力测试,考虑到是堆内存溢出的问题,就报错误:FATAL ERROR: C ...
随机推荐
- DWZ(JUI) 教程 跨域请求 iframeNavTab
如果想navTab访问其他的网址,可以使用 iframe navTab 使用时也非常简单 <li><a href="http://www.baidu.com" ...
- delphi 启动停止windows服务 转
http://blog.csdn.net/haiou327/article/details/6106233 不用cmd用delphi如何实现启动停止windows服务建议参考一下Delphi的Sckt ...
- 一步一步学习ASP.NET 5 (一)-基本概念和环境配置
编者语:时代在变,在csdn开博一年就发了那么的两篇文章.不管是什么原因都认为有愧了.可是今年重心都会在这里发表一些文章,和大家谈谈.NET, 移动跨平台,云计算等热门话题.希望有更好的交流. 好吧言 ...
- lodash用法系列(4),使用Map/Reduce转换
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- ECShop 调用自定义广告
原文地址:http://www.ecshoptemplate.com/article-1348.html ECShop中关于广告的调用方法,网上有很多,现在要介绍的不同于其他,根据实际情况选择使用,以 ...
- 猫都能学会的Unity3D Shader入门指南(一)
动机 自己使用Unity3D也有一段时间了,但是很多时候是流于表面,更多地是把这个引擎简单地用作脚本控制,而对更深入一些的层次几乎没有了解.虽然说Unity引擎设计的初衷就是创建简单的不需要开发者操心 ...
- Node.js批量去除BOM文件
之前的同事写了一个工具,但有bug,就是在替换文件后原文件的格式变成utf8 BOM了,这种带BOM的XML在Mac下可能读取不出来,所以就需要写个工具处理一下- 其实思路比较简单,首先遍历目录, ...
- 如何用 Java 实现 Web 应用中的定时任务?
定时任务,是指定一个未来的时间范围执行一定任务的功能.在当前WEB应用中,多数应用都具备任务调度功能,针对不同的语音,不同的操作系统, 都有其自己的语法及解决方案,windows操作系统把它叫做任务计 ...
- 使用PHP生成二维码图像
1.PHP生成二维码图像的类QRcode http://www.phper.org.cn/?post=128 QRcode是用于生成二维条形码的开放源码 (LGPL) 库.提供 API 创建条码图像. ...
- 【ContestHunter】【弱省胡策】【Round7】
Prufer序列+高精度+组合数学/DP+可持久化线段树 Magic 利用Prufer序列,我们考虑序列中每个点是第几个插进去的,再考虑环的连接方式,我们有$$ans=\sum_{K=3}^n N^{ ...