* 探究JVM 堆内存溢出,堆当中存储的是对象的实例,只要我们不断的new对象实例 就OK * @author pengchengliu * JVM Args: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError * */ publicclass{ staticclassOOMObject{} publicstaticvoidmain(String[] args){ List<OOMObject> list = new ArrayList<OOMObject>(); while (true) { list.add(new OOMObject()); } } } java.lang.OutOfMemoryError: Java heap space Dumping heap to java_pid795.hprof ... Heap dump file created [27575251 bytes in 0.114 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 outofmemoryerror.HeapOOM.main(HeapOOM.java:19)
当JVM堆当中出现OOM的异常还是比较常见的,当JVM当中出现堆内存溢出的时候。控制台上面便会抛出OOM异常,紧接着便会在后面出现 Java heap space 这样的字样,如果出现了这样的字样的时候,便可以得出当前的JVM堆现在状态便是内存溢出的状态。那么关于JVM堆内存溢出我们应该如何解决呢?