问题描述
我不明白垃圾收集上下文中的世代"是什么.谁能简单的解释一下?
I don't understand what "generations" are in the context of Garbage Collection. Can someone explain in simple terms?
推荐答案
来自 了解 .NET 中的垃圾回收
世代
分代垃圾收集器多收集短命的对象经常比寿命更长的.短期对象存储在第一代,0代.寿命更长的物体被推入更高的世代,1 或 2.垃圾收集器工作更多经常在低代比更高的.
A generational garbage collector collects the short-lived objects more frequently than the longer lived ones. Short-lived objects are stored in the first generation, generation 0. The longer-lived objects are pushed into the higher generations, 1 or 2. The garbage collector works more frequently in the lower generations than in the higher ones.
当一个对象第一次被创建时,它是放入第 0 代.当第0代被填满,垃圾收集器被调用.那些对象在垃圾收集中幸存下来第一代被提升到下一代更高一代,第 1 代.在垃圾中幸存的对象第 1 代的收集是晋升到下一个和最高的一代,2代.这算法有效地适用于对象的垃圾收集,因为它很快.请注意,第 2 代是最高的一代由垃圾收集器支持.
When an object is first created, it is put into generation 0. When the generation 0 is filled up, the garbage collector is invoked. The objects that survive the garbage collection in the first generation are promoted onto the next higher generation, generation 1. The objects that survive garbage collection in generation 1 are promoted onto the next and the highest generation, generation 2. This algorithm works efficiently for garbage collection of objects, as it is fast. Note that generation 2 is the highest generation that is supported by the garbage collector.
.NET 中的垃圾收集
世代
内存分配时托管堆很快,GC本身可能需要一些时间.考虑到这一点进行了多项优化以提高性能.GC支持世代的概念,基于这样的假设一个对象在堆上的时间更长,它可能会停留的时间越长那里.当一个对象被分配到它属于第 0 代的堆.每个垃圾收集器对象生存增加其1代(目前最高支持的世代是 2).明显地搜索速度更快,并且垃圾收集所有的子集堆上的对象,所以 GC 有仅收集一代的选项0、1 或 2 个对象(或其他它选择的组合,直到它有足够的内存).即使在GC 只收集较年轻的对象还可以确定旧对象是否有对新对象的引用以确保它不会无意中忽略正在使用的对象.
While memory allocation on the managed heap is fast, GC itself may take some time. With this in mind several optimisations have been made to improve performance. The GC supports the concept of generations, based on the assumption that the longer an object has been on the heap, the longer it will probably stay there. When an object is allocated on the heap it belongs in generation 0. Each garbage collection that that object survives increases its generation by 1 (currently the highest supported generation is 2). Obviously it's faster to search through, and garbage collect a subset of all objects on the heap, so the GC has the option of collecting only generation 0, 1 or 2 objects (or whatever combination it chooses until it has sufficient memory). Even while collecting only younger objects the GC can also determine if old objects have references to new objects to ensure that it doesn't inadvertently ignore in-use objects.
这篇关于垃圾收集的世代是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!