简体   繁体   English

生成如何帮助垃圾收集器?

[英]How generation help garbage collector?

I've read few articles about how garbage collection works and still don't understand how using generations helps? 我读过几篇有关垃圾回收工作原理的文章,但仍不了解使用世代有何帮助? As I understood the main idea is that we start collection from the youngest generation and move to older generations. 据我了解,主要思想是,我们从最年轻的一代开始收集产品,然后转向老一辈。 But why the authors of this idea decided that starting from the youngest generation is the most efficient way? 但是,为什么这个想法的作者决定从最年轻的一代开始是最有效的方法?

The older the generation, means object has been used quite a many times, and possibly will need again. 较老的一代意味着对象已经使用了很多次,并且可能会再次需要。 Removing recently created object makes no sense, May be its temporary(scope : local) object. 删除最近创建的对象没有任何意义,可能是其临时(作用域:本地)对象。

The authors start with the youngest generation first simply because that's what gets filled up first after your application starts, however in reality which generation is being swept and when is non-deterministic as your application runs. 作者首先从最年轻的一代开始,这仅仅是因为那是您的应用程序启动后首先被填充的东西,但是实际上,哪一代正在席卷而何时不确定何时运行您的应用程序。

The important points with generational GC are: 世代GC的重点是:

  • the young generation uses a copying collector which is copying objects to a space that it considers to be empty (the unused survivor spaces) from eden and the current survivor space and is therefore fast and the GC pause is minimal. 年轻一代使用复制收集器,该复制器将对象复制到它认为来自eden和当前幸存者空间为空的空间(未使用的幸存者空间),因此速度很快且GC暂停最小。

  • add to this fact that most objects die young and therefore the pause required to copy a small number of surviving objects from the eden and the current surviver space is small as only objects with live references are copied, after which eden and the previous survivor space can be wiped. 此外,大多数对象会早逝,因此从伊甸园复制少量幸存对象所需的停顿时间很小,因为仅复制具有实时引用的对象,因此伊甸园和先前的幸存者空间可以复制,因此当前的幸存者空间很小被擦拭。

  • after being copied several times objects are copied to the tenured (old) generation; 在多次复制对象之后,将对象复制到保有期限(旧)的代 Eventually the tenured generation will fill up, however, this time there's not a clean space to copy the objects to, so the garbage collector has to sweap and compact within the generation, which is slow (when compared to the copy performed in eden and the survivor space) meaning a longer pause. 最终使用权的代将填满,但是,这一次没有干净的空间可将对象复制到其中,因此垃圾收集器必须在代内进行交换和压缩,这很慢(与在eden和幸存者空间)意味着更长的暂停时间。

  • the good news, based on the most objects die young heuristic is, major GCs happen much less frequently than minor keeping GC pauses to a minimum over the lifetime of an application. 好消息是,基于大多数启发式对象,而不是次要启发式操作,大型GC发生的频率要比次要GC发生的频率要低得多,从而使GC在应用程序生命周期内的暂停时间降至最低。

  • there's also a benefit that all new objects are allocated on the top of the heap, meaning there's mininal instructions required to do so, with defragmentation occurring naturally as part of the copy process. 还有一个好处是,所有新对象都分配在堆的顶部,这意味着需要这样做的基本指示,在复制过程中自然会进行碎片整理。

Both these pages, Oracle Garbage Collection Tuning and Useful JVM Flags – Part 5 (Young Generation Garbage Collection) , describe this. 这两个页面( Oracle垃圾收集优化有用的JVM标志–第5部分(年轻一代垃圾收集))都对此进行了描述。

Read this one . 阅读这一个

Using different generations, makes the allocation of objects easy and fast as MOST of the allocations are done in a single region of Heap - Eden. 使用不同的世代,使对象的分配变得容易和快速,因为大多数分配都是在Heap-Eden的单个区域中完成的。 Based on the observation that most objects die young from Weak Generational Hypothesis, collections in Young generation have more garbage which will reclaim more memory and its relatively small compared to the heap which means that time taken to scan the objects is also less. 根据观察,大多数对象都死于弱代假说,年轻代中的集合具有更多的垃圾,这些垃圾将回收更多的内存,并且与堆相比,垃圾相对较小,这意味着扫描对象所需的时间也更少。 Thats why Young generation GCs are fast. 这就是为什么年轻一代GC速度快的原因。

For more details on GC and generations, you can refer to this 有关GC和世代的更多详细信息,您可以参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM