內存分配策略

大對象直接進入老年代
大對象就是指需要大量連續空間的java對象,寫程序時應該避免“短命的大對象”
可根據-XX:PretenureSizeThreshold來設置多大的對象直接進入老年代,但這東西只對Serial和ParNew收集器有效

長期存活的對象進入老年代
虛擬機爲每個對象定義一個年齡計數器
在第一次Minor GC後仍然存活, 將對象移入Survivor空間年齡設爲1。
今後每執行一次Minor GC,年齡加1。加到一定程度(-XX:MaxTenuringThreshold 默認15),晉升到老年代。
VM : -verbose:gc - Xms20M -Xmx20M -Xmn10M - XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution -XX:+PrintGCDetails
byte[] allocation1, allocation2, allocation3;
allocation1 = new byte [_1MB / 4];
allocation2 = new byte [4 * _1MB];
allocation3 = new byte [4 * _1MB];
allocation3 = null;
allocation3 = new byte [4 * _1MB];
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age 1: 413416 bytes, 413416 total
: 4695K->403K(9216K), 0.0028899 secs] 4695K->4499K(19456K), 0.0029114 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
: 4499K->0K(9216K), 0.0006270 secs] 8595K->4499K(19456K), 0.0006456 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4423K [0x040e0000, 0x04ae0000, 0x04ae0000)
eden space 8192K, 54% used [0x040e0000, 0x04531fa8, 0x048e0000)
from space 1024K, 0% used [0x048e0000, 0x048e0000, 0x049e0000)
to space 1024K, 0% used [0x049e0000, 0x049e0000, 0x04ae0000)
tenured generation total 10240K, used 4499K [0x04ae0000, 0x054e0000, 0x054e0000)
the space 10240K, 43% used [0x04ae0000, 0x04f44ef8, 0x04f45000, 0x054e0000)
compacting perm gen total 12288K, used 2111K [0x054e0000, 0x060e0000, 0x094e0000)
the space 12288K, 17% used [0x054e0000, 0x056effe0, 0x056f0000, 0x060e0000)
No shared spaces configured.
VM : - verbose:gc - Xms20M -Xmx20M - Xmn10M - XX:SurvivorRatio=8 - XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution - XX:+PrintGCDetails
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age 1: 413416 bytes, 413416 total
: 4695K->403K(9216K), 0.0026816 secs] 4695K->4499K(19456K), 0.0027035 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
- age 2: 413416 bytes, 413416 total
: 4499K->403K(9216K), 0.0005062 secs] 8595K->4499K(19456K), 0.0005261 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4827K [0x04130000, 0x04b30000, 0x04b30000)
eden space 8192K, 54% used [0x04130000, 0x04581fa8, 0x04930000)
from space 1024K, 39% used [0x04930000, 0x04994ee8, 0x04a30000)
to space 1024K, 0% used [0x04a30000, 0x04a30000, 0x04b30000)
tenured generation total 10240K, used 4096K [0x04b30000, 0x05530000, 0x05530000)
the space 10240K, 40% used [0x04b30000, 0x04f30010, 0x04f30200, 0x05530000)
compacting perm gen total 12288K, used 2111K [0x05530000, 0x06130000, 0x09530000)
the space 12288K, 17% used [0x05530000, 0x0573ffe0, 0x05740000, 0x06130000)
No shared spaces configured.

動態年齡判斷,就是說相同年齡所有對象的大小相加的總和大於Survivor的一半,那年齡大於或等於這個年齡的對象就直接進入老年代
VM : - verbose:gc - Xms20M -Xmx20M - Xmn10M - XX:SurvivorRatio=8 - XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution - XX:+PrintGCDetails
byte[] allocation1, allocation2, allocation3, allocation4;
allocation1 = new byte [_1MB /6+12*1024];
allocation2 = new byte [_1MB / 6+12*1024];

allocation3 = new byte [4 * _1MB];
allocation4 = new byte [4 * _1MB];
allocation4 = null;
allocation4 = new byte [4 * _1MB];
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 15)
- age 1: 525384 bytes, 525384 total
: 4805K->513K(9216K), 0.0026955 secs] 4805K->4609K(19456K), 0.0027157 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 15 (max 15)
: 4609K->0K(9216K), 0.0005254 secs] 8705K->4609K(19456K), 0.0005400 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4423K [0x04250000, 0x04c50000, 0x04c50000)
eden space 8192K, 54% used [0x04250000, 0x046a1fa8, 0x04a50000)
from space 1024K, 0% used [0x04a50000, 0x04a50000, 0x04b50000)
to space 1024K, 0% used [0x04b50000, 0x04b50000, 0x04c50000)
tenured generation total 10240K, used 4609K [0x04c50000, 0x05650000, 0x05650000)
the space 10240K, 45% used [0x04c50000, 0x050d0458, 0x050d0600, 0x05650000)
compacting perm gen total 12288K, used 2112K [0x05650000, 0x06250000, 0x09650000)
the space 12288K, 17% used [0x05650000, 0x05860010, 0x05860200, 0x06250000)
No shared spaces configured.

空間分配擔保
在發生Minor GC虛擬機會統計進入老年代的平均大小是否大於剩餘空間,如果大於,直接Full GC,如果小於,查看HandlePromotionFailure設置是否允許擔保失敗。
如果允許,進行Minor GC ,不允許,進行Full GC。
當HandlePromotionFailure失敗,只好Full GC
建議HandlePromotionFailure打開,避免Full GC過於頻繁
發佈了28 篇原創文章 · 獲贊 0 · 訪問量 938
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章