idea内存分析工具:JProfiler 11
安装教程
Heap
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
import java.util.ArrayList;
/*
*VM Args: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
*堆的最小值和最大值都为20MB,防止自己动扩展;
*-XX:+HeapDumpOnOutOfMemoryError可以让虚拟机出现内存溢出异常时Dump出当前
*的内存堆转储快照以便事后进行分析。
*/
public class Heap_OutOfMemory {
static class OOMObject{
public static void main(String[] args) {
ArrayList<OOMObject> oomObjects = new ArrayList<>();
while (true){
oomObjects.add(new OOMObject());
}
}
}
}
虚拟机栈和本地方法栈
HotSpot虚拟机中并不区分虚拟机栈和本地方法栈
stack length:19241 Exception in thread "main" java.lang.StackOverflowError
/**
* VM Args: -Xss 180k
* The Java thread stack size specified is too small. Specify at least 180k
*
*/
public class JavaVMStackSOF {
private int stackLength = 1;
public void stackLeak(){
stackLength ++