Quantcast
Channel: IT社区推荐资讯 - ITIndex.net
Viewing all articles
Browse latest Browse all 15843

JVM内存分析系列(二)内存溢出的类型分析

$
0
0
Java堆溢出
虚拟机栈和本地方法栈溢出
运行时常量池溢出
方法区溢出
本机直接内存溢出


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 {
    }
}



已有 0人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐




Viewing all articles
Browse latest Browse all 15843

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>