Node.js的垃圾收集器 [英] Node.js' garbage collector

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

问题描述

我从这个线程学习了使用node.js进行垃圾回收我常常使用循环对象引用(我最终会删除/确保超出范围),并且想知道节点是否存在.js可以很好地处理它们。因此,例如。如果它是使用ref完成的。计数,会有一个问题,所以我想知道节点的好处。



一些使用场景:


  1. 对于每个http请求,我都会创建一个带有lambda的setTimeout,该lambda可能具有对作用域对象的引用。范围对象也有对超时对象的引用等等。


  2. 对于每个用户会话,我都有一个指针 (还在做C编程)对http请求对象的引用,这些对象也具有对会话对象的引用等等...请求对象经常被删除,但会话对象不是。

编辑:我问这是因为我在网上找到的这个链接 http://lifecs.likai.org/2010/02/how-generational-garbage-collector.html


<正如你所提到的,NodeJS使用了一代GC ......特别是 v8 。因此,它不做引用计数类型的GC。相反,它会执行完整的mark-and-sweep类型的GC。

因此,只要您摆脱对一组对象的所有引用(即使它们是循环的,他们应该在某个时候收集垃圾。

这并不意味着你不应该关心GC。如果你有一个非常活跃的NodeJS服务器,那么垃圾收集器将非常难以清理你的垃圾,特别是如果你有很多中等寿命对象(即不是短期或长期存在的)。


I have learnt from this thread garbage collection with node.js that node.js uses a generational GC.

I routinely use cyclic object references (both of which I delete/ensure go out of scope eventually) and would like to know if node.js handles them well. So for eg. if it was done using ref. counting, there would be a problem, so I would like to know how good node is at this.

Some usage scenarios:

  1. For every http request, I create a setTimeout with a lambda which potentially has references to scope objects. The scope object also has a reference to the timeout object, etc...

  2. For every user session, I have a pointer (still doing C programming) reference to the http request objects which also have references to the session object, etc... The request objects are deleted often, but the session object is not.

Edit: I ask because of this link that I found online http://lifecs.likai.org/2010/02/how-generational-garbage-collector.html

解决方案

As you mentioned, NodeJS uses a generational GC... specifically v8. And therefore, it does NOT do reference counting type GC. Instead, it does a full mark-and-sweep type GC.

So as long as you get rid of all references to a set of objects (even if they cyclic-ly point to one another), they should get garbage collected at some point.

That doesn't mean you shouldn't care about the GC. If you have a very active NodeJS server, the garbage collector will be working really hard to cleanup your garbage, especially if you have lots of "medium-life-span" objects (i.e. not short or long lived).

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

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