一:JVM內存分佈圖:java
二:JVM內存常見內存問題:ide
1 HeapOutOfMemory實戰演示oop
import java.util.ArrayList; import java.util.List; /** * Created by hadoop on 2016/5/2. */ public class HeapOutOfMemory { public static void main(String arts[]){ List<Integer> list = new ArrayList<>(); int count = 0; for(;;){ list.add(++count); } } }
idea參數調整:idea
-verbose:gc -Xms10M -Xmx10M -Xss128k -XX:+PrintGCDetails
運行結果:code
2 StackOverFlow實戰演示內存
public class StackOverFlow { public static void count(){ count(); } public static void main(String args[]){ count(); } }
運行結果:
hadoop
3 JVM:ConstantOutOfMemory實戰演示string
public class ConstantOutOfMemory { public static void main(String arts[]){ List<String> stringList = new ArrayList<>(); int item = 0; while(true){ stringList.add(String.valueOf(item++).intern()); } } }
運行結果:it
4 DirectMemoryOutOfMemoryio
import java.nio.ByteBuffer; public class HelloDirectMemoryOutOfmemory { private static final int ONE_GB = 1024*1024*1024; private static int count = 1; public static void main(String[] args) { try { while (true) { ByteBuffer buffer = ByteBuffer.allocateDirect(ONE_GB); count++; } } catch (Exception e) { System.out.println("Exception:instance created "+count); e.printStackTrace(); } catch (Error e) { System.out.println("Error:instance created "+count); e.printStackTrace(); } } }
運行結果: