后台垃圾回收和并发垃圾回收的区别? [英] Difference between background and concurrent garbage collection?

查看:37
本文介绍了后台垃圾回收和并发垃圾回收的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到使用 .NET Framework 4 替换了当前的垃圾收集实现:

I read that with .NET Framework 4 the current garbage collection implementation is replaced:

.NET Framework 4 提供背景垃圾收集.这功能替换并发垃圾以前版本中的集合和提供更好的性能.

The .NET Framework 4 provides background garbage collection. This feature replaces concurrent garbage collection in previous versions and provides better performance.

这个页面那里是一个解释它是如何工作的,但我不确定我是否理解它.

At this page there is an explanation how it works but I am not sure I understood it.

在实际应用中,这种新的 GC 实现有什么好处?它是一个可用于推动从 3.5 或之前版本过渡到 4.0 的功能吗?

In practical world application what is the benefit of this new GC implementation? Is it a feature that could be use to push for a transition from 3.5 or previous to 4.0?

推荐答案

这里,微软使用并发"和背景"这两个名称来描述它在 .NET 中使用的两个版本的 GC.在 .NET 世界中,后台收集器"是对并发收集器"的增强,因为它对收集器运行时应用程序线程可以做什么的限制较少.

Here, Microsoft uses the names "concurrent" and "background" to describe two versions of the GC it uses in .NET. In the .NET world, the "background collector" is an enhancement over the "concurrent collector" in that it has less restrictions on what application threads can do while the collector is running.

基本 GC 使用stop-the-world"策略:应用线程从公共堆中分配内存块.当 GC 必须运行时(例如,分配了太多块,需要进行一些清理),所有应用(托管)线程停止.最后一个停止线程运行 GC,并在完成后解除所有其他线程的阻塞.stop-the-world GC 实现起来很简单,但会导致用户可以感知到的暂停.

A basic GC uses a "stop-the-world" strategy: applicative threads allocate memory blocks from a common heap. When the GC must run (e.g. too many blocks have been allocated, some cleanup is needed), all applicative (managed) threads stop. The last stopping thread runs the GC, and unblocks all the other threads when it has finished. A stop-the-world GC is simple to implement but induces pauses which can be perceptible at the user level.

Microsoft 的并发 GC"是分代的:它仅对堆的有限部分(他们称之为第 0 代和第 1 代")使用 stop-the-world 策略.由于该部分仍然很小,因此暂停仍然很短(例如低于 50 毫秒),因此用户不会注意到它们.堆的其余部分由专用 GC 线程收集,该线程可以与应用线程并发运行(因此得名).

Microsoft's "concurrent GC" is generational: it uses the stop-the-world strategy for only a limited part of the heap (what they call "generations 0 and 1"). Since that part remains small, pauses remain short (e.g. below 50ms), so that the user will not notice them. The rest of the heap is collected with a dedicated GC thread, which can run concurrently with the applicative threads (hence the name).

并发 GC 有一些限制.也就是说,有时 GC 线程必须承担对堆的某种排他性控制.在这种情况下,应用线程只能从小的线程特定区域分配块.有更大需求的线程很快就会发现主堆,此时主堆被 GC 线程锁定.然后分配线程必须阻塞,直到 GC 线程完成其锁堆阶段.这再次引起停顿.与 stop-the-world GC 相比,暂停更少,并且这些暂停不会影响所有线程.不过还是停了下来.

The concurrent GC has some limitations. Namely, there are moments when the GC thread must assume a somewhat exclusive control of the heap. During such times, applicative threads may allocate blocks only from small thread-specific areas. Threads which have bigger needs will soon stumble upon the main heap, which, at that time, is locked by the GC thread. The allocating thread must then block until the GC thread has finished its lock-the-heap phase. This again induces pauses. Less pauses than with a stop-the-world GC, and these pauses do not affect all threads. Yet pauses nonetheless.

后台 GC"是一种增强型 GC,其中 GC 线程不需要锁定堆.这消除了上一段中描述的额外暂停;只在收集年轻代时保持有限的暂停(微软称之为前台收集").

The "background GC" is an enhanced GC in which the GC thread needs not lock the heap. This removes the extra pauses described in the previous paragraph; only remain the limited pauses when the young generations are collected (what Microsoft calls "a foreground collection").

注意:并发 GC 和后台 GC 存在隐藏成本".为了让这些 GC 正常运行,来自应用线程的内存访问必须以一些非常特定的方式完成,这对性能有轻微影响.此外,GC 线程可能会对缓存内存产生不利影响,从而间接降低性能.对于不需要用户交互的纯计算任务,stop-the-world 收集器平均而言可能会产生更好的性能(例如,20 小时的计算将在 19 小时内完成).但这是一种边缘情况,在大多数情况下并发和后台 GC 更好.

Note: there are "hidden costs" with the concurrent GC and the background GC. For these GC to operate properly, memory accesses from applicative threads must be done in some very specific ways, which have a slight impact on performance. Also, the GC thread may have an adverse effect on cache memory, thus indirectly degrading performance. For a purely computational task with no need for user interaction, a stop-the-world collector may, on average, yield somewhat better performance (e.g. a twenty-hours-long computation will complete in nineteen hours). But this is an edge case, and in most situations the concurrent and background GC are better.

这篇关于后台垃圾回收和并发垃圾回收的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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