【疑惑】JVM 堆内存溢出后,该线程持有的内存资源是否会被释放

矛盾点

查阅网上资料,部分博客写到:当一个线程抛出OOM异常后,它所占据的内存资源会全部被释放掉,从而不会影响其他线程的运行。

首先肯定的是工作线程内存溢出(OOM)不会停止其他线程运行。

但关于该线程的内存资源是否会被释放,我在实验后发现并没有被释放,与网上观点产生冲突,因此想要请教请教大佬们

实验过程

以下是我的实验代码:

public class Demo2_1 {
    private static final int _512KB = 512 * 1024;
    private static final int _1MB = 1024 * 1024;
    private static final int _6MB = 6 * 1024 * 1024;
    private static final int _7MB = 7 * 1024 * 1024;
    private static final int _8MB = 8 * 1024 * 1024;

    // -Xms20M -Xmx20M -Xmn10M -XX:+UseSerialGC -XX:+PrintGCDetails -verbose:gc -XX:-ScavengeBeforeFullGC
    public static void main(String[] args) throws InterruptedException, IOException {
        new Thread(() -> {
            ArrayList<byte[]> list = new ArrayList<>();
            list.add(new byte[_8MB]);
            list.add(new byte[_8MB]);
        }).start();
        while (true) {
            System.out.println("test");
            Thread.sleep(2000);
        }
    }
}

以下为输出结果:

[GC (Allocation Failure) [DefNew: 3790K->608K(9216K), 0.0032626 secs][Tenured: 8192K->8798K(10240K), 0.0031762 secs] 11982K->8798K(19456K), [Metaspace: 4103K->4103K(1056768K)], 0.0066109 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [Tenured: 8798K->8742K(10240K), 0.0019716 secs] 8798K->8742K(19456K), [Metaspace: 4103K->4103K(1056768K)], 0.0020003 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
test
test
Heap
 def new generation   total 9216K, used 1453K [0x00000007bec00000, 0x00000007bf600000, 0x00000007bf600000)
  eden space 8192K,  17% used [0x00000007bec00000, 0x00000007bed6b510, 0x00000007bf400000)
  from space 1024K,   0% used [0x00000007bf500000, 0x00000007bf500000, 0x00000007bf600000)
  to   space 1024K,   0% used [0x00000007bf400000, 0x00000007bf400000, 0x00000007bf500000)
 tenured generation   total 10240K, used 8742K [0x00000007bf600000, 0x00000007c0000000, 0x00000007c0000000)
   the space 10240K,  85% used [0x00000007bf600000, 0x00000007bfe89b18, 0x00000007bfe89c00, 0x00000007c0000000)
 Metaspace       used 4613K, capacity 4740K, committed 4992K, reserved 1056768K
  class space    used 517K, capacity 560K, committed 640K, reserved 1048576K

可以看到的是在程序结束后打印出的堆使用情况中,老年代仍存在工作线程所持有的堆内存资源,并没有被完全释放!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值