简体   繁体   English

MaxTenuringThreshold - 它究竟是如何工作的?

[英]MaxTenuringThreshold - how exactly it works?

We know that there's few main memory domains: Young, Tenured (Old gen) and PermGen.我们知道很少有主内存域:Young、Tenured(老一代)和 PermGen。

  • Young domain is divided into Eden and Survivor (with two). Young域分为Eden和Survivor(有两种)。
  • OldGen is for surviving objects. OldGen 用于幸存的对象。

MaxTenuringThreshold keeps objects from being finally copied to the OldGen space too early. MaxTenuringThreshold 可防止对象过早地最终复制到 OldGen 空间。 It's pretty clear and understandable.这是非常清楚和易于理解的。

But how does it work?但它是如何工作的? How is the garbage collector dealing with these objects which are still surviving till MaxTenuringThreshold and in what way?垃圾收集器如何处理这些在 MaxTenuringThreshold 之前仍然存活的对象?以什么方式? Where are they located?它们位于何处?

Objects are being copied back to Survivor spaces for garbage collection.. or does it happen somehow else?对象正在被复制回 Survivor 空间进行垃圾收集......还是以其他方式发生?

Each object in Java heap has a header which is used by Garbage Collection (GC) algorithm. Java 堆中的每个对象都有一个由垃圾收集 (GC) 算法使用的标头。 The young space collector (which is responsible for object promotion) uses a few bit(s) from this header to track the number of collections object that have survived (32-bit JVM use 4 bits for this, 64-bit probably some more).年轻空间收集器(负责对象提升)使用此标头中的一些位来跟踪幸存的集合对象的数量(32 位 JVM 为此使用 4 位,64 位可能更多) .

During young space collection, every single object is copied.在年轻空间收集期间,每个对象都被复制。 The Object may be copied to one of survival spaces (one which is empty before young GC) or to the old space.对象可以复制到生存空间之一(在年轻 GC 之前为空的空间)或旧空间。 For each object being copied, GC algorithm increases it's age (number of collection survived) and if the age is above the current tenuring threshold it would be copied (promoted) to old space.对于每个被复制的对象,GC 算法会增加它的年龄(存活的集合数量),如果年龄高于当前的任期阈值,它将被复制(提升)到旧空间。 The Object could also be copied to the old space directly if the survival space gets full (overflow).如果生存空间已满(溢出),也可以将对象直接复制到旧空间。

The journey of Object has the following pattern: Object 的旅程具有以下模式:

  • allocated in eden分配在伊甸园
  • copied from eden to survival space due to young GC由于年轻 GC 从伊甸园复制到生存空间
  • copied from survival to (other) survival space due to young GC (this could happen few times)由于年轻的 GC 从生存空间复制到(其他)生存空间(这可能发生几次)
  • promoted from survival (or possible eden) to old space due to young GC (or full GC)由于年轻GC(或完整GC),从生存(或可能的伊甸园)提升到旧空间

the actual tenuring threshold is dynamically adjusted by JVM, but MaxTenuringThreshold sets an upper limit on it.实际的任期阈值由 JVM 动态调整,但 MaxTenuringThreshold 对其设置了上限。

If you set MaxTenuringThreshold=0, all objects will be promoted immediately.如果设置 MaxTenuringThreshold=0,则所有对象将立即提升。

I have few articles about java garbage collection, there you can find more details.我有几篇关于java垃圾收集的文章,你可以在那里找到更多细节。

(Disclaimer: This covers HotSpot VM only) (免责声明:这仅涵盖 HotSpot VM)

As Alexey states, the actually used tenuring threshold is determined by the JVM dynamically.正如 Alexey 所说,实际使用的任期阈值是由 JVM 动态确定的。 There is very little value in setting it.设置它的价值很小。 For most applications the default value of 15 will be high enough, as usually way more object survive the collection.对于大多数应用程序,默认值 15 已经足够高了,因为通常会有更多的对象在集合中存活下来。 When many objects survive the collection, the survivor spaces overflow directly to old.当许多对象在集合中幸存下来时,幸存者空间直接溢出到旧的。 This is called premature promotion and an indicator of a problem.这称为过早提升和问题的指标。 However it seldom can be solved by tuning MaxTenuringThreshold.然而,它很少可以通过调整 MaxTenuringThreshold 来解决。

In those cases sometimes SurvivorRatio might be used to increase the space in the survivor spaces, allowing the tenuring to actually work.在这些情况下,有时可能会使用 SurvivorRatio 来增加幸存者空间中的空间,从而使任期真正起作用。 However, most often enlarging the young generation is the only good choice (from configuration perspective).然而,通常扩大年轻代是唯一好的选择(从配置的角度来看)。 If you are looking from coding perspective, you should avoid excess object allocation to let tenuring work as designed.如果您是从编码的角度来看,您应该避免过多的对象分配,让任期按设计工作。

To answer exactly what you asked: When an object reaches its JVM determinded tenuring threshold, it is copied to old.准确回答您的问题:当一个对象达到其 JVM 确定的任期阈值时,它会被复制到旧的。 Before that, it will be copied to the empty survivor space.在此之前,它将被复制到空的幸存者空间。 Objects that have been surviving a while but are de-referenced before reaching the threshold are cleaned from survivor very efficiently.已经存活一段时间但在达到阈值之前被取消引用的对象会非常有效地从幸存者中清除。

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

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