简体   繁体   English

nodejs 服务器空闲数组内存分配

[英]nodejs server free array memory allocation

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?这是我第一次创建 Nodejs 服务器,我注意到全局变量值在对服务器的不同查询之间是持久的,这让我想我应该清除数组还是做其他事情来减少内存使用?

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?例如,如果我有一个数组(在函数中声明),它被推入值并记录到response.send(JSON.stringify(array)) ,我应该在函数调用后将它设置为 null 吗?

Furthermore is there a better way to handle the end of connection/request/response in Nodejs to reduce memory usage?此外,是否有更好的方法来处理 Nodejs 中的连接/请求/响应结束以减少内存使用?

...
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();
}

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.但是,如果您在函数外部声明数组并在每次请求时将元素推入其中,则您需要采取行动,因为它正在全局保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM