當(dāng)使用stop-the-world 方式的GC在執(zhí)行時(shí),整個(gè)應(yīng)用會(huì)暫停住的。
而并發(fā)是指GC可以和應(yīng)用一起執(zhí)行,不用stop the world。
一般的說(shuō),并發(fā)GC可以做到大部分的運(yùn)行時(shí)間,是可以和應(yīng)用并發(fā)的,但還是有一些小任務(wù),不得不短暫的stop the world。
stop the world 的GC相對(duì)簡(jiǎn)單,因?yàn)?/font>heap被凍結(jié),對(duì)象的活動(dòng)也已經(jīng)停止。但缺點(diǎn)是可能不太滿足對(duì)實(shí)時(shí)性要求很高的應(yīng)用。
相應(yīng)的,并發(fā)GC的stop the world時(shí)間非常短,并且需要做一些額外的事情,因?yàn)椴l(fā)的時(shí)候,對(duì)象的引用狀態(tài)有可能發(fā)生改變的。
所以,并發(fā)GC需要花費(fèi)更多的時(shí)間,并且需要較大的heap。
年輕代的GC叫young GC,有時(shí)候也叫 minor GC。年老代或者永久代的GC,叫 full GC,也叫major GC。
也就是說(shuō),所有的代都會(huì)進(jìn)行GC。
一般的,首先是進(jìn)行年輕代的GC,(使用針對(duì)年輕代的GC),然后是年老代和永久代使用相同的GC。如果要壓縮(解決內(nèi)存碎片問(wèn)題),每個(gè)代需要分別壓縮。
有時(shí)候,如果年老區(qū)本身就已經(jīng)很滿了,滿到無(wú)法放下從survivor熬出來(lái)的對(duì)象,那么,YGC就不會(huì)再次觸發(fā),而是會(huì)使用FullGC對(duì)整個(gè)堆進(jìn)行GC(除了CMS這種GC,因?yàn)?/font>CMS不能對(duì)年輕代進(jìn)行GC)
4.4 快速分配內(nèi)存
多線程進(jìn)行對(duì)象建立的時(shí)候,在為對(duì)象分配內(nèi)存的時(shí)候,就應(yīng)該保證線程安全,為此,就應(yīng)該進(jìn)入全局鎖。但全局鎖是非常消耗性能的。
為此,HotSpot引入了Thread Local Allocation Buffers (TLAB)技術(shù),這種技術(shù)的原理就是為每個(gè)線程分配一個(gè)緩沖,用來(lái)分配線程自己的對(duì)象。
每個(gè)線程只使用自己的TLAB,這樣,就保證了不用使用全局鎖。當(dāng)TLAB不夠用的時(shí)候,才需要使用全局鎖。但這時(shí)候?qū)︽i的時(shí)候,頻率已經(jīng)相當(dāng)?shù)牡土恕?/font>
為了減少TLAB對(duì)空間的消耗,分配器也想了很多方法,平均來(lái)說(shuō),TLAB占用Eden區(qū)的不到1%。
關(guān)于-XX:+UseCMSInitiatingOccupancyOnly 和 -XX:CMSInitiatingOccupancyFraction ,詳細(xì)解釋見(jiàn)下:
The concurrent collection generally cannot be sped up but it can be started earlier.
A concurrent collection starts running when the percentage of allocated space in the old generation crosses a threshold. This threshold is calculated based on general experience with the concurrent collector. If full collections are occurring, the concurrent collections may need to be started earlier. The command line flag CMSInitiatingOccupancyFraction can be used to set the level at which the collection is started. Its default value is approximately 68%. The command line to adjust the value is
-XX:CMSInitiatingOccupancyFraction=<percent>
The concurrent collector also keeps statistics on the promotion rate into the old generation for the application and makes a prediction on when to start a concurrent collection based on that promotion rate and the available free space in the old generation. Whereas the use of CMSInitiatingOccupancyFraction must be conservative to avoid full collections over the life of the application, the start of a concurrent collection based on the anticipated promotion adapts to the changing requirements of the application. The statistics that are used to calculate the promotion rate are based on the recent concurrent collections. The promotion rate is not calculated until at least one concurrent collection has completed so at least the first concurrent collection has to be initiated because the occupancy has reached CMSInitiatingOccupancyFraction . Setting CMSInitiatingOccupancyFraction to 100 would not cause only the anticipated promotion to be used to start a concurrent collection but would rather cause only non-concurrent collections to occur since a concurrent collection would not start until it was already too late. To eliminate the use of the anticipated promotions to start a concurrent collection set UseCMSInitiatingOccupancyOnly to true.
-XX:+UseCMSInitiatingOccupancyOnly
關(guān)于內(nèi)存管理完整詳細(xì)信息,請(qǐng)查看文檔