首页 > 编程知识 正文

g1垃圾回收器缺点,垃圾收集器为什么用G1

时间:2023-05-03 11:49:47 阅读:15007 作者:926

此内存区域被分割为固定大小的区域。

区域大小是在JVM启动时确定的。 JVM通常有2,000个大小不同的32Mb空间。

thecolorsinthepictureshowswhichregionisassociatedwithwhichrole.liveobjectsareevacuated (I.e . copiedormoved (fromoneregiontoanother.regionsaredesignedtobecollectedinparallelwithorwithoutstoppingallotherapplicationthretionthrethretionthrer

asshownregionscanbeallocatedintoeden,survivor,andoldgenerationregions.in addition, thereisafourthtypeofobjectknownashumongousregions.theseregionsaredesignedtoholdobjectsthatare 50 % thesizeofastandardregionor fcontiguousregions.finallythelasttypeofregionswouldbetheunusedareasoftheheap。

note : at the time of this writing,collectinghumongousobjectshasnotbeenoptimized.therefore,youshouldavoidcreatingobjectsofted

theheapissplitintoapproximately 2000 regions.minimumsizeis1mbandmaximumsizeis 32m b.blueregionsholdoldgenerationobjectsandgreeeis

notethattheregionsarenotrequiredtobecontiguousliketheolderjjdds。

liveobjectsareevacuated(I.e .copied or moved ) tooneormoresurvivorregions.iftheagingthresholdismet,someoftheobjectsarrred

thisisastoptheworld(STW ) )。 pause.edensizeandsurvivorsizeiscalculatedforthenextyounggc.accountinginformationiskepttohelpcalculatethesize.thingsliketethete

thisapproachmakesitveryeasytoresizeregions,makingthembiggerorsmallerasneeded。

endofayounggcwithxnd/strongkddw/pliveobjectshavebeenevacuatedtosurvivorregionsortooldgenerationregions。

recentlypromotedobjectsareshownindarkbl

ue. Survivor regions in green.

In summary, the following can be said about the young generation in G1:

The heap is a single memory space split into regions.Young generation memory is composed of a set of non-contiguous regions. This makes it easy to resize when needed.Young generation garbage collections, or young GCs, are stop the world events. All application threads are stopped for the operation.The young GC is done in parallel using multiple threads.Live objects are copied to new survivor or old generation regions. Old Generation Collection with G1

Like the CMS collector, the G1 collector is designed to be a low pause collector for old generation objects. The following table describes the G1 collection phases on old generation.

G1 Collection Phases - Concurrent gxdlsing Cycle Phases

The G1 collector performs the following phases on the old generation of the heap. Note that some phases are part of a young generation collection.

PhaseDescription(1) Initial gxdls
(Stop the World Event)This is a stop the world event. With G1, it is piggybacked on a normal young GC. gxdls survivor regions (root regions) which may have references to objects in old generation.(2) Root Region ScanningScan survivor regions for references into the old generation. This happens while the application continues to run. The phase must be completed before a young GC can occur.(3) Concurrent gxdlsingFind live objects over the entire heap. This happens while the application is running. This phase can be interrupted by young generation garbage collections.(4) Remark
(Stop the World Event)Completes the marking of live object in the heap. Uses an algorithm called snapshot-at-the-beginning (SATB) which is much faster than what was used in the CMS collector.(5) Cleanup
(Stop the World Event and Concurrent) Performs accounting on live objects and completely free regions. (Stop the world)Scrubs the Remembered Sets. (Stop the world)Reset the empty regions and return them to the free list. (Concurrent)(*) Copying
(Stop the World Event)These are the stop the world pauses to evacuate or copy live objects to new unused regions. This can be done with young generation regions which are logged as htddx pause (young)]. Or both young and old generation regions which are logged as htddx Pause (mixed)]. G1 Old Generation Collection Step by Step

With the phases defined, let's look at how they interact with the old generation in the G1 collector.

Initial gxdlsing Phase

Initial marking of live object is piggybacked on a young generation garbage collection. In the logs this is noted as GC pause (young)(inital-mark).

Concurrent gxdlsing Phase

If empty regions are found (as denoted by the "X"), they are removed immediately in the Remark phase. Also, "accounting" information that determines liveness is calculated.

Remark Phase

Empty regions are removed and reclaimed. Region liveness is now calculated for all regions.

Copying/Cleanup Phase After Copying/Cleanup Phase

The regions selected have been collected and compacted into the dark blue region and the dark green region shown in the diagram.

Summary of Old Generation GC

In summary, there are a few key points we can make about the G1 garbage collection on the old generation.

Concurrent gxdlsing Phase Liveness information is calculated concurrently while the application is running.This liveness information identifies which regions will be best to reclaim during an evacuation pause.There is no sweeping phase like in CMS.Remark Phase Uses the Snapshot-at-the-Beginning (SATB) algorithm which is much faster then what was used with CMS.Completely empty regions are reclaimed.Copying/Cleanup Phase Young generation and old generation are reclaimed at the same time.Old generation regions are selected based on their liveness.

【参考资料】

1. Oarcle . http://www.oracle.com/technetwork/tutorials/tutorials-1876574.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。