简体   繁体   English

G1 GC中的内存分配

[英]Memory allocation in G1 GC

As I understand when using G1 GC heap is partitioned into a set of equal-sized heap regions. 据我所知,当使用G1时,GC会被分割成一组大小相等的堆区域。
How JVM allocates new objects in the regions? JVM如何在区域中分配新对象? Which region is choosed for the allocation? 选择哪个区域进行分配?

I guess the reason for multiple Edens is that they're thread-local. 我想多个Edens的原因是它们是线程本地的。 Something like this is most probably used by other collectors too, as allocation needs to be fast and dealing with shared variables is slow and scales badly. 像这样的东西很可能也被其他收藏家使用,因为分配需要很快并且处理共享变量很慢并且规模很大。 When such an Eden gets exhausted, some synchronization is needed in order to get a new chunk of VM. 当这样的Eden耗尽时,需要一些同步才能获得一大块VM。

IIUIC the G1 differs in how it selects the areas to be collected, not in how it allocates. IIUIC G1在选择要收集的区域方面有所不同,而不是如何分配区域。

From the Oracle Docs : 来自Oracle Docs

在此输入图像描述

The heap is partitioned into a set of equal-sized heap regions, each a contiguous range of virtual memory. 堆被分区为一组大小相等的堆区域,每个区域都是一个连续的虚拟内存区域。 Certain region sets are assigned the same roles (eden, survivor, old) as in the older collectors, but there is not a fixed size for them. 某些区域集具有与旧收集器中相同的角色(eden,survivor,old),但它们没有固定的大小。 This provides greater flexibility in memory usage. 这为内存使用提供了更大的灵活性。

Also check Garbage First Garbage Collector Tuning 还要检查Garbage First Garbage Collector Tuning

Here is what the original Garbage-First Garbage Collection Research Paper says about allocation: 以下是原始垃圾收集研究论文关于分配的内容:

Allocation in a heap region consists of incrementing a boundary, top , between allocated and unallocated space. 堆区域中的分配包括在分配和未分配空间之间递增边界顶部 One region is the current allocation region from which storage is being allocated. 一个区域是分配存储的当前分配区域 Since we are mainly concerned with multiprocessors, mutator threads allocate only thread-local allocation buffers , or TLABs , directly in this heap region, using a compare-and-swap , or CAS, operation. 由于我们主要关注多处理器,因此mutator线程使用比较和交换或CAS操作直接在此堆区域中分配线程局部分配缓冲区TLAB They then allocate objects privately within those buffers, to minimize allocation contention. 然后,他们在这些缓冲区中私下分配对象,以最大限度地减少分配争用。 When the current allocation region is filled, a new allocation region is chosen. 当填充当前分配区域时,选择新的分配区域。 Empty regions are organized into a linked list to make region allocation a constant time operation. 空区域被组织成链表以使区域分配成为恒定时间操作。

In general, you need to realize that G1 is still generational garbage collector . 一般来说,你需要意识到G1仍然 世代垃圾收集器 So, that means that object allocation happens in the Young Generation (Eden space) for the usual case. 因此,这意味着对象分配发生在通常情况下的Young Generation(Eden空间)中。 From this perspective, there is nothing new in G1. 从这个角度来看,G1没有什么新东西。 The difference between G1 and eg CMS is that the Young Gen is split into several equally sized regions. G1和例如CMS之间的区别在于Young Gen被分成几个大小相等的区域。

The Eden regions are collected in a stop-the-world pause and the objects are compacted into the To space , so it is not really a problem having those object allocated all over the different Eden regions. Eden区域在世界停留时收集的,并且对象被压缩到To空间中 ,因此将这些对象分配到不同的Eden区域并不是真正的问题。

Humongous object allocation happens in the humongous regions - this is a special case of allocation for large objects. 堆积如山的对象分配发生在堆积如山的地区-这是分配一个大对象的特殊情况。

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

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