为什么堆分为伊甸园、幸存者空间和老年代?
你能回答我一个关于 JVM 垃圾收集过程的问题吗?
Could you please answer me a question about JVM Garbage Collection process?
为什么堆分为伊甸园、幸存者空间和老年代?
Why is heap divided into Eden, Survivor spaces and Old Generation?
在处理年轻疏散时,通过从根开始的引用访问对象以找出无法访问的对象.可达对象被标记为活动",不可达对象不被标记,将被淘汰.
When a young evacuation is processed objects are visited through references starting from the roots to find out unreachable ones. Reachable objects are marked as ‘alive’ and unreachable are not marked and will be eliminated.
因此,考虑 ALL 个对象,包括在老年代分配的对象,如果它们可达,也会被访问和标记.
As a result, ALL objects are considered, including objects allocated in Old Generation are also visited and marked if they are reachable.
据我了解,一次回收年轻代和老代要求很高,因为这些代位于内存的不同连续部分.
As I understand reclaiming Young Generation and Old Generation at once is demanding because these generations are located in different contiguous parts of memory.
但是,即使在 Young 疏散级别上进行了最简单的标记之后,如果所有可到达和不可到达的对象都已知并且可以删除,那么为什么我们需要这种划分?
But why do we need this division if even after the simplest marking on the Young evacuation level we have the entire bitmap with all alive and dead objects if all reachable and unreachable objects are known and can be deleted?
我也知道弱代际假设,但为什么我们需要分裂?
I also know weak generational hypothesis about but why do we need the division?
推荐答案
基本前提是在创建新对象时,不存在旧对象对新对象的引用,而且对于很多对象,甚至大部分他们,这永远不会改变.这意味着,如果您可以证明仍然没有从旧对象到新对象的引用,或者当您确切知道创建了哪些引用时,您可以只对年轻代进行次要"垃圾回收.
The basic premise is that when new objects are created, no reference from an old object to the new one exists and for a lot of objects, or even most of them, this never changes. This implies that you can do a "minor" garbage collection scanning the young generation only, if you can prove that there are still no references from old objects to new objects or when you know precisely which references have been created.
这意味着必须跟踪和记住对旧对象的引用更改(但请记住此类更改不会经常发生的前提).
This implies that reference changes to old objects must be tracked and remembered (but recall the premise that such changes don’t happen so often).
一种实施策略是卡标记:
如果垃圾收集器没有收集整个堆(增量收集),则垃圾收集器需要知道从堆的未收集部分到堆的部分的指针在哪里正在收集.这通常用于分代垃圾收集器,其中堆的未收集部分通常是老年代,而堆中已收集的部分是年轻代.保存这些信息的数据结构(指向年轻代对象的老年代指针)是一个记忆集.卡片表是一种特殊类型的记忆集.Java HotSpot VM 使用字节数组作为卡片表.每个字节被称为一个卡片.一张卡片对应于堆中的一系列地址.Dirtying a card 表示将字节的值更改为 dirty 值;脏值可能包含卡覆盖的地址范围内从老年代到年轻代的新指针.
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.
当然,使用生成只提供一个好处,如果它使我们能够在扫描期间跳过某些内存区域并且维护这些记忆集不会超过节省的成本.
Of course, using generations only provides a benefit, if it enables us to skip certain memory regions during the scan and if maintaining these remembered sets does not outweigh the savings.
相关文章