矛盾点
查阅网上资料,部分博客写到:当一个线程抛出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
可以看到的是在程序结束后打印出的堆使用情况中,老年代仍存在工作线程所持有的堆内存资源,并没有被完全释放!!!