Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

javascript - Node.js' garbage collector

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...