垃圾收集器在Node.js中 [英] Garbage collector in Node.js

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

问题描述

据谷歌称,V8通过采用世界停止,世代,准确,垃圾收集器来使用高效垃圾收集。部分原因是V8在执行垃圾收集周期时会停止执行程序。



一个显而易见的问题是,如何在暂停程序执行时拥有高效的GC?



我试图找到更多关于这个话题的信息,因为我有兴趣知道当每秒可能发生数十次请求和请求时,GC如何影响响​​应时间您的node.js服务器。



任何专家的帮助,个人经验或链接将不胜感激



谢谢

解决方案

Efficient可以表示几件事情。这里可能指的是高吞吐量。当看到响应时间时,你对 latency 更感兴趣,这可能确实比其他GC策略更差。



主要选择停止世界GC是


  • 增量GC,在将控制权移交给增变器之前,无需完成收集周期 1

  • 并发GC(它们(虚拟)同时操作 作为增变器,只是非常短暂地中断它(例如扫描


    $ b

    在面对并发修改堆的情况下,两者都需要执行额外的工作(例如,如果一个新的对象被创建并附加到已扫描的对象,必须注意这个新的引用)。这会影响总吞吐量,即实际清理整个堆会花费更长的时间。好处是他们不会(通常)中断程序很长时间,如果有的话,所以延迟很低(er)。



    虽然V8文档仍然提到一个停止世界的收集器,似乎 V8 GC 增量。因此,尽管它偶尔会停止执行程序,但它并不会停止程序,只需要很长时间扫描整个堆。相反,它可以扫描几毫秒,然后让程序继续。


    $ b

    1 Mutator是程序的GC术语其堆是垃圾收集。

    2 至少原则上,这可能是可配置的。


    According to google, V8 uses an efficient garbage collection by employing a "stop-the-world, generational, accurate, garbage collector". Part of the claim is that the V8 stops program execution when performing a garbage collection cycle.

    An obvious question is how can you have an efficient GC when you pause program execution?

    I was trying to find more about this topic as I would be interested to know how does the GC impacts the response time when you have possibly tens of thounsands requests per second firing your node.js server.

    Any expert help, personal experience or links would be greatly appreciated

    Thank you

    解决方案

    "Efficient" can mean several things. Here it probably refers to high throughput. When looking at response time, you're more interested in latency, which could indeed be worse than with alternative GC strategies.

    The main alternatives to stop-the-world GCs are

    • incremental GCs, which need not finish a collection cycle before handing back control to the mutator1 temporarily, and
    • concurrent GCs which (virtually) operate at the same time as the mutator, interrupting it only very briefly (e.g. to scan the stack).

    Both need to perform extra work to be correct in the face of concurrent modification of the heap (e.g. if a new object is created and attached to an already-scanned object, this new reference must be noticed). This impacts total throughput, i.e., it takes longer to actually clean the entire heap. The upside is that they do not (usually) interrupt the program for very long, if at all, so latency is low(er).

    Although the V8 documentation still mentions a stop-the-world collector, it seems that an the V8 GC is incremental since 2011. So while it does stop program execution once in a while, it does not 2 stop the program for however long it takes to scan the entire heap. Instead it can scan for, say, a couple milliseconds, and let the program resume.

    1 "Mutator" is GC terminology for the program whose heap is garbage collected.

    2 At least in principle, this is probably configurable.

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

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