1.先上代码:
public class OOMObject {
public byte[] bytes = new byte[128 * 1204];
public static void main(String[] args) throws InterruptedException {
// 睡五秒,用来启动Jconsole
Thread.sleep(5000);
System.out.println("start...");
fillHeep(1000);
}
private static void fillHeep(int i) throws InterruptedException {
List<OOMObject> objects = new ArrayList<OOMObject>();
for (int j = 0; j < i; j++) {
Thread.sleep(500);
objects.add(new OOMObject());
}
}
}
2.双击 xxx/bin/下的jconsole.exe 打开可视化工具
- jdk 1.8
- jdk1.7
参考:《深入理解Java虚拟机》-周志鹏