我是在 Node js 中设计服务器的新手
我知道 Node js 在单个进程中运行,
我的疑问是
1)函数执行后局部变量的内存状态是否仍被存储 - 例如:函数可能会启动一些异步方法,即使在函数执行之后,其回调也会使用这些变量。
所以假设上述情况为真
2)每次调用(服务器网址)时,都会使用请求、响应变量来调用回调。因此内存状态必须不断增加(因为每个 url 调用的内存状态都会被存储,即因为回调 onRequest 中变量的状态) (请求,响应)被存储),因此它应该在某个时间点由于内存不足而崩溃。这种情况会发生吗?
最佳答案
Is the memory state of local variables of a function stored even after its execution
是的。这称为 closure 。只要您的回调处于事件状态,其范围内的变量就可用。
everytime the (server url) is called its callback is called with request,response variables.So the memory state must be continuosly increasing and therefore it should crash at some point of time due to lack of memory.
没有。当所有工作完成后,并且不再有未完成的(异步)回调引用您的变量,它们将是 garbage collected .
Does this happen?
是的,您可能会编写导致内存泄漏的错误代码。
关于javascript - Node js 中服务器执行期间的内存状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24688588/