来自http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html ,我发现JVM记录生成计数在对象上:
At the next minor GC, the same thing happens for the eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. Once all surviving objects have been moved to S1, both S0 and eden are cleared. Notice we now have differently aged object in the survivor space.
然而,在《Thinking in Java 4th》第 124 页中,作者提到 JVM 记录在内存块上的生成计数:
As previously mentioned, in the JVM described here memory is allocated in big blocks. If you allocate a large object, it gets its own block. Strict stop-and-copy requires copying every live object from the source heap to a new heap before you can free the old one, which translates to lots of memory. With blocks, the garbage collection can typically copy objects to dead blocks as it collects. Each block has a generation count to keep track of whether it’s alive. In the normal case, only the blocks created since the last garbage collection are compacted; all other blocks get their generation count bumped if they have been referenced from somewhere. This handles the normal case of lots of short-lived temporary objects. Periodically, a full sweep is made—large objects are still not copied (they just get their generation count bumped), and blocks containing small objects are copied and compacted. The JVM monitors the efficiency of garbage collection and if it becomes a waste of time because all objects are long-lived, then it switches to mark-andsweep. Similarly, the JVM keeps track of how successful mark-and-sweep is, and if the heap starts to become fragmented, it switches back to stop-and-copy. This is where the “adaptive” part comes in, so you end up with a mouthful: “Adaptive generational stop-and-copy mark-andsweep.”
哪一个是正确的?或者也许他们只是说不同的话?请帮我弄清楚。
最佳答案
在 HotSpot JVM(Oracle 的 JVM)中,对象 header 中为对象年龄保留了一些位。位数可能会有所不同,但在任何配置中至少有 4 位可用。
因此,单独跟踪每个对象的年龄。对于 HotSpot JVM 上可用的所有收集器都是如此。
不过,其他供应商可能有不同的方法。例如。 Azul Zing 在内存块粒度上跟踪年龄。
关于java - JVM 是否记录对象或 block 的生成计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46487290/