【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 ...
随机推荐
- spanner-becoming-a-sql-system 2017
https://blog.acolyer.org/2017/07/03/spanner-becoming-a-sql-system/?utm_source=tuicool&utm_medium ...
- 百度地图api---实现新建地图
调用这个函数 function refresh() { history.go(0); } 实现了地图新建
- Delphi 完全时尚手册之 Visual Style 篇 (界面不错) 转自http://blog.csdn.net/iseekcode/article/details/4733229
这里先说说两个概念:Theme(主题)和 Visual Style .Theme 最早出现在 Microsoft Plus! for Windows 95 中,是 Windows 中 Wallpape ...
- 一个最简单的Delphi2010的PNG异形窗口方法
同事演示了一个.NET的的PNG异形窗口.挺漂亮.于是也想用Delphi显摆一个. 关于Delphi用PNG做异形窗口的资料有不少.都是用GDIPlus或者TPNGImage组件加载PNG图像做的.但 ...
- 安装/使用 MVVMLight(转)
安装 MVVMLight Toolkit 为什么说是 MVVMLight Toolkit ?实际上我们一般安装的都是 MVVMLight Toolkit ,这个里面包含了工具就是在VS新建工程的时候你 ...
- SQLServer2008:在查看表记录或者修改存储过程时出现错误。错误消息为: 目录名无效
登陆数据库后,右键打开表提示:目录名无效,执行SQL语句也提示有错误,本来想重装的这个肯定能解决,但是这个方法真的不视为上上策啊,于是在网上找到了这个解决办法,还真是立即见效啊!分享给大家,希望有帮助 ...
- IOS应用发布NSLog的注释及使用重定向,把控制台内容写入文件
1 选择工程的Target -> Build Settings -> Preprocessor Macros. 如图,默认 Debug项,是“DEBUG=1”. 2 在程序中设置全局宏定义 ...
- Android Initializing a Build Environment
from://https://source.android.com/source/initializing.html#next-download-the-source Initializing a B ...
- android ActionBarImpl can only be used with a compatible window decor layout
在Activity中添加 requestWindowFeature(Window.FEATURE_ACTION_BAR); requestWindowFeature(Window.FEATURE_AC ...
- CentOS 7 在vmware中的网络设置
一环境说明 二centos在vmware中的安装 三NAT网络设置 四设置固定IP 1修改网卡配置说明 2修改etcresolvconf 实现域名解析 五设置防火墙iptables 1 centos安 ...