猫鼬错误:JavaScript堆内存不足

时间:2019-03-28 16:05:09

标签: javascript node.js mongoose

我尝试通过用户获取一些统计信息。这是我尝试执行的操作:

function(cb) { // Get users count + blocked + Abos
    console.log('---- launching count ----');
    User.find({pageId: pageId}).exec(function(error, _users) {
        if (error) return cb(error);
        req._locals.usersCount = _users.length;
        console.log('---- userCount:', _users.length, ' ----' );
        return cb(_users.map((_user) => {
            if (_user.getVariable('tip-subscription-optin') !== null)
                req._locals.tipsSubscriptions++;
            if (_user.getVariable('tip-mercredi-subscription-optin') !== null)
                req._locals.greenSubscriptions++;
        }));
    });
},

但这不起作用。这是错误:

---- launching count ----

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

如您所见,我的第一个console.log在工作,但第二个却没有。因此,我很确定mongoose.find请求对于我的服务器来说太大。

1 个答案:

答案 0 :(得分:1)

确保我仍然自己对此有所了解,因此您的查询用尽了内存来处理数据,并导致接近堆溢出。从技术上讲,您在同一堆栈级别(我认为..)上两次调用此(users.length

 req._locals.usersCount = _users.length;
 console.log('---- userCount:', _users.length, ' ----' );

所以您的堆(RAM上的可用内存)已经用完了内存,无法分配给堆栈(函数使用的内容)..如果删除变量,它将起作用吗 req._locals.usersCount = _users.length;?这样可以减少函数调用所需的内存分配

(另外,其他人,请纠正我,我仍在学习,这对我很有趣:))