一:JVM内存分布图:
二:JVM内存常见内存问题:
1 HeapOutOfMemory实战演示
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参数调整:
-verbose:gc -Xms10M -Xmx10M -Xss128k -XX:+PrintGCDetails
运行结果:
2 StackOverFlow实战演示
public class StackOverFlow {
public static void count(){
count();
}
public static void main(String args[]){
count();
}
}
运行结果:
3 JVM:ConstantOutOfMemory实战演示
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());
}
}
}
运行结果:
4 DirectMemoryOutOfMemory
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();
}
}
}
运行结果: