• JVM系列(二)各区域的OOM


      stack的内存溢出demo,无限递归:

    public class StackOOM {
    
        private static void fun(){
            fun();
        }
        
        /**
         * VM arg  -Xss128K
         * @param args
         */
        public static void main(String[] args) {
            fun();
        }
    
    }

       direct memory:

    public class DirectMemoryOOM {
    
        static int ONE_MB = 1024*1024;
        
        /**
         * -XX:MaxDirectMemorySize=5M
         * @param args
         * @throws InterruptedException
         */
        public static void main(String[] args) throws InterruptedException {
            List<ByteBuffer> list = new ArrayList<ByteBuffer>();
            
            for(int i=0; i<ONE_MB;++i){
                ByteBuffer buffer = ByteBuffer.allocateDirect(ONE_MB*128);        
                list.add(buffer);
                System.out.println("分派第"+(i+1)+" 个128MB");
            }
                        
        }
    
    }

      heap:

    public class HeapOOM {
    
        static int MB = 1024*1024;
        
        /**
         * -Xmx16M
         * @param args
         */
        public static void main(String[] args) {
            
            List<Object> list = new ArrayList<Object>();
            
            for(int i=0; i<1000; ++i){
                ByteBuffer bb = java.nio.ByteBuffer.allocate(MB);
                list.add(bb);
                System.out.println("分派第"+(i+1)+" MB");
            }
    
        }
    }

      常量池:

    public class ConstantPoolOOM {
    
        /**
         * -XX:MaxPermSize=10M
         * @param args
         */
        public static void main(String[] args) {
            List<Object> list = new ArrayList<Object>();
            
            for(int i=0; i<1000; ++i){
                list.add(UUID.randomUUID().toString().intern());
                System.out.println("分派第"+(i+1)+" 个");
            }
        }
    
    }

      方法区(借助cglib,大量的字节码加强,从而撑爆方法区):

    public class MethodAreaOOM {
    
        static class OOMObject {
    
        }
    
        /**
         * -XX:MaxPermSize=10M
         * @param args
         */
        public static void main(String[] args) {
    
            for (int i = 0; i < 9999; ++i) {
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(OOMObject.class);
                enhancer.setUseCache(false);
                enhancer.setCallback(new MethodInterceptor() {
                    public Object intercept(Object obj, Method method,
                            Object[] args, MethodProxy proxy) throws Throwable {
                        return proxy.invokeSuper(obj, args);
                    }
                });
                enhancer.create();
            }
        }
    }

      具体代码见github:https://github.com/emmerichLuang/differentOOM

  • 相关阅读:
    祖国,让我为您写首歌
    提升信任度,是做网络营销成功的必由之路
    名字作诗已成流行语,你“OUT”了吧?
    腾飞天涯
    沈阳首个文化强市评价体系出炉,为沈阳文化振兴打分
    寄语“官员诗人”车延高:我挺你
    “自己选的路,就算跪着也要走完”引热议
    我在月光下想你
    国庆献礼谁为重
    gcc和g++的区别
  • 原文地址:https://www.cnblogs.com/ELMND/p/4630070.html
Copyright © 2020-2023  润新知