Summary of five memory overflow cases: including stack deep overflow, permanent generation memory overflow, local method stack overflow, JVM stack memory overflow and heap overflow

Hello, I'm glacier~~

I believe that in the process of their daily work, they will more or less encounter a scene: memory overflow. If you haven't encountered this scenario, you are a fake programmer. Ha ha, just kidding, we do encounter this problem during our normal work. Today, I will make a simple summary of the memory overflow encountered in my usual work and share it with you intuitively in the form of easy to understand code cases. I hope it can bring substantive help to my friends.

Case introduction

Here, I will share the memory overflow summarized in my daily work with you intuitively in the form of code cases, hoping to bring substantive help to my friends.

Next, we will analyze various memory overflows in the form of code cases.

Define main class structure

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

public class BlowUpJVM {  
} 

Stack depth overflow

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

The stack recurses continuously and is not processed, so the virtual machine stack goes deeper and deeper, and the stack depth overflows.

Permanent generation memory overflow

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

It was planned to fill the String constant pool, but it failed. After JDK1.7, the constant pool was placed in the heap and garbage can be collected.

Then, in another way, use cglib and use Class to fill up the old proxy

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 has successfully overflowed memory. Can the classes generated by JDK dynamic agent 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; 
         } 
      }); 
   } 
} 

Facts show that the classes of poor students in JDK dynamic agent will not cause memory overflow. The reason is that the class information generated by JDK dynamic agent will not be put in the permanent generation, but in the heap.

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, and each thread pool creates 10 threads. These thread pools are in the local method area. Over 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();   
     }   
} 

Threads will be created directly in the JVM stack, but in this example, no memory overflow is seen. The host hangs first, not the JVM, but the host hangs, both on mac and windows.

Warm tip, this will really 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 fill the heap with new StringBuffer objects, and if the heap is full, it will overflow directly.

Write at the end

If you want to enter a big factory, want to be promoted and raised, or are confused about your existing work, you can communicate with me privately. I hope some of my experience can help you~~

Recommended reading:

Well, that's all for today. Let's praise, collect and comment. Let's walk up three times with one button. I'm glacier. I'll see you next time~~

👇🏻 Search below the official account to pay attention to me. 👇🏻

Tags: Java Programmer Optimize

Posted on Mon, 22 Nov 2021 19:24:29 -0500 by rachelkoh