当使用大批量(>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的更多相关文章

  1. 解决 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 下增大内存( ...

  5. 解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

    解决Vue编译和打包时频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 如上图所示:频繁出现此 ...

  6. Vue项目运行或打包时,频繁内存溢出情况CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

    前端使用基于vue的Nuxt框架,但是随着项目功能增多,项目变大,频繁出现此种情况,原因是项目太大,导致内存溢出,排除代码问题外,可参照以下方式解决 解决方案 1.全局安装increase-memor ...

  7. 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 ...

  8. webpack 内存溢出 Allocation failed - JavaScript heap out of memory

    项目中,当组件文件过多,webpack-dev-server 编译时,容易内存溢出, 在 \node_modules\.bin\webpack-dev-server.cmd 加以下红色配置,暂可解决 ...

  9. nodejs内存溢出 FATAL ERROR: CALL_AND_RETRY_0 Allocation failed – process out of memory

    spa项目整体迁移转为ssr后,改动之后部署一切还好,就是突然有一天访问人数太多,node进程很容易就挂了自动重启. 最后经过压力测试,考虑到是堆内存溢出的问题,就报错误:FATAL ERROR: C ...

随机推荐

  1. oracle like 条件拼接

    (1) ibatis xml配置:下面的写法只是简单的转义 namelike '%$name$%' (2) 这时会导致sql注入问题,比如参数name传进一个单引号“'”,生成的sql语句会是:nam ...

  2. 微信emoji表情编码 、MySQL 存储 emoji 表情符号字符集

    相关资料 微信emoji表情编码 微信用户名显示「emoji表情」 PHP处理微信中带Emoji表情的消息发送和接收(Unicode字符转码编码) MySQL 存储emoji表情 MySQL 存储 e ...

  3. 在busybox里使用ulimit命令

    刚才想使用ulimit修改用户进程的用户栈的大小,发现busybox里没有这个命令,上google搜索得到如下解释: "ulimit" is a shell builtin, me ...

  4. 测试RemObjects Pascal Script

    unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  5. AndroidStudio工具将Module项目导出成Jar和arr库

    原文:http://blog.csdn.net/liulei823581722/article/details/52919697 该篇首先讲述利用AndroidStudio如何把一个module项目导 ...

  6. win7设置电脑锁屏时间

    方法/步骤 1 小编用的win7电脑,进入控制面板先~ 2 选择系统与安全选项. 3 如图所示,箭头所指,可以设置锁屏时间,不过电源选项中还有个设置开启屏幕输入密码的设置,第一个就是. 4 如图所示, ...

  7. 犯罪现场调查第一季/全集CSI迅雷下载

    英文译名 CSI (第1季) (2000首播)CBS. 本季看点:<犯罪现场调查>赌城拉斯维加斯吸引着做发财梦的人,也吸引着形形色色的罪犯,该市警察局的犯罪现场调查局在全美国名列第二.该剧 ...

  8. 使用Aptana Studio 3开发让Extjs变的更简单

    工欲善其事必先利器,做EXTJS开发先整好IDE,为后续的项目应用做准备. 下载地址 http://www.aptana.com/products/studio3/download# 下载汉化包 配置 ...

  9. [DEFCON全球黑客大会] 针对CTF,大家都是怎么训练的?

    https://www.zhihu.com/question/30505597 https://www.zhihu.com/question/42067737

  10. Java NIO AsynchronousFileChannel

    In Java 7 the AsynchronousFileChannel was added to Java NIO. The AsynchronousFileChannel makes it po ...