package org.example;import java.io.IOException;public class Main { public static int a=5; public static void main(String[] args) throws InterruptedException { Thread.sleep(10000000000l); return ; }}
E:F:T=8:1:1,其中T默认是已使用内存
既然刚启动,那为什么EdenSpace会有占用呢?
类加载过程
思考:当new的对象占用内存如果超过了幸存区内存会发生什么?内存溢出?动态占比分配?试验代码:package org.example; import java.io.IOException; import java.lang.reflect.Array; import java.util.ArrayList; public class Main { public static int a=5; public static void main(String[] args) throws InterruptedException, IOException { System.in.read(); ArrayList bytes=new ArrayList<>(); System.out.println("添加对象中"); bytes.add(new byte1024*1024*5); bytes.forEach(System.out::println); System.in.read(); bytes.add(new byte1024*1024*5); bytes.forEach(System.out::println); System.in.read(); bytes.add(new byte1024*1024*5); bytes.forEach(System.out::println); bytes.add(new byte1024*1024*5); bytes.forEach(System.out::println); return ; } }结果:D:\Program\jdk\bin\java.exe -Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintGCDetails -verbose:gc "-javaagent:D:\Program\IntelliJ IDEA 2022.3.1\lib\idea_rt.jar=61656:D:\Program\IntelliJ IDEA 2022.3.1\bin" -Dfile.encoding=UTF-8 -classpath H:\JVM\gc\target\classes org.example.Main 0.003sgc -XX:+PrintGCDetails is deprecated. Will use -Xlog:gc* instead. 0.010sgc Using Serial 0.010sgc,init Version: 17.0.1+12-39 (release) 0.010sgc,init CPUs: 8 total, 8 available 0.010sgc,init Memory: 13810M 0.010sgc,init Large Page Support: Disabled 0.010sgc,init NUMA Support: Disabled 0.010sgc,init Compressed Oops: Enabled (32-bit) 0.010sgc,init Heap Min Capacity: 20M 0.010sgc,init Heap Initial Capacity: 20M 0.010sgc,init Heap Max Capacity: 20M 0.010sgc,init Pre-touch: Disabled 0.010sgc,metaspace CDS archive(s) mapped at: [0x0000000800000000-0x0000000800bc0000-0x0000000800bc0000), size 12320768, SharedBaseAddress: 0x0000000800000000, ArchiveRelocationMode: 0. 0.011sgc,metaspace Compressed class space mapped at: 0x0000000800c00000-0x0000000840c00000, reserved size: 1073741824 0.011sgc,metaspace Narrow klass base: 0x0000000800000000, Narrow klass shift: 0, Narrow klass range: 0x100000000 10.160sgc,start GC(0) Pause Young (Allocation Failure) 10.165sgc,heap GC(0) DefNew: 8192K(9216K)->1023K(9216K) Eden: 8192K(8192K)->0K(8192K) From: 0K(1024K)->1023K(1024K) 10.165sgc,heap GC(0) Tenured: 0K(10240K)->1443K(10240K) 10.165sgc,metaspace GC(0) Metaspace: 2679K(2880K)->2679K(2880K) NonClass: 2400K(2496K)->2400K(2496K) Class: 279K(384K)->279K(384K) 10.165sgc GC(0) Pause Young (Allocation Failure) 8M->2M(19M) 4.780ms 10.165sgc,cpu GC(0) User=0.00s Sys=0.02s Real=0.01s 10.384sgc,start GC(1) Pause Young (Allocation Failure) 10.390sgc,heap GC(1) DefNew: 9215K(9216K)->482K(9216K) Eden: 8192K(8192K)->0K(8192K) From: 1023K(1024K)->482K(1024K) 10.390sgc,heap GC(1) Tenured: 1443K(10240K)->2463K(10240K) 10.390sgc,metaspace GC(1) Metaspace: 4881K(5120K)->4881K(5120K) NonClass: 4357K(4480K)->4357K(4480K) Class: 523K(640K)->523K(640K) 10.390sgc GC(1) Pause Young (Allocation Failure) 10M->2M(19M) 5.666ms 10.390sgc,cpu GC(1) User=0.00s Sys=0.00s Real=0.01s 26.252sgc,start GC(2) Pause Full (System.gc()) 26.252sgc,phases,start GC(2) Phase 1: Mark live objects 26.257sgc,phases GC(2) Phase 1: Mark live objects 5.380ms 26.257sgc,phases,start GC(2) Phase 2: Compute new object addresses 26.259sgc,phases GC(2) Phase 2: Compute new object addresses 2.149ms 26.259sgc,phases,start GC(2) Phase 3: Adjust pointers 26.262sgc,phases GC(2) Phase 3: Adjust pointers 3.118ms 26.263sgc,phases,start GC(2) Phase 4: Move objects 26.263sgc,phases GC(2) Phase 4: Move objects 0.855ms 26.264sgc,heap GC(2) DefNew: 8083K(9216K)->0K(9216K) Eden: 7600K(8192K)->0K(8192K) From: 482K(1024K)->0K(1024K) 26.264sgc,heap GC(2) Tenured: 2463K(10240K)->3900K(10240K) 26.264sgc,metaspace GC(2) Metaspace: 7956K(8256K)->7956K(8256K) NonClass: 7083K(7232K)->7083K(7232K) Class: 872K(1024K)->872K(1024K) 26.264sgc GC(2) Pause Full (System.gc()) 10M->3M(19M) 12.256ms 26.264sgc,cpu GC(2) User=0.02s Sys=0.00s Real=0.01s 添加对象中 [B@3b07d329 49.675sgc,start GC(3) Pause Young (Allocation Failure) 49.678sgc,heap GC(3) DefNew: 8192K(9216K)->35K(9216K) Eden: 8192K(8192K)->0K(8192K) From: 0K(1024K)->35K(1024K) 49.678sgc,heap GC(3) Tenured: 3900K(10240K)->9020K(10240K) 49.678sgc,metaspace GC(3) Metaspace: 8137K(8384K)->8137K(8384K) NonClass: 7263K(7360K)->7263K(7360K) Class: 874K(1024K)->874K(1024K) 49.678sgc GC(3) Pause Young (Allocation Failure) 11M->8M(19M) 2.957ms 49.678sgc,cpu GC(3) User=0.00s Sys=0.02s Real=0.00s 96.458sgc,start GC(4) Pause Young (Allocation Failure) 96.458sgc GC(4) Pause Young (Allocation Failure) 14M->14M(19M) 0.078ms 96.458sgc,cpu GC(4) User=0.00s Sys=0.00s Real=0.00s 96.458sgc,start GC(5) Pause Full (Allocation Failure) 96.458sgc,phases,start GC(5) Phase 1: Mark live objects 96.467sgc,phases GC(5) Phase 1: Mark live objects 8.235ms 96.467sgc,phases,start GC(5) Phase 2: Compute new object addresses 96.469sgc,phases GC(5) Phase 2: Compute new object addresses 2.466ms 96.469sgc,phases,start GC(5) Phase 3: Adjust pointers 96.473sgc,phases GC(5) Phase 3: Adjust pointers 4.087ms 96.473sgc,phases,start GC(5) Phase 4: Move objects 96.474sgc,phases GC(5) Phase 4: Move objects 0.946ms 96.475sgc,heap GC(5) DefNew: 5571K(9216K)->0K(9216K) Eden: 5535K(8192K)->0K(8192K) From: 35K(1024K)->0K(1024K) 96.475sgc,heap GC(5) Tenured: 9020K(10240K)->8999K(10240K) 96.475sgc,metaspace GC(5) Metaspace: 8213K(8448K)->8092K(8448K) NonClass: 7339K(7424K)->7244K(7424K) Class: 874K(1024K)->848K(1024K) 96.475sgc GC(5) Pause Full (Allocation Failure) 14M->8M(19M) 16.586ms 96.475sgc,cpu GC(5) User=0.02s Sys=0.00s Real=0.02s [B@3b07d329 [B@682a0b20 96.477sgc,start GC(6) Pause Young (Allocation Failure) 96.477sgc GC(6) Pause Young (Allocation Failure) 13M->13M(19M) 0.086ms 96.477sgc,cpu GC(6) User=0.00s Sys=0.00s Real=0.00s 96.477sgc,start GC(7) Pause Full (Allocation Failure) 96.477sgc,phases,start GC(7) Phase 1: Mark live objects 96.485sgc,phases GC(7) Phase 1: Mark live objects 8.022ms 96.485sgc,phases,start GC(7) Phase 2: Compute new object addresses 96.487sgc,phases GC(7) Phase 2: Compute new object addresses 1.451ms 96.487sgc,phases,start GC(7) Phase 3: Adjust pointers 96.491sgc,phases GC(7) Phase 3: Adjust pointers 4.088ms 96.491sgc,phases,start GC(7) Phase 4: Move objects 96.492sgc,phases GC(7) Phase 4: Move objects 0.856ms 96.492sgc,heap GC(7) DefNew: 5217K(9216K)->5121K(9216K) Eden: 5217K(8192K)->5121K(8192K) From: 0K(1024K)->0K(1024K) 96.492sgc,heap GC(7) Tenured: 8999K(10240K)->8962K(10240K) 96.492sgc,metaspace GC(7) Metaspace: 8097K(8448K)->8028K(8448K) NonClass: 7247K(7424K)->7189K(7424K) Class: 849K(1024K)->838K(1024K) 96.492sgc GC(7) Pause Full (Allocation Failure) 13M->13M(19M) 15.333ms 96.492sgc,cpu GC(7) User=0.02s Sys=0.00s Real=0.02s 96.492sgc,start GC(8) Pause Full (Allocation Failure) 96.492sgc,phases,start GC(8) Phase 1: Mark live objects 96.498sgc,phases GC(8) Phase 1: Mark live objects 6.004ms 96.498sgc,phases,start GC(8) Phase 2: Compute new object addresses 96.500sgc,phases GC(8) Phase 2: Compute new object addresses 1.182ms 96.500sgc,phases,start GC(8) Phase 3: Adjust pointers 96.504sgc,phases GC(8) Phase 3: Adjust pointers 4.837ms 96.505sgc,phases,start GC(8) Phase 4: Move objects 96.507sgc,phases GC(8) Phase 4: Move objects 2.185ms 96.507sgc,heap GC(8) DefNew: 5121K(9216K)->5121K(9216K) Eden: 5121K(8192K)->5121K(8192K) From: 0K(1024K)->0K(1024K) 96.507sgc,heap GC(8) Tenured: 8962K(10240K)->8450K(10240K) 96.507sgc,metaspace GC(8) Metaspace: 8028K(8448K)->8028K(8448K) NonClass: 7189K(7424K)->7189K(7424K) Class: 838K(1024K)->838K(1024K) 96.507sgc GC(8) Pause Full (Allocation Failure) 13M->13M(19M) 14.864ms 96.507sgc,cpu GC(8) User=0.02s Sys=0.00s Real=0.02s 96.509sgc,heap,exit Heap 96.509sgc,heap,exit def new generation total 9216K, used 5405K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000) 96.509sgc,heap,exit eden space 8192K, 65% used [0x00000000fec00000, 0x00000000ff1475c8, 0x00000000ff400000) 96.509sgc,heap,exit from space 1024K, 0% used [0x00000000ff500000, 0x00000000ff500000, 0x00000000ff600000) 96.509sgc,heap,exit to space 1024K, 0% used [0x00000000ff400000, 0x00000000ff400000, 0x00000000ff500000) 96.509sgc,heap,exit tenured generation total 10240K, used 8450K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000) 96.509sgc,heap,exit the space 10240K, 82% used [0x00000000ff600000, 0x00000000ffe40b90, 0x00000000ffe40c00, 0x0000000100000000) 96.509sgc,heap,exit Metaspace used 8034K, committed 8448K, reserved 1056768K 96.509sgc,heap,exit class space used 839K, committed 1024K, reserved 1048576K Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at org.example.Main.main(Main.java:21) 进程已结束,退出代码1