深入理解java虚拟机(第一篇)

在eclipse中查看内存溢出的案例:

package main;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘卫卫
 * 2018年9月12日下午3:22:49
 */
public class HeapOOM {

	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		while(true) {
			list.add("OOM");
		}
	}
}

在debug标签中设置jvm参数

《深入理解java虚拟机(第一篇)》

限制java堆的大小为20M,并且不可扩展。这里讲Xms,Xmx(堆的最小值和最小值)都设置为20M即可避免对自动扩展。

对溢出打印的信息:

[GC (Allocation Failure) [PSYoungGen: 7622K->664K(9216K)] 7622K->2782K(19456K), 0.0052667 secs] [Times: user=0.00 sys=0.00, real=0.04 secs] 
[Full GC (Ergonomics) [PSYoungGen: 8734K->0K(9216K)] [ParOldGen: 9240K->7696K(10240K)] 17974K->7696K(19456K), [Metaspace: 2605K->2605K(1056768K)], 0.0675706 secs] [Times: user=0.13 sys=0.00, real=0.07 secs] 
[GC (Allocation Failure) [PSYoungGen: 0K->0K(9216K)] 7696K->7696K(19456K), 0.0005580 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(9216K)] [ParOldGen: 7696K->7673K(10240K)] 7696K->7673K(19456K), [Metaspace: 2605K->2605K(1056768K)], 0.0313635 secs] [Times: user=0.09 sys=0.00, real=0.03 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:261)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
	at java.util.ArrayList.add(ArrayList.java:458)
	at main.HeapOOM.main(HeapOOM.java:15)
Heap
 PSYoungGen      total 9216K, used 241K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 8192K, 2% used [0x00000000ff600000,0x00000000ff63c638,0x00000000ffe00000)
  from space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
  to   space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
 ParOldGen       total 10240K, used 7673K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 74% used [0x00000000fec00000,0x00000000ff37e548,0x00000000ff600000)
 Metaspace       used 2637K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 280K, capacity 386K, committed 512K, reserved 1048576K

java.lang.OutOfMemoryError: Java heap space

次错误指示内存溢出,而且是堆内存

 

    原文作者:java虚拟机
    原文地址: https://blog.csdn.net/nuc2015/article/details/82663292
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞