JVM_内存溢出(Java heap space)
import java.util.ArrayList;
import java.util.List;
/**
* -XX:MaxHeapSize=10m -XX:InitialHeapSize=10m -XX:+PrintGCDetails
*/
public class GCTest2 {
public static void main(String[] args) {
System.out.println("main start");
List<byte[]> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
byte[] bytes = new byte[1024 * 10]; // 约10k
list.add(bytes);
System.out.println("list size=" + list.size());
}
System.out.println("main end");
}
}
设置了堆空间大小为 10m, 并且打印GC 信息。
定义了一个存储 byte[] 的list, 每次创建大小 约为10k 的byte 数组,并把它添加到 list 中。
则添加不到1000次 (10k * 1000 约为10m) 就会报堆空间溢出,在报内存溢出之前,会尝试对新生代、老年代进行垃圾回收。
list size=124
[GC (Allocation Failure) [PSYoungGen: 2045K->480K(2560K)] 2045K->1857K(9728K), 0.0017847 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
list size=322
[GC (Allocation Failure) [PSYoungGen: 2522K->488K(2560K)] 3900K->3814K(9728K), 0.0018051 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
list size=520
[GC (Allocation Failure) [PSYoungGen: 2532K->504K(2560K)] 5859K->5815K(9728K), 0.0018736 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 504K->0K(2560K)] [ParOldGen: 5311K->5735K(7168K)] 5815K->5735K(9728K), [Metaspace: 2774K->2774K(1056768K)], 0.0079695 secs] [Times: user=0.03 sys=0.00, real=0.00 secs]
list size=718
[Full GC (Ergonomics) [PSYoungGen: 2045K->981K(2560K)] [ParOldGen: 5735K->6738K(7168K)] 7780K->7719K(9728K), [Metaspace: 2774K->2774K(1056768K)], 0.0037200 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
list size=847
[Full GC (Ergonomics) [PSYoungGen: 2048K->2043K(2560K)] [ParOldGen: 6970K->6969K(7168K)] 9018K->9013K(9728K), [Metaspace: 2774K->2774K(1056768K)], 0.0032546 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
list size=867
[Full GC (Ergonomics) [PSYoungGen: 2048K->2043K(2560K)] [ParOldGen: 7158K->7158K(7168K)] 9206K->9201K(9728K), [Metaspace: 2774K->2774K(1056768K)], 0.0023095 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 2043K->2043K(2560K)] [ParOldGen: 7158K->7158K(7168K)] 9201K->9201K(9728K), [Metaspace: 2774K->2774K(1056768K)], 0.0029875 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 2560K, used 2048K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 100% used [0x00000000ffd00000,0x00000000fff00000,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 7168K, used 7159K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 99% used [0x00000000ff600000,0x00000000ffcfdec0,0x00000000ffd00000)
Metaspace used 2805K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 281K, capacity 386K, committed 512K, reserved 1048576K
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.example.javatest.gctest.GCTest2.main(GCTest2.java:17)
本文地址:https://blog.csdn.net/whjk20/article/details/112250429
推荐阅读