java 内存溢出 heap_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 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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值