nodejs RangeError: Maximum call stack size exceeded
发布于 8天前 作者 ansendu 204 次浏览 来自 问答

如题 好像 v8 引擎对js的变量有内存限制 这个要如何不让它限制呢最好从代码的角度来修改

11 回复

你这难道不是死循环导致的吗

@malphi 不是的 贴下代码

function load_file_edu(file_edu, cb) {

var file_path = writeDir + generate_file_map['edu_school_dict'];

// var file_path = writeDir + "test.json";

fs.exists(file_path, function (exist) {
    if (exist != true) {
        //直接获取

// edu_school_dict = fs.readFileSync(file_path, “utf8”); // edu_school_dict = require(file_path); // console.log(edu_school_dict); cb(null, null); } else { //分析保存 readLine(file_edu).go(function (line, next) { var items = php.explode("\001", line); var cv_id = items[0]; var shool_id = (items[1] == undefined || items[1] == ‘’) ? 0 : items[1]; var major_id = (items[2] == undefined || items[2] == ‘’) ? 0 : items[2];

            if (!shool_id && !major_id) {
                next();
            }

            if (shool_id) {
                if (edu_school_dict['school'] == undefined) {
                    edu_school_dict['school'] = {};
                }

                if (edu_school_dict['school'][cv_id] == undefined) {
                    edu_school_dict['school'][cv_id] = {};
                    if (edu_school_dict['school'][cv_id][shool_id] == undefined) {
                        edu_school_dict['school'][cv_id][shool_id] = shool_id;
                    }
                } else {
                    if (edu_school_dict['school'][cv_id][shool_id] == undefined) {
                        edu_school_dict['school'][cv_id][shool_id] = shool_id;
                    } else {
                        edu_school_dict['school'][cv_id][shool_id] = shool_id;
                    }
                }
            }
            if (major_id) {
                if (edu_school_dict['majors'] == undefined) {
                    edu_school_dict['majors'] = {};
                }

                if (edu_school_dict['majors'][cv_id] == undefined) {
                    edu_school_dict['majors'][cv_id] = {};
                    if (edu_school_dict['majors'][cv_id][major_id] == undefined) {
                        edu_school_dict['majors'][cv_id][major_id] = major_id;
                    }
                } else {
                    if (edu_school_dict['majors'][cv_id][major_id] == undefined) {
                        edu_school_dict['majors'][cv_id][major_id] = major_id;
                    } else {
                        edu_school_dict['majors'][cv_id][major_id] = major_id;
                    }
                }
            }

// console.log(edu_school_dict); next(); }, function () { console.log(edu_school_dict[‘majors’][12938664]); cb(null, null); });

    }
});

}

@malphi

edu_school_dict这个变量越来越大

单个函数堆栈层级超过V8引擎允许极限,堆栈溢出,大概是10000了,你循环引用了

@enmoon 有什么好的办法吗

经测 同一函数同时只能调用: 17964 次

@hezedu 没什么好的办法了吗?

逻辑就不看了,分析下你的想法; V8设置内存限制是由理由的,每个进程1.7G左右的内存足够作为服务进程来使用。如果超过这个限制首先考虑的是内存泄露,其次是超过单实例能力,最后实在没辙再从node启动项调整进程的可用最大内存。如果前两部没有做完直接修改只能是治标不治本。 BTW: node app.js --max-stack-size 最大内存

@ansendu 应该从程序设计上解决,调用那么多次是有问题的设计嘛

@haozxuan V8 window下是0.7G,其它是1.4G的

@enmoon 学习了,都是在Windows下写,linux下部署。

回到顶部