1

It's my first time creating a Nodejs server, I have noticed that global variable values are persistent between different queries to the server, that made me think should I clear arrays or do any thing else for reducing memory usage?

For example if I have an array(declared inside a function) that get pushed with values and get logged to response.send(JSON.stringify(array)), should I set it to null after the function call?

Furthermore is there a better way to handle the end of connection/request/response in Nodejs to reduce memory usage?

...
code
...

if (results.length > 0) {
    var cur_data = {};
    cur_data["day"] = day_i;
    cur_data["data"] = results;
    ret_val.push(cur_data);
}

...
code
...

finish(con, req, res, JSONstringfy(ret_val), false);
ret_val = null;

function finish(con, req, res, log, is_error)
{
    req.pause();
    res.status(is_error == true ? 400 : 200);
    res.send(log);
    con.end();
}

1 Answer 1

1

If the array is inside the function, you don't need to be worry because as soon as function finish its execution it release its resources. But if you declared the array outside the functions and pushing element in it on every request then you need to take action because it is saving globally.

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

Comments

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.