node.js的垃圾收集 [英] garbage collection with node.js

查看:116
本文介绍了node.js的垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇嵌套函数的node.js模式如何与v8的垃圾收集器一起工作。
这是一个简单的例子

I was curious about how the node.js pattern of nested functions works with the garbage collector of v8. here's a simple example

readfile("blah", function(str) {
   var val = getvaluefromstr(str);
   function restofprogram(val2) { ... } (val)
})

如果restofprogram长时间运行,这是不是意味着str永远不会被垃圾收集?我的理解是,对于节点,你最终会使用嵌套函数。如果在外部声明restofprogram,这是否会收集垃圾,因此str不在范围内。这是推荐的做法吗?

if restofprogram is long running, doesnt that mean that str will never get garbage collected? My understanding is that with node you end up with nested functions a lot. Does this get garbage collected if restofprogram was declared outside, so str could not be in scope. Is this a recommended practice?

编辑我不打算让问题变得复杂。那只是粗心,所以我修改了它。

EDIT I didnt intend to make the problem complicated. That was just carelessness, so I've modified it.

推荐答案

简单回答:如果的值str 未在其他任何地方引用(并且 str 本身未引用 restofprogram )一旦函数(str){...} 返回,它就会无法访问。

Simple answer: if value of the str is not referenced from anywhere else (and str itself is not referenced from restofprogram) it will become unreachable as soon as the function (str) { ... } returns.

详细信息:V8编译器将真正的 local 变量与闭包捕获的所谓 context 变量区分开来,由 with -statement或隐藏eval 调用。

Details: V8 compiler distinguishes real local variables from so called context variables captured by a closure, shadowed by a with-statement or an eval invocation.

局部变量存在于堆栈中并在函数执行完成后立即消失。

Local variables live on the stack and disappear as soon as function execution completes.

上下文变量存在于堆分配的上下文结构。当上下文结构消失时,它们就会消失。这里要注意的重要一点是来自同一范围的上下文变量存在于相同结构中。让我用示例代码来说明它:

Context variables live in a heap allocated context structure. They disappear when the context structure dies. Important thing to note here is that context variables from the same scope live in the same structure. Let me illustrate it with an example code:

function outer () {
  var x; // real local variable
  var y; // context variable, referenced by inner1
  var z; // context variable, referenced by inner2

  function inner1 () {
    // references context 
    use(y);
  }

  function inner2 () {
    // references context 
    use(z);
  }

  function inner3 () { /* I am empty but I still capture context implicitly */ } 

  return [inner1, inner2, inner3];
}

在此示例变量 x 外部返回,c>将消失,但变量 y z 仅在 inner1 inner2 inner3 死亡。发生这种情况是因为 y z 在相同的上下文结构中分配,并且所有三个闭包隐式引用此上下文结构(甚至 inner3 没有明确使用它。)

In this example variable x will disappear as soon as outer returns but variables y and z will disappear only when both inner1, inner2 and inner3 die. This happens because y and z are allocated in the same context structure and all three closures implicitly reference this context structure (even inner3 which does not use it explicitly).

当你开始使用时,情况会变得更加复杂 -statement, try / catch -statement,它在V8中包含一个隐含的 with -statement catch子句或global eval

Situation gets even more complicated when you start using with-statement, try/catch-statement which on V8 contains an implicit with-statement inside catch clause or global eval.

function complication () {
  var x; // context variable

  function inner () { /* I am empty but I still capture context implicitly */ }

  try { } catch (e) { /* contains implicit with-statement */ }

  return inner;
}

在此示例中 x 只有在内部死亡时才会消失。因为:

In this example x will disappear only when inner dies. Because:


  • 尝试/捕获 - 在catch子句中隐含 with -statement

  • V8假设 - 陈述阴影全部本地人

  • try/catch-contains implicit with-statement in catch clause
  • V8 assumes that any with-statement shadows all the locals

这迫使 x 成为一个上下文变量, inner 捕获上下文所以 x 存在直到内部去世。

This forces x to become a context variable and inner captures the context so x exists until inner dies.

一般来说,如果你要确保给定变量不会保留某个对象超过实际需要的时间,您可以通过为该变量分配 null 轻松销毁此链接。

In general if you want to be sure that given variable does not retain some object for longer than really needed you can easily destroy this link by assigning null to that variable.

这篇关于node.js的垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆