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.

1

1 Answer 1

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 :) )

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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
also with javascript es6 you can refactor ` { pageId: pageId }` to be { pageId }
Yeah i know it Bryan. I put it like that for the comprehension. I already tried to comment everything.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.