本测试JDK版本 ,由于1.7之前和之后方法区做了很多调整,目前改用meta space(元空间),直接内存分配啦
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
public class UserSerialGCTest {
public static int _1M = 1024*1024;
/**
* UserSerialGC -verbose:gc -Xmx16M -Xms16M -Xmn10M -XX:SurvivorRatio=8 -XX:+PrintGCDetails
*/
public static void main(String[] args) {
}
}
/结果
Heap
def new generation total 9216K, used 5104K [0x00000000ff000000, 0x00000000ffa00000, 0x00000000ffa00000)
eden space 8192K, 62% used [0x00000000ff000000, 0x00000000ff4fc268, 0x00000000ff800000)
from space 1024K, 0% used [0x00000000ff800000, 0x00000000ff800000, 0x00000000ff900000)
to space 1024K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ffa00000)
tenured generation total 6144K, used 0K [0x00000000ffa00000, 0x0000000100000000, 0x0000000100000000)
the space 6144K, 0% used [0x00000000ffa00000, 0x00000000ffa00000, 0x00000000ffa00200, 0x0000000100000000)
Metaspace used 3471K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 379K, capacity 388K, committed 512K, reserved 1048576K
(6988-939)- (6988-2987)= 2987-939 = 2048K 进入老年代
这时候第一个 3M 进入 很大,但我也能装下,第二个3M 进入=》 触发minor GC ,这时候eden空间不够,启动老年代担保,将新生代中能移到老年代的全掏出来,2个2M对象全滚进去,但新生代还是不够,结果就是 promotion failed 担保失败
public class UserSerialGCTest {
public static int _1M = 1024*1024;
/**
* UserSerialGC -verbose:gc -Xmx16M -Xms16M -Xmn10M -XX:SurvivorRatio=8 -XX:+PrintGCDetails
*/
public static void main(String[] args) {
byte[] a1,a2,a3,a4;
a1 = new byte[2 * _1M];
a2 = new byte[2 * _1M];
a3 = new byte[3 * _1M];
a4 = new byte[3 * _1M];
}
}
//结果
[GC (Allocation Failure) [DefNew: 6988K->939K(9216K), 0.0040029 secs] 6988K->2987K(15360K), 0.0040670 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew (promotion failed) : 6299K->5359K(9216K), 0.0033263 secs][Tenured: 4894K->4894K(6144K), 0.0030257 secs] 8347K->7966K(15360K), [Metaspace: 3452K->3452K(1056768K)], 0.0063990 secs] [Times: user=0.02 sys=0.02, real=0.02 secs]
Heap
def new generation total 9216K, used 6445K [0x00000000ff000000, 0x00000000ffa00000, 0x00000000ffa00000)
eden space 8192K, 78% used [0x00000000ff000000, 0x00000000ff64b680, 0x00000000ff800000)
from space 1024K, 0% used [0x00000000ff800000, 0x00000000ff800000, 0x00000000ff900000)
to space 1024K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ffa00000)
tenured generation total 6144K, used 4894K [0x00000000ffa00000, 0x0000000100000000, 0x0000000100000000)
the space 6144K, 79% used [0x00000000ffa00000, 0x00000000ffec7840, 0x00000000ffec7a00, 0x0000000100000000)
Metaspace used 3468K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 377K, capacity 388K, committed 512K, reserved 1048576K
//这样可以防止出现担保失信问题:
a1 = new byte[2 * _1M];
a2 = new byte[2 * _1M];
a3 = new byte[3 * _1M];
a3 = null;
a4 = new byte[3 * _1M];
/**
* UserSerialGC -verbose:gc -Xmx16M -Xms16M -Xmn10M -XX:SurvivorRatio=8 -XX:+PrintGCDetails
* -XX:PretenureSizeThreshold=3*1024*1024
*/
public static void main(String[] args) {
byte[] a1,a2,a3,a4;
a1 = new byte[2 * _1M];
a3 = new byte[3 * _1M];
a4 = new byte[3 * _1M];
}
}
[GC (Allocation Failure) [Tenured: 3072K->6060K(6144K), 0.0057558 secs] 10060K->6060K(15360K), [Metaspace: 3461K->3461K(1056768K)], 0.0058700 secs] [Times: user=0.00 sys=0.01, real=0.01 secs]
[Full GC (Allocation Failure) [TenuredException in thread "main" java.lang.OutOfMemoryError: Java heap space
at anormaltest.xyztest.test20190506_gc.default_gc_cp.UserSerialGCTest.main(UserSerialGCTest.java:21)
: 6060K->6042K(6144K), 0.0030817 secs] 6060K->6042K(15360K), [Metaspace: 3461K->3461K(1056768K)], 0.0031168 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
def new generation total 9216K, used 329K [0x00000000ff000000, 0x00000000ffa00000, 0x00000000ffa00000)
eden space 8192K, 4% used [0x00000000ff000000, 0x00000000ff0527f0, 0x00000000ff800000)
from space 1024K, 0% used [0x00000000ff800000, 0x00000000ff800000, 0x00000000ff900000)
to space 1024K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ffa00000)
tenured generation total 6144K, used 6042K [0x00000000ffa00000, 0x0000000100000000, 0x0000000100000000)
the space 6144K, 98% used [0x00000000ffa00000, 0x00000000fffe6858, 0x00000000fffe6a00, 0x0000000100000000)
Metaspace used 3507K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 382K, capacity 388K, committed 512K, reserved 1048576K
//15岁的2次GC from space 依旧存在值
[GC (Allocation Failure) [DefNew: 4940K->965K(9216K), 0.0023876 secs] 4940K->965K(15360K), 0.0024423 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 8708K->256K(9216K), 0.0050977 secs] 8708K->5176K(15360K), 0.0051250 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
def new generation total 9216K, used 3410K [0x00000000ff000000, 0x00000000ffa00000, 0x00000000ffa00000)
eden space 8192K, 38% used [0x00000000ff000000, 0x00000000ff314930, 0x00000000ff800000)
from space 1024K, 25% used [0x00000000ff800000, 0x00000000ff8400f0, 0x00000000ff900000)
to space 1024K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ffa00000)
tenured generation total 6144K, used 4919K [0x00000000ffa00000, 0x0000000100000000, 0x0000000100000000)
the space 6144K, 80% used [0x00000000ffa00000, 0x00000000ffecdf30, 0x00000000ffece000, 0x0000000100000000)
Metaspace used 3471K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 379K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure) [DefNew: 5196K->1024K(9216K), 0.0021866 secs] 5196K->1193K(15360K), 0.0022640 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 8429K->2K(9216K), 0.0043497 secs] 8599K->5291K(15360K), 0.0043723 secs] [Times: user=0.02 sys=0.02, real=0.02 secs]
Heap
def new generation total 9216K, used 3212K [0x00000000ff000000, 0x00000000ffa00000, 0x00000000ffa00000)
eden space 8192K, 39% used [0x00000000ff000000, 0x00000000ff3226c8, 0x00000000ff800000)
from space 1024K, 0% used [0x00000000ff800000, 0x00000000ff800a98, 0x00000000ff900000)
to space 1024K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ffa00000)
tenured generation total 6144K, used 5288K [0x00000000ffa00000, 0x0000000100000000, 0x0000000100000000)
the space 6144K, 86% used [0x00000000ffa00000, 0x00000000fff2a338, 0x00000000fff2a400, 0x0000000100000000)
Metaspace used 3457K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 377K, capacity 388K, committed 512K, reserved 1048576K
5 在空间担保开启的情况下空间不足 - 执行 Full GC ---可以关闭: -XX:HandlePromotionFailure=false
[GC (Allocation Failure) [DefNew: 5196K->1023K(9216K), 0.0023341 secs] 5196K->1193K(15360K), 0.0023855 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 8511K->4K(9216K), 0.0047692 secs] 8681K->5293K(15360K), 0.0048009 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [DefNew: 6335K->6335K(9216K), 0.0000154 secs][Tenured: 5289K->5289K(6144K), 0.0044805 secs] 11624K->11437K(15360K), [Metaspace: 3451K->3451K(1056768K)], 0.0045605 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [Tenured: 5289K->5269K(6144K), 0.0046285 secs] 11437K->11417K(15360K), [Metaspace: 3451K->3451K(1056768K)], 0.0046602 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
Heap
def new generation total 9216K, used 6265K [0x00000000ff000000, 0x00000000ffa00000, 0x00000000ffa00000)
eden space 8192K, 76% used [0x00000000ff000000, 0x00000000ff61e720, 0x00000000ff800000)
from space 1024K, 0% used [0x00000000ff800000, 0x00000000ff800000, 0x00000000ff900000)
to space 1024K, 0% used [0x00000000ff900000, 0x00000000ff900000, 0x00000000ffa00000)
tenured generation total 6144K, used 5269K [0x00000000ffa00000, 0x0000000100000000, 0x0000000100000000)
the space 6144K, 85% used [0x00000000ffa00000, 0x00000000fff25750, 0x00000000fff25800, 0x0000000100000000)
Metaspace used 3470K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 378K, capacity 388K, committed 512K, reserved 1048576K