java開發過程內存報錯是常見bug,圍繞內存問題,今天參考周志明的深刻理解java虛擬機理解。php
如圖,共享數據區包括 方法區和堆;隔離數據區包括虛擬堆棧和本地方法棧,程序計數器。java
注意:棧記錄的是局部變量,類中的int 等,方法區記錄的是靜態變量: static int.兩者此處不一樣區域。eclipse
java堆溢出:spa
/** * VM Args:-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError vm參數是傳給虛擬機的,程序參數是傳給main; */ public class HeapOOM { static class OOMObject { } public static void main(String[] args) { List<OOMObject> list = new ArrayList<OOMObject>(); while (true) { list.add(new OOMObject()); } } }
效果線程
[GC (Allocation Failure) [PSYoungGen: 7716K->1016K(9216K)] 7716K->5225K(19456K), 0.0114724 secs] [Times: user=0.06 sys=0.00, real=0.05 secs] [GC (Allocation Failure) --[PSYoungGen: 9208K->9208K(9216K)] 13417K->19440K(19456K), 0.0214058 secs] [Times: user=0.08 sys=0.00, real=0.02 secs] [Full GC (Ergonomics) [PSYoungGen: 9208K->0K(9216K)] [ParOldGen: 10232K->9865K(10240K)] 19440K->9865K(19456K), [Metaspace: 2526K->2526K(1056768K)], 0.4930188 secs] [Times: user=0.52 sys=0.00, real=0.49 secs] [Full GC (Ergonomics) [PSYoungGen: 7630K->8076K(9216K)] [ParOldGen: 9865K->7755K(10240K)] 17495K->15832K(19456K), [Metaspace: 2526K->2526K(1056768K)], 0.4078616 secs] [Times: user=0.41 sys=0.00, real=0.41 secs] [Full GC (Allocation Failure) [PSYoungGen: 8076K->8074K(9216K)] [ParOldGen: 7755K->7755K(10240K)] 15832K->15829K(19456K), [Metaspace: 2526K->2526K(1056768K)], 0.4094363 secs] [Times: user=0.44 sys=0.00, real=0.41 secs] java.lang.OutOfMemoryError: Java heap space Dumping heap to java_pid6376.hprof ... Heap dump file created [27904125 bytes in 0.355 secs] [Full GC (Ergonomics) [PSYoungGen: 8192K->0K(9216K)] [ParOldGen: 7755K->485K(10240K)] 15947K->485K(19456K), [Metaspace: 2552K->2552K(1056768K)], 0.0377330 secs] [Times: user=0.02 sys=0.00, real=0.04 secs] Heap PSYoungGen total 9216K, used 82K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000) eden space 8192K, 1% used [0x00000000ff600000,0x00000000ff614938,0x00000000ffe00000) from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000) to space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000) ParOldGen total 10240K, used 485K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000) object space 10240K, 4% used [0x00000000fec00000,0x00000000fec79428,0x00000000ff600000) Metaspace used 2558K, capacity 4486K, committed 4864K, reserved 1056768K class space used 274K, capacity 386K, committed 512K, reserved 1048576K 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 Test.main(Test.java:14)
用Memory Analyzer 分析內存:code
下載http://www.eclipse.org/mat/downloads.php對象
載入dump文件,生命週期
查看java內存映像:進程
1,用jps查看java運行進程的lvmid;ip
2,jmap生成dump文件;
3,jhat查看dump文件