jvm运行调试参数
jdk.18
参数 | - |
---|---|
-xms30M -Xmx30M | 堆空间30M |
-Xmn10m | 新生代10m |
-XX:SurvivorRatio=8 | 设置Eden和Survivor比例 默认8:1:1 |
-XX:+UseParallelGC | jdk8默认使用的收集器 |
-XX:+PrintGCDetails | 打印gc详情 |
-XX:+PrintCommandLineFlags | 打印jvm设置参数 |
-XX:+PrintHeapAtGC | 打印GC前后的各代大小 |
-XX:+PrintTenuringdistribution | 打印存活区每段年龄的大小 |
调试代码很简单:
public static void main(String[] args) {
int maxSiz = 1024 * 1024 * 20;
for (int i = 0; i < 100000; i++) {
System.out.println("循环次数"+i);
List<byte[]> data = new ArrayList<>();
System.out.println("+2");
byte [] obj = new byte [maxSiz]; // 20M
System.out.println("+2");
byte [] obj2 = new byte [maxSiz]; // 20M
System.out.println("+2");
byte [] obj3 = new byte [maxSiz]; // 20M
data.add(obj);
data.add(obj2);
data.add(obj3);
data=null;
//if(i == 200) return;
}
}
运行日志如下:
循环次数0
+2
+2
+2
循环次数1
+2
{Heap before GC invocations=1 (full 0):
PSYoungGen total 92160K, used 67993K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 83% used [0x00000007b9c00000,0x00000007bde666d0,0x00000007bec00000)
from space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
to space 10240K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf600000)
ParOldGen total 204800K, used 0K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad400000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 10485760 bytes, new threshold 7 (max 15)
[PSYoungGen: 67993K->704K(92160K)] 67993K->712K(296960K), 0.0007076 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=1 (full 0):
PSYoungGen total 92160K, used 704K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bec00000,0x00000007becb0020,0x00000007bf600000)
to space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
+2
+2
循环次数2
+2
{Heap before GC invocations=2 (full 0):
PSYoungGen total 92160K, used 63723K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 76% used [0x00000007b9c00000,0x00000007bd98ad40,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bec00000,0x00000007becb0020,0x00000007bf600000)
to space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 10485760 bytes, new threshold 7 (max 15)
[PSYoungGen: 63723K->752K(92160K)] 63731K->760K(296960K), 0.0010497 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=2 (full 0):
PSYoungGen total 92160K, used 752K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bec00000)
from space 10240K, 7% used [0x00000007bf600000,0x00000007bf6bc010,0x00000007c0000000)
to space 10240K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf600000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
+2
+2
循环次数3
+2
{Heap before GC invocations=3 (full 0):
PSYoungGen total 92160K, used 65229K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 78% used [0x00000007b9c00000,0x00000007bdaf77e0,0x00000007bec00000)
from space 10240K, 7% used [0x00000007bf600000,0x00000007bf6bc010,0x00000007c0000000)
to space 10240K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf600000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 10485760 bytes, new threshold 7 (max 15)
[PSYoungGen: 65229K->640K(92160K)] 65237K->648K(296960K), 0.0004890 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=3 (full 0):
PSYoungGen total 92160K, used 640K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bec00000,0x00000007beca0020,0x00000007bf600000)
to space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
+2
+2
-----------------
.....
循环次数5
+2
{Heap before GC invocations=5 (full 0):
PSYoungGen total 92160K, used 63602K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 76% used [0x00000007b9c00000,0x00000007bd978af0,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bf600000,0x00000007bf6a4010,0x00000007c0000000)
to space 10240K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf600000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 1048576 bytes, new threshold 6 (max 15)
[PSYoungGen: 63602K->720K(92160K)] 63610K->728K(296960K), 0.0005024 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
Heap after GC invocations=5 (full 0):
PSYoungGen total 92160K, used 720K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bec00000)
from space 10240K, 7% used [0x00000007bec00000,0x00000007becb4010,0x00000007bf600000)
to space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
+2
+2
循环次数6
+2
{Heap before GC invocations=6 (full 0):
PSYoungGen total 92160K, used 63699K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 76% used [0x00000007b9c00000,0x00000007bd980c90,0x00000007bec00000)
from space 10240K, 7% used [0x00000007bec00000,0x00000007becb4010,0x00000007bf600000)
to space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 1048576 bytes, new threshold 5 (max 15)
[PSYoungGen: 63699K->672K(101376K)] 63707K->680K(306176K), 0.0004998 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=6 (full 0):
PSYoungGen total 101376K, used 672K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 100352K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bfe00000)
from space 1024K, 65% used [0x00000007bff00000,0x00000007bffa8000,0x00000007c0000000)
to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
+2
+2
循环次数7
+2
+2
{Heap before GC invocations=7 (full 0):
PSYoungGen total 101376K, used 84534K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 100352K, 83% used [0x00000007b9c00000,0x00000007bedddb90,0x00000007bfe00000)
from space 1024K, 68% used [0x00000007bff00000,0x00000007bffb0000,0x00000007c0000000)
to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3306K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 1048576 bytes, new threshold 4 (max 15)
[PSYoungGen: 84534K->64K(101376K)] 84542K->21131K(306176K), 0.0135895 secs] [Times: user=0.11 sys=0.00, real=0.01 secs]
Heap after GC invocations=7 (full 0):
PSYoungGen total 101376K, used 64K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 100352K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bfe00000)
from space 1024K, 6% used [0x00000007bfe00000,0x00000007bfe10000,0x00000007bff00000)
to space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
ParOldGen total 204800K, used 21067K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 10% used [0x00000007ad400000,0x00000007ae892c38,0x00000007b9c00000)
Metaspace used 3306K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
+2
------------------
.....
循环次数16
+2
+2
+2
{Heap before GC invocations=14 (full 0):
PSYoungGen total 100864K, used 84713K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 99328K, 85% used [0x00000007b9c00000,0x00000007beeaa578,0x00000007bfd00000)
from space 1536K, 4% used [0x00000007bfd00000,0x00000007bfd10000,0x00000007bfe80000)
to space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
ParOldGen total 204800K, used 143947K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 70% used [0x00000007ad400000,0x00000007b6092c98,0x00000007b9c00000)
Metaspace used 3308K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 1048576 bytes, new threshold 1 (max 15)
[PSYoungGen: 84713K->64K(101376K)] 228660K->184979K(306176K), 0.0505708 secs] [Times: user=0.32 sys=0.03, real=0.05 secs]
Heap after GC invocations=14 (full 0):
PSYoungGen total 101376K, used 64K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 100352K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bfe00000)
from space 1024K, 6% used [0x00000007bff00000,0x00000007bff10000,0x00000007c0000000)
to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen total 204800K, used 184915K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 90% used [0x00000007ad400000,0x00000007b8894cb8,0x00000007b9c00000)
Metaspace used 3308K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=15 (full 1):
PSYoungGen total 101376K, used 64K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 100352K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bfe00000)
from space 1024K, 6% used [0x00000007bff00000,0x00000007bff10000,0x00000007c0000000)
to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen total 204800K, used 184915K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 90% used [0x00000007ad400000,0x00000007b8894cb8,0x00000007b9c00000)
Metaspace used 3308K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[Full GC (Ergonomics) [PSYoungGen: 64K->0K(101376K)] [ParOldGen: 184915K->41482K(204800K)] 184979K->41482K(306176K), [Metaspace: 3308K->3308K(1056768K)], 0.0177344 secs] [Times: user=0.09 sys=0.01, real=0.01 secs]
Heap after GC invocations=15 (full 1):
PSYoungGen total 101376K, used 0K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 100352K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bfe00000)
from space 1024K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007c0000000)
to space 1024K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007bff00000)
ParOldGen total 204800K, used 41482K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 20% used [0x00000007ad400000,0x00000007afc82948,0x00000007b9c00000)
Metaspace used 3308K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
根据JVM打印的日志来慢慢解析JVM的内存是如何回收释放的:
根据我们设置的参数,新生代一共分配100M,按照比例8:1:1分配,s0和s1分配有10M空间, eden分配80M
循环0 内存增加了60M < eden space(80M) 不进行youngGc,此时根据下一次循环的gc日志可以判断大概使用了83%的空间(初始还需要一些空间的)
循环1 分配内存时判断如果+20M后 >=eden space(80M) 所以先触发youngGc,日志中new threshold 7 (max 15)代表年龄达到7次放入老年代;然后往eden中一次放入20+20+20共60M空间,后面几次循环情况和这一次循环完全相同
循环5 发现空间使用情况依旧一样但是new threshold降低成6了,说明新生代生存周期的阀值会进行调节
循环6、循环7 发现新生代空间发生了调整,S0S1分别开始降低到1M,ede扩大,下一次循环7时发现存入80M还没有触发youngGC(使用率83%),这时候继续加入20M发现空间不够,触发youngGC,但发现根据s0s1复制算法发现循环7的第一次20M的内存加入是不需要清理的,复制到to中时发现不够这时候使用担保机制存入old区,所以也就在youngGC的日志中发现在GC后老年代中多了20M这样的内存使用。可以使用参数UseAdaptiveSizePolicy=false禁止调节(jdk1.8默认开启),使用此参数后SurvivorRatio参数就无效了
循环16 在youngGC后发现old区最终空间不够发生了第一次fullGC
public static void main(String[] args) throws Exception {
int maxSiz = 1024 * 1024 * 40;
for (int i = 0; i < 100000; i++) {
System.out.println("循环次数"+i);
List<byte[]> data = new ArrayList<>(8);
System.out.println("+30");
byte [] obj = new byte [maxSiz]; // 20M
// System.out.println("+2");
// byte [] obj2 = new byte [maxSiz]; // 20M
// System.out.println("+2");
// byte [] obj3 = new byte [maxSiz]; // 20M
data.add(obj);
// data.add(obj2);
// data.add(obj3);
data=null;
if(i == 50) return;
}
}
循环次数0
+30
循环次数1
+30
循环次数2
+30
循环次数3
+30
循环次数4
+30
循环次数5
+30
{Heap before GC invocations=1 (full 0):
PSYoungGen total 92160K, used 47513K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 58% used [0x00000007b9c00000,0x00000007bca66710,0x00000007bec00000)
from space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
to space 10240K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf600000)
ParOldGen total 204800K, used 163840K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 80% used [0x00000007ad400000,0x00000007b7400040,0x00000007b9c00000)
Metaspace used 3304K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 10485760 bytes, new threshold 7 (max 15)
[PSYoungGen: 47513K->688K(92160K)] 211353K->164536K(296960K), 0.0027131 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
Heap after GC invocations=1 (full 0):
PSYoungGen total 92160K, used 688K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bec00000,0x00000007becac010,0x00000007bf600000)
to space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
ParOldGen total 204800K, used 163848K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 80% used [0x00000007ad400000,0x00000007b7402040,0x00000007b9c00000)
Metaspace used 3304K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
循环次数6
+30
{Heap before GC invocations=2 (full 0):
PSYoungGen total 92160K, used 43286K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 52% used [0x00000007b9c00000,0x00000007bc5999f0,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bec00000,0x00000007becac010,0x00000007bf600000)
to space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
ParOldGen total 204800K, used 163848K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 80% used [0x00000007ad400000,0x00000007b7402040,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 10485760 bytes, new threshold 7 (max 15)
[PSYoungGen: 43286K->656K(92160K)] 207134K->164504K(296960K), 0.0023688 secs] [Times: user=0.02 sys=0.00, real=0.00 secs]
Heap after GC invocations=2 (full 0):
PSYoungGen total 92160K, used 656K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bf600000,0x00000007bf6a4010,0x00000007c0000000)
to space 10240K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf600000)
ParOldGen total 204800K, used 163848K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 80% used [0x00000007ad400000,0x00000007b7402040,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
发现第一次出发youngGC的时候old区竟然就快被占满了,这是什么情况。
这就跟选用的垃圾收集器有关了,当前程序参数UseParallelGC使用了Parallel Scavenge收集器。在这种收集器下,担保机制的出发条件和serial不太一样,在GC之前,判断如果分配的内存>=Eden区大小的一半,那么会直接把申请的空间放入old区,否则才会进入担保机制。
根据第一次循环eden放入40M,第二次循环发现40M>=eden/2,所以直接进入老年代,所以上面日志中在old区的近160M内存正好是从第二次到第5次这4次申请的内存空间。
但是此时又出现一个问题,在循环次数6时发生的gc为什么还是youngGC而不是fullGC。
首先youngGC的触发条件:(HandlePromotionFailure 默认允许担保失败)
- 老年代剩余连续内存空间 > 新生代对象总空间>晋升到老年代的对象的平均大小
- 晋升到老年代的对象的平均大小<老年代剩余连续内存空间 < 新生代对象总空间
根据代码判断 晋升到老年代的对象的平均大小 约40M,目前old区剩余40M(还要偏小一点),而eden区只有80*%48的空间小于40M。(代码改成每次申请60M大小也一样,最终old区剩余20M,同样不会触发fullGC)。
所以这种情况下,猜测(还未验证):在fullGC前会进行一次youngGC
下面将代码再稍做调整
改成30大小
public static void main(String[] args) throws Exception {
int maxSiz = 1024 * 1024 * 30;
for (int i = 0; i < 100000; i++) {
System.out.println("循环次数"+i);
List<byte[]> data = new ArrayList<>(8);
System.out.println("+30");
byte [] obj = new byte [maxSiz]; // 20M
// System.out.println("+2");
byte [] obj2 = new byte [1024*1024]; // 20M
// System.out.println("+2");
// byte [] obj3 = new byte [maxSiz]; // 20M
data.add(obj);
data.add(obj2);
// data.add(obj3);
data=null;
if(i == 50) return;
}
}
循环次数0
+30
循环次数1
+30
循环次数2
+30
{Heap before GC invocations=1 (full 0):
PSYoungGen total 92160K, used 69017K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 84% used [0x00000007b9c00000,0x00000007bdf666d0,0x00000007bec00000)
from space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
to space 10240K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf600000)
ParOldGen total 204800K, used 0K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad400000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 10485760 bytes, new threshold 7 (max 15)
[PSYoungGen: 69017K->688K(92160K)] 69017K->696K(296960K), 0.0008276 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=1 (full 0):
PSYoungGen total 92160K, used 688K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bec00000,0x00000007becac010,0x00000007bf600000)
to space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
循环次数3
+30
循环次数4
+30
{Heap before GC invocations=2 (full 0):
PSYoungGen total 92160K, used 64732K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 78% used [0x00000007b9c00000,0x00000007bda8b068,0x00000007bec00000)
from space 10240K, 6% used [0x00000007bec00000,0x00000007becac010,0x00000007bf600000)
to space 10240K, 0% used [0x00000007bf600000,0x00000007bf600000,0x00000007c0000000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
[GC (Allocation Failure)
Desired survivor size 10485760 bytes, new threshold 7 (max 15)
[PSYoungGen: 64732K->736K(92160K)] 64740K->744K(296960K), 0.0006516 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=2 (full 0):
PSYoungGen total 92160K, used 736K [0x00000007b9c00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 81920K, 0% used [0x00000007b9c00000,0x00000007b9c00000,0x00000007bec00000)
from space 10240K, 7% used [0x00000007bf600000,0x00000007bf6b8000,0x00000007c0000000)
to space 10240K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf600000)
ParOldGen total 204800K, used 8K [0x00000007ad400000, 0x00000007b9c00000, 0x00000007b9c00000)
object space 204800K, 0% used [0x00000007ad400000,0x00000007ad402000,0x00000007b9c00000)
Metaspace used 3305K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 364K, capacity 388K, committed 512K, reserved 1048576K
}
这种情况下old区一直都未被占用,因为根据paralleGC的判断条件,一直会先进行youngGC,之后空间就都是足够的了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。