【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 ...
随机推荐
- Hadoop: the definitive guide 第三版 拾遗 第四章
第四章中提到了通过CompressionCodec对streams进行压缩和解压缩,并提供了示例程序: 输入:标准输入流 输出:压缩后的标准输出流 // cc StreamCompressor A p ...
- delphi代码实现窗口最小化,最大化,关闭消息发送
分类: 代码实现窗口最小化,最大化,关闭 var hwnd: hwnd;//句柄 PostMessage(hwnd,WM_SYSCOMMAND, SC_MINIMIZE,0); //最小化Post ...
- 安装/使用 MVVMLight(转)
安装 MVVMLight Toolkit 为什么说是 MVVMLight Toolkit ?实际上我们一般安装的都是 MVVMLight Toolkit ,这个里面包含了工具就是在VS新建工程的时候你 ...
- mormot中间件成功匹配客户端FDMemTable和ClientDataSet
mormot中间件成功匹配客户端FDMemTable和ClientDataSet
- 解决警告 warning: directory not found for option
解决方法: 选择项目名称----->Targets----->Build Settings----->Search Paths----->Library Search Path ...
- self.xxx = nil 可以等效于[_xxx release] _xxx= nil 么
如果属性是copy.retain的话是等价的.如下: - (void)setXXX:(NSString*)axx { if (_xxx != axx) { [_xxx release]; _xxx = ...
- Android上的单元测试
Android上的单元测试 http://www.sina.com.cn 2009年12月04日 16:07 IT168.com [IT168 技术文档]任何程序的开发都离不开单元测试来保证其健壮 ...
- 动态规划算法——最长公共子序列问题(java实现)
已知序列X=(A,B,C,A,B,D,A)和序列Y=(B,A,D,B,A),求它们的最长公共子序列S. /* * LCSLength.java * Version 1.0.0 * Created on ...
- 黑吃黑第一季/全集Banshee迅雷下载
第一季 Banshee Season 1 (2013)看点:“Banshee”在爱尔兰盖尔语中被称为“bean sidhe”,意思是“拥有超能力的女人”.不过在鲍尔的新剧中,Banshee并不是指超自 ...
- [Web 前端 ] ES6 == ES 2015
cp from : https://www.cnblogs.com/ricoliu/p/5996149.html 遇到了一个困惑 原来称作es6的现在突然变成es2015 了 原因是这个事ecma ...