java - heap为什么要分为Eden、Survivor spaces和Old Generation?

标签 java garbage-collection jvm heap-memory g1gc

你能回答我一个关于 JVM 垃圾收集过程的问题吗?

堆为什么分为Eden、Survivor空间和Old Generation?

当一个年轻的疏散被处理时,通过从根开始的引用来访问对象,以找出无法访问的对象。可到达的对象被标记为“存活”,不可到达的对象不被标记并将被淘汰。

因此,所有 对象都会被考虑,包括在老年代分配的对象也会被访问并标记它们是否可达。

据我所知,同时回收年轻一代和老一代是一项要求很高的工作,因为这些一代位于内存的不同连续部分。

但是,即使在 Young 疏散级别上进行了最简单的标记之后,如果所有可到达和不可到达的对象都是已知的并且可以删除,我们就拥有了包含所有 Activity 对象和死对象的整个位图,那么为什么我们需要这种划分呢?

我也知道弱代假设但是为什么我们需要划分?

最佳答案

基本前提是,当创建新对象时,不存在从旧对象到新对象的引用,并且对于很多对象,甚至大多数对象,这永远不会改变。这意味着如果您可以证明仍然没有从旧对象到新对象的引用,或者当您准确知道已创建哪些引用时,您可以只扫描年轻代进行“次要”垃圾回收。

这意味着必须跟踪和记住对旧对象的引用更改(但请记住这样的更改不会经常发生的前提)。

一个实现策略是Card Marking :

If a garbage collector does not collect the entire heap (an incremental collection), the garbage collector needs to know where there are pointers from the uncollected part of the heap into the part of the heap that is being collected. This is typically for a generational garbage collector in which the uncollected part of the heap is usually the old generation, and the collected part of the heap is the young generation. The data structure for keeping this information (old generation pointers to young generation objects), is a remembered set. A card table is a particular type of remembered set. Java HotSpot VM uses an array of bytes as a card table. Each byte is referred to as a card. A card corresponds to a range of addresses in the heap. Dirtying a card means changing the value of the byte to a dirty value; a dirty value might contain a new pointer from the old generation to the young generation in the address range covered by the card.

Processing a card means looking at the card to see if there is an old generation to young generation pointer and perhaps doing something with that information such as transferring it to another data structure.

当然,使用分代只会提供一个好处,如果它使我们能够在扫描期间跳过某些内存区域,并且如果维护这些 memset 不会超过节省的话。

关于java - heap为什么要分为Eden、Survivor spaces和Old Generation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47504142/

相关文章:

java - java 中奇怪的 char[] siu 。无法复制值并跳过空格

Java 垃圾收集引用类

flash - 在要进行 GC 的对象中将引用设置为 null?

java - 在java中将 "0.25000%"转换为double

java - ImageIO 无法解析为类型

memory-management - 为什么要在网络应用程序中进行垃圾收集?

java - Firefox ESR - 在两个不同选项卡中加载同一个小程序两次时,仅打开一个虚拟机

java - 堆大小参数 xmx 被覆盖

Java EE - 具有许多 Web 应用程序的应用程序服务器的 JVM

java - 以十六进制读入 Java