简体   繁体   中英

Memory allocation in G1 GC

As I understand when using G1 GC heap is partitioned into a set of equal-sized heap regions.
How JVM allocates new objects in the regions? Which region is choosed for the allocation?

I guess the reason for multiple Edens is that they're thread-local. 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.

IIUIC the G1 differs in how it selects the areas to be collected, not in how it allocates.

From the 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. This provides greater flexibility in memory usage.

Also check 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. 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 . So, that means that object allocation happens in the Young Generation (Eden space) for the usual case. From this perspective, there is nothing new in G1. The difference between G1 and eg CMS is that the Young Gen is split into several equally sized regions.

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.

Humongous object allocation happens in the humongous regions - this is a special case of allocation for large objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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