深入理解java虛擬機(一):java內存區域(內存結構劃分)
深入理解java虛擬機(二):java內存溢出實戰
深入理解java虛擬機(三):String.intern()-字符串常量池
深入理解java虛擬機(四):對象存活判定算法和垃圾收集算法
深入理解java虛擬機(五):hotspot垃圾收集算法實現
深入理解java虛擬機(六):java垃圾收集分析實戰(內存分配與回收策略)
深入理解java虛擬機(七):java垃圾收集分析總結
深入理解java虛擬機(八):java內存分析工具-MAT和OQL
試驗環境jdk 1.6.0_37。
虛擬機參數-XX:PrintGCDetails讓虛擬機在發生垃圾回收行爲時打印內存回收日誌,並在進程退出時候輸出當前的內存各區域分配情況。
以下代碼測試都將加上 -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 參數,即設置java堆大小限制爲20M,新生代10M,Eden區和Survivor區的
比例是8,即Eden區爲8M,每個Survivor區大小爲1M,並打印垃圾回收日誌。
一、對象優先在Eden分配
在大多數情況下,對象優先分配在Eden區,當Eden區沒有足夠的空間進行分配時,虛擬機將發起一次Minor GC。
/**
* VM參數:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
* 限制java堆大小爲20M,年輕代爲10M,Eden爲8M,兩個survivor分別爲1M
*/
public static void testAllocation() {
byte[] allocation1, allocation2, allocation3, allocation4;
allocation1 = new byte[2 * _1MB];
allocation2 = new byte[2 * _1MB];
allocation3 = new byte[2 * _1MB];
allocation4 = new byte[4 * _1MB]; // 出現一次Minor GC
/**
[GC [DefNew: 6487K->152K(9216K), 0.0040116 secs] 6487K->6296K(19456K), 0.0040436 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4576K [0x32750000, 0x33150000, 0x33150000)
eden space 8192K, 54% used [0x32750000, 0x32ba1fa8, 0x32f50000)
from space 1024K, 14% used [0x33050000, 0x33076150, 0x33150000)
to space 1024K, 0% used [0x32f50000, 0x32f50000, 0x33050000)
tenured generation total 10240K, used 6144K [0x33150000, 0x33b50000, 0x33b50000)
the space 10240K, 60% used [0x33150000, 0x33750030, 0x33750200, 0x33b50000)
compacting perm gen total 12288K, used 376K [0x33b50000, 0x34750000, 0x37b50000)
the space 12288K, 3% used [0x33b50000, 0x33bae2c0, 0x33bae400, 0x34750000)
ro space 10240K, 55% used [0x37b50000, 0x380d1140, 0x380d1200, 0x38550000)
rw space 12288K, 55% used [0x38550000, 0x38bf44c8, 0x38bf4600, 0x39150000)
*
*/
}
二、大對象直接進入老年代
當創建的對象超過指定大小時,直接把對象分配在老年代中。
-XX:PretenureSizeThreshold=3145728 參數設定超過對象超過多少時,分配到老年代中,此例爲3M(3*1024*1024)。
/**
* VM參數:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=3145728
* 對象超過3M 時直接進入老年代
*/
public static void testPretenureSizeThreshold() {
byte[] allocation;
allocation = new byte[4 * _1MB]; //直接分配在老年代中
/**
Heap
def new generation total 9216K, used 507K [0x32750000, 0x33150000, 0x33150000)
eden space 8192K, 6% used [0x32750000, 0x327cef38, 0x32f50000)
from space 1024K, 0% used [0x32f50000, 0x32f50000, 0x33050000)
to space 1024K, 0% used [0x33050000, 0x33050000, 0x33150000)
tenured generation total 10240K, used 4096K [0x33150000, 0x33b50000, 0x33b50000)
the space 10240K, 40% used [0x33150000, 0x33550010, 0x33550200, 0x33b50000)
compacting perm gen total 12288K, used 376K [0x33b50000, 0x34750000, 0x37b50000)
the space 12288K, 3% used [0x33b50000, 0x33bae3b8, 0x33bae400, 0x34750000)
ro space 10240K, 55% used [0x37b50000, 0x380d1140, 0x380d1200, 0x38550000)
rw space 12288K, 55% used [0x38550000, 0x38bf44c8, 0x38bf4600, 0x39150000)
*/
}
三、長期存活對象進入老年代
對象在兩個survivor區每複製(Minor gc)一次,年齡就增長一歲,當超過指定最大隨時時轉移到老年代中。
-XX:MaxTenuringThreshold=8 參數用於設定對象最大年齡閾值。
/**
* VM參數:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1 -XX:+PrintTenuringDistribution
*
*/
@SuppressWarnings("unused")
public static void testTenuringThreshold() {
byte[] allocation1, allocation2, allocation3;
allocation1 = new byte[_1MB / 4]; //262144 什麼時候進入老年代決定於XX:MaxTenuringThreshold設置
allocation2 = new byte[4 * _1MB]; //4194304
allocation3 = new byte[4 * _1MB];
allocation3 = null;
allocation3 = new byte[4 * _1MB];
/**
*
* XX:MaxTenuringThreshold =8
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 8 (max 8)
- age 1: 418144 bytes, 418144 total
: 4695K->408K(9216K), 0.0036693 secs] 4695K->4504K(19456K), 0.0036983 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 8 (max 8)
- age 1: 136 bytes, 136 total
- age 2: 417936 bytes, 418072 total
: 4668K->408K(9216K), 0.0010034 secs] 8764K->4504K(19456K), 0.0010296 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4668K [0x32750000, 0x33150000, 0x33150000)
eden space 8192K, 52% used [0x32750000, 0x32b78fe0, 0x32f50000)
from space 1024K, 39% used [0x32f50000, 0x32fb6118, 0x33050000)
to space 1024K, 0% used [0x33050000, 0x33050000, 0x33150000)
tenured generation total 10240K, used 4096K [0x33150000, 0x33b50000, 0x33b50000)
the space 10240K, 40% used [0x33150000, 0x33550010, 0x33550200, 0x33b50000)
compacting perm gen total 12288K, used 377K [0x33b50000, 0x34750000, 0x37b50000)
the space 12288K, 3% used [0x33b50000, 0x33bae5b8, 0x33bae600, 0x34750000)
ro space 10240K, 55% used [0x37b50000, 0x380d1140, 0x380d1200, 0x38550000)
rw space 12288K, 55% used [0x38550000, 0x38bf44c8, 0x38bf4600, 0x39150000)
*/
/**
*
* XX:MaxTenuringThreshold=1
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age 1: 418144 bytes, 418144 total
: 4695K->408K(9216K), 0.0054252 secs] 4695K->4504K(19456K), 0.0054708 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[GC [DefNew
Desired survivor size 524288 bytes, new threshold 1 (max 1)
- age 1: 136 bytes, 136 total
: 4668K->0K(9216K), 0.0013601 secs] 8764K->4504K(19456K), 0.0013867 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4260K [0x32750000, 0x33150000, 0x33150000)
eden space 8192K, 52% used [0x32750000, 0x32b78fe0, 0x32f50000)
from space 1024K, 0% used [0x32f50000, 0x32f50088, 0x33050000)
to space 1024K, 0% used [0x33050000, 0x33050000, 0x33150000)
tenured generation total 10240K, used 4504K [0x33150000, 0x33b50000, 0x33b50000)
the space 10240K, 43% used [0x33150000, 0x335b60a0, 0x335b6200, 0x33b50000)
compacting perm gen total 12288K, used 377K [0x33b50000, 0x34750000, 0x37b50000)
the space 12288K, 3% used [0x33b50000, 0x33bae5c0, 0x33bae600, 0x34750000)
ro space 10240K, 55% used [0x37b50000, 0x380d1140, 0x380d1200, 0x38550000)
rw space 12288K, 55% used [0x38550000, 0x38bf44c8, 0x38bf4600, 0x39150000)
*/
}
解釋
- age 1: 136 bytes, 136 total- age 2: 417936 bytes, 418072 total
- age 2: 417936 bytes, 418072 total
表示 年齡爲1 的對象有總共多大,以此類似。total 從上往下加。
四、對象動態年齡判斷
雖然可以設置對象最大年齡閾值,但有時候虛擬機會根據內存情況自己動態計算對象的閾值。
當survivor區相同年齡的大小不小於survivor區的一半時,虛擬機會把survivor區等於和大於此年齡的對象轉移到老年代。
/**
* VM參數:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution
*
*/
@SuppressWarnings("unused")
public static void testTenuringThreshold2() {
byte[] allocation1, allocation2, allocation3, allocation4;
allocation1 = new byte[_1MB / 4]; // allocation1+allocation2大於survivo空間一半
allocation2 = new byte[_1MB / 4];
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: 680304 bytes, 680304 total
: 4951K->664K(9216K), 0.0033210 secs] 4951K->4760K(19456K), 0.0033442 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 1: 136 bytes, 136 total
: 4924K->0K(9216K), 0.0011772 secs] 9020K->4760K(19456K), 0.0011987 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 4260K [0x32750000, 0x33150000, 0x33150000)
eden space 8192K, 52% used [0x32750000, 0x32b78fe0, 0x32f50000)
from space 1024K, 0% used [0x32f50000, 0x32f50088, 0x33050000)
to space 1024K, 0% used [0x33050000, 0x33050000, 0x33150000)
tenured generation total 10240K, used 4760K [0x33150000, 0x33b50000, 0x33b50000)
the space 10240K, 46% used [0x33150000, 0x335f60b0, 0x335f6200, 0x33b50000)
compacting perm gen total 12288K, used 377K [0x33b50000, 0x34750000, 0x37b50000)
the space 12288K, 3% used [0x33b50000, 0x33bae5c0, 0x33bae600, 0x34750000)
ro space 10240K, 55% used [0x37b50000, 0x380d1140, 0x380d1200, 0x38550000)
rw space 12288K, 55% used [0x38550000, 0x38bf44c8, 0x38bf4600, 0x39150000)
*/
}
解釋:
new threshold 1 (max 15) 表示 虛擬機自己計算的閾值爲1 ,最大15。
五、空間分配擔保
在發送minor gc之前,虛擬機會首先檢查老年代最大可連續空間是否大於新生代所有對象總和,如果這個條件成立,可以確保這次minor gc是安全的,
如果不成立,虛擬機會查看HandlePromotionFailure設置值是否允許擔保失敗。如果允許,那麼會繼續檢查老年代最大可連續空間是否大於歷次晉升到老年代對象
的評價大小,如果大於,將嘗試一次minor gc,儘管這次minor gc是有風險的;如果小於,或者HandlePromotionFailure設置不允許冒險,那麼這時也要改爲一次Full gc。
在jdk1.6 update24之後,HandlePromotionFailure參數不會影響虛擬機空間分配擔保策略,虛擬機改爲,只要老年代最大連續空間大於新生代對象總和或者大於歷次晉升平均大小,都將進行minor gc,否則將進行Full gc。
/**
* VM參數:-Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:-HandlePromotionFailure
*/
@SuppressWarnings("unused")
public static void testHandlePromotion() {
byte[] allocation1, allocation2, allocation3, allocation4, allocation5, allocation6, allocation7;
allocation1 = new byte[2 * _1MB];
allocation2 = new byte[2 * _1MB];
allocation3 = new byte[2 * _1MB];
allocation1 = null;
allocation4 = new byte[2 * _1MB];
allocation5 = new byte[2 * _1MB];
allocation6 = new byte[2 * _1MB];
allocation4 = null;
allocation5 = null;
allocation6 = null;
allocation7 = new byte[2 * _1MB];
/**
*
java.version = 1.6.0_37
[GC [DefNew: 6487K->152K(9216K), 0.0040346 secs] 6487K->4248K(19456K), 0.0040639 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC [DefNew: 6546K->152K(9216K), 0.0004896 secs] 10642K->4248K(19456K), 0.0005141 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 2364K [0x32750000, 0x33150000, 0x33150000)
eden space 8192K, 27% used [0x32750000, 0x32978fe0, 0x32f50000)
from space 1024K, 14% used [0x32f50000, 0x32f76108, 0x33050000)
to space 1024K, 0% used [0x33050000, 0x33050000, 0x33150000)
tenured generation total 10240K, used 4096K [0x33150000, 0x33b50000, 0x33b50000)
the space 10240K, 40% used [0x33150000, 0x33550020, 0x33550200, 0x33b50000)
compacting perm gen total 12288K, used 377K [0x33b50000, 0x34750000, 0x37b50000)
the space 12288K, 3% used [0x33b50000, 0x33bae758, 0x33bae800, 0x34750000)
ro space 10240K, 55% used [0x37b50000, 0x380d1140, 0x380d1200, 0x38550000)
rw space 12288K, 55% used [0x38550000, 0x38bf44c8, 0x38bf4600, 0x39150000)
Warning: The flag -HandlePromotionFailure has been EOL'd as of 6.0_24 and will be ignored
*/
}