4

I try to get some stats trough my users. Here's how i try to do it :

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++;
        }));
    });
},

But this isn't working. Here's the error:

---- launching count ----

<--- JS stacktrace --->

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

As you can see, my first console.log is working but not my second. So i'm pretty sure that the mongoose.find request is too big for my server.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Jeremy M.
  • 1,154
  • 1
  • 9
  • 29
  • check this answer: https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory/66914674#66914674 – sidverma Apr 02 '21 at 06:34

1 Answers1

1

Granted that I'm still learning about this myself, you've run out of memory from your query to process the data, and are causing close to a heap overflow. Technically you call this (users.length) twice on the same stack level (I think..)

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

so your heap (free memory on your RAM) has ran out of memory to allocate to your stack (what's used by the functions).. would it work if you remove the variable req._locals.usersCount = _users.length;? This should cut back on the memory allocation needed for the function call

(Also, others, please correct me as well as I'm still learning about this and it's interesting to me :) )

Bryan
  • 274
  • 3
  • 9
  • I tried with removing the two lines you'r showing and it still crash. I really think this is because of my mongoose call and not what's inside it. Plus i'v already tried to put the console log right at the bottom of the mongoose request. – Jeremy M. Mar 28 '19 at 16:26
  • I'd say try to just console.log the `_users.length` and comment everything out. If it works then try it with the variable and only have those 2 things in the function.. If it's not those 2 things then it will be with the DB query itself – Bryan Mar 28 '19 at 17:21
  • also with javascript es6 you can refactor ` { pageId: pageId }` to be `{ pageId }` – Bryan Mar 28 '19 at 17:26
  • Yeah i know it Bryan. I put it like that for the comprehension. I already tried to comment everything. – Jeremy M. Mar 28 '19 at 18:09
  • https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory take a look at this stackoverflow.. you may need to expand the memory usage of your app – Bryan Mar 28 '19 at 20:11