jvm内存映像分析
- - ITeye博客 jdk自带的jmap就是java内存映像工具,可以用于上生成堆转储快照:. 在eclipse中启动一个java类,打开jdk安装目录下的C:\Program Files\Java\jdk1.6.0_11\bin目录,双击jconsole.exe,显示连接窗口:. ,单击pid为6920的选项,点连接进入,可以看到jvm运行时的多种参数,.
package com.chinaso.phl; import java.lang.reflect.Field; /** * @author piaohailin * @date 2013-12-24 */ public class Server { private int stackLength = 1; public void stackLeak() { stackLength++; stackLeak(); } /** * @param args * @author piaohailin * @throws Throwable * @date 2013-12-24 */ public static void main(String[] args) throws Throwable { //1 Java堆溢出 // List<Server> list = new ArrayList<Server>(); // while (true) { // list.add(new Server()); // } //2 虚拟机栈和本地方法栈溢出 // Server s = new Server(); // try { // s.stackLeak(); // } catch (Throwable t) { // System.out.println("stack length:" + s.stackLength); // throw t; // } //3 运行时常量池溢出 // List<String> list = new ArrayList<String>(); // int i = 0; // while (true) { //// list.add(String.valueOf(i++).intern()); // list.add(new String(String.valueOf(i++))); // } //4 方法区溢出 // while (true) { // Enhancer enhancer = new Enhancer(); // enhancer.setSuperclass(OOMObject.class); // enhancer.setUseCache(false); // enhancer.setCallback(new MethodInterceptor() { // @Override // public Object intercept(Object obj, // Method method, // Object[] args, // MethodProxy proxy) throws Throwable { // return proxy.invokeSuper(obj, args); // } // }); // enhancer.create(); // } //5 本机直接内存溢出 Field unsafeField = sun.misc.Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible(true); sun.misc.Unsafe unsafe = (sun.misc.Unsafe) unsafeField.get(null); while (true) { unsafe.allocateMemory(1024 * 1024); } } static class OOMObject { } }