当前位置:网站首页>[performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!

[performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!

2020-11-06 01:15:00 Ice team

Write it at the front

I believe that in the process of daily work , More or less, there will be a scene : out of memory . If you haven't come across this scene , That means you're a fake programmer . ha-ha , To make fun of , In the process of normal work , We do have this problem . today , I will usually work in the process of memory overflow to do a simple summary , In the form of easy to understand code cases, intuitive to share with you . Hope to be able to bring substantial help to the partners .

Case introduction

here , I will summarize the situation of memory overflow in the course of my daily work , In the form of code cases intuitive to share with you , Hope to be able to bring substantial help to the partners .

Next , We will analyze various memory overflow situations in the form of code cases .

Define the main class structure

First , We create a class called BlowUpJVM, All the case experiments are based on this class .

public class BlowUpJVM {  
} 

Stack depth overflow

public static void  testStackOverFlow(){ 
      BlowUpJVM.testStackOverFlow(); 
} 

Stack recursion , And they didn't deal with it , So the virtual machine stack goes deeper and deeper , So the stack depth overflows .

Permanent generation memory overflow

public static void testPergemOutOfMemory1(){ 
   // Method one failed  
    List<String> list = new ArrayList<String>(); 
 
   while(true){ 
      list.add(UUID.randomUUID().toString().intern()); 
   } 
} 

Plan to put String Constant pool full , I didn't expect to fail ,JDK1.7 After the constant pool is put into the heap , You can also recycle .

And then in a different way , Use cglib, use Class Fill up the old generation

public static void testPergemOutOfMemory2(){ 
   try { 
      while (true) { 
         Enhancer enhancer = new Enhancer(); 
         enhancer.setSuperclass(OOM.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(); 
      } 
   } 
   catch (Exception e){ 
      e.printStackTrace(); 
   } 
} 

The virtual machine successfully ran out of memory , that JDK Can classes generated by dynamic proxy overflow ?

public static void testPergemOutOfMemory3(){ 
   while(true){ 
   final OOM oom = new OOM(); 
   Proxy.newProxyInstance(oom.getClass().getClassLoader(), oom.getClass().getInterfaces(), new InvocationHandler() { 
         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
            Object result = method.invoke(oom, args); 
            return result; 
         } 
      }); 
   } 
} 

It turns out that ,JDK Class generated by dynamic proxy will not cause memory overflow , as a result of :JDK Class information generated by dynamic proxy , It's not going to be in the permanent generation , It's in the pile .

Local method stack overflow

public static void testNativeMethodOutOfMemory(){ 
   int j = 0; 
   while(true){ 
      Printer.println(j++); 
      ExecutorService executors = Executors.newFixedThreadPool(50); 
      int i=0; 
      while(i++<10){ 
         executors.submit(new Runnable() { 
            public void run() { 
            } 
         }); 
      } 
   } 
} 

The principle of this is to continuously create thread pools , Each thread pool is created 10 Threads , These thread pools are all in the local method area , in the course of time , The local method area overflows .

JVM Stack memory overflow

public static void testStackOutOfMemory(){ 
    while (true) {   
            Thread thread = new Thread(new Runnable() {   
                   public void run() { 
                          while(true){ 
                      } 
                   }   
            });   
            thread.start();   
     }   
} 

The creation of the thread will be directly in the JVM Create in stack , But in this case , No memory overflow , The host is hung up first , No JVM Hang up , The host is really down , No matter in mac Still windows, It's all gone .

reminder , This is going to crash .

Heap overflow

public static void testOutOfHeapMemory(){ 
   List<StringBuffer> list = new ArrayList<StringBuffer>(); 
   while(true){ 
      StringBuffer B = new StringBuffer(); 
      for(int i = 0 ; i < 10000 ; i++){ 
         B.append(i); 
      } 
      list.add(B); 
   } 
} 

Continuously adding new StringBuffer object , It overflows when it's full .

Heavy benefits

Search on wechat 【 Glacier Technology 】 WeChat official account , Focus on this deep programmer , Read hard core technology dry goods every day , Reply in official account 【PDF】 I have prepared the interview materials of the first-line large factories and my original superhard core PDF Technical documentation , And I carefully prepared for you a set of Resume Template ( Constantly updating ), I hope you can find the job you want , Learning is a matter of depression , The way to laugh now and then , come on. . If you succeed in getting into the company you want , Don't slack off , Workplace growth is the same as learning new technology , move forward , or you 'll fall behind . If we're lucky, we'll see you again !

in addition , I open source each PDF, I will continue to update and maintain , Thank you for your long-term support for the glacier !!

版权声明
本文为[Ice team]所创,转载请带上原文链接,感谢