【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 ...
随机推荐
- Revit API修改链接文件房间边界
start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] );//设置房间边界 ...
- 在busybox里使用ulimit命令
刚才想使用ulimit修改用户进程的用户栈的大小,发现busybox里没有这个命令,上google搜索得到如下解释: "ulimit" is a shell builtin, me ...
- 解决ubuntu上在androidstudio中启动emulator闪退的问题(1)
作者 彭东林 pengdonglin137@163.com 平台 Ubuntu14.04 64 androidstudio 2.3.3 现象 在创建好模拟器后,点击启动时,模拟器界面刚出来就闪退了 解 ...
- 淘宝接口 TopAPi
演示一下调用淘宝的接口,让大家心里有个数, 很简单,新建一个工程,拖一个IDHttp,Button和Memo到窗体上去 然后在这个Button的OnClick事件中写入如下代码: [delphi] v ...
- Maven 构建
最近在工作中越来越经常的用到了Maven作为项目管理和Jar包管理和构建的工具,感觉Maven的确是很好用的.而且要将Maven的功能最大发挥出来,多模块是一个很好的集成例子. 一个Maven项目包括 ...
- AES加解密算法在Android中的应用及Android4.2以上版本调用问题
from://http://blog.csdn.net/xinzheng_wang/article/details/9159969 AES加解密算法在Android中的应用及Android4.2以上 ...
- jquery.timers使用说明
jQuery Timers提供了三个函式 1. everyTime(时间间隔, [定时器名称], 函式名称, [次数限制], [等待函式程序完成])2. oneTime(时间间隔, [定时器名称], ...
- nginx 获取自定义header头部信息
为了排查线上的bug,需要在nginx的日志中,打印客户端上传上来的header头部信息,同时头部信息是自定义的.在尝试多重方案后,找到解决方法: log_format dm '"$remo ...
- 使用系统自带的 UIRefreshControl 实现下拉刷新
UIRefreshControl 为 UITableViewController 中的一个属性,从以下可以看出, IOS6.0 以上才支持. @property (nonatomic,retain) ...
- 如何选择JAVA培训机构,兼议什么样的人适合培训机构
首先,利益相关,本人就是培训机构创办者兼讲师,但这不妨碍我对此发表看法. 我们先来看什么样的人适合培训机构?要回答这个问题,需要先换个角度.这个角度就是,你学习一门语言的动机是什么?99%的人的回答是 ...