服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - JAVA 内存溢出案例汇总

JAVA 内存溢出案例汇总

2020-07-22 14:19冰河团队 Java教程

这篇文章主要介绍了JAVA 内存溢出案例的汇总,文中讲解非常细致,帮助各位工作学习时避免内存溢出,感兴趣的朋友可以了解下

写在前面

作为程序员,多多少少都会遇到一些内存溢出的场景,如果你还没遇到,说明你工作的年限可能比较短,或者你根本就是个假程序员!哈哈,开个玩笑。今天,我们就以Java代码的方式来列举几个典型的内存溢出案例,希望大家在日常工作中,尽量避免写这些low水平的代码。

定义主类结构

首先,我们创建一个名称为BlowUpJVM的类,之后所有的案例实验都是基于这个类进行。如下所示。

?
1
2
public class BlowUpJVM {
}

栈深度溢出

?
1
2
3
public static void testStackOverFlow(){
   BlowUpJVM.testStackOverFlow();
}

栈不断递归,而且没有处理,所以虚拟机栈就不断深入不断深入,栈深度就这样溢出了。

永久代内存溢出

?
1
2
3
4
5
6
7
public static void testPergemOutOfMemory1(){
  //方法一失败
  List<String> list = new ArrayList<String>();
  while(true){
   list.add(UUID.randomUUID().toString().intern());
  }
}

打算把String常量池堆满,没想到失败了,JDK1.7后常量池放到了堆里,也能进行垃圾回收了。

然后换种方式,使用cglib,用Class把老年代取堆满

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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();
  }
}

虚拟机成功内存溢出了,那JDK动态代理产生的类能不能溢出呢?

?
1
2
3
4
5
6
7
8
9
10
11
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;
     }
   });
  }
}

事实表明,JDK动态代理差生的类不会造成内存溢出,原因是:JDK动态代理产生的类信息,不会放到永久代中,而是放在堆中。

本地方法栈溢出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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() {
      }
     });
   }
  }
}

这个的原理就是不断创建线程池,而每个线程池都创建10个线程,这些线程池都是在本地方法区的,久而久之,本地方法区就溢出了。

JVM栈内存溢出

?
1
2
3
4
5
6
7
8
9
10
11
public static void testStackOutOfMemory(){
  while (true) { 
      Thread thread = new Thread(new Runnable() { 
          public void run() {
             while(true){
           }
          
      }); 
      thread.start(); 
   
}

线程的创建会直接在JVM栈中创建,但是本例子中,没看到内存溢出,主机先挂了,不是JVM挂了,真的是主机挂了,无论在mac还是在windows,都挂了。

温馨提示,这个真的会死机的。

堆溢出

?
1
2
3
4
5
6
7
8
9
10
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);
  }
}

不断往堆中塞新增的StringBuffer对象,堆满了就直接溢出了。

测试案例完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class BlowUpJVM {
  //栈深度溢出
  public static void testStackOverFlow(){
    BlowUpJVM.testStackOverFlow();
    }
  
  //不能引起永久代溢出
  public static void testPergemOutOfMemory1(){
    //方法一失败
    List<String> list = new ArrayList<String>();
    while(true){
     list.add(UUID.randomUUID().toString().intern());
    }
  }
  
  //永久代溢出
  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();
    }
  }
  
  //不会引起永久代溢出
  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;
       }
     });
    }
  }
  
  //本地方法栈溢出
  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() {
        }
       });
     }
    }
  }
  
  //JVM内存溢出
  public static void testStackOutOfMemory(){
    while (true) { 
        Thread thread = new Thread(new Runnable() { 
            public void run() {
               while(true){
             }
            
        }); 
        thread.start(); 
     
  }
  
  //堆溢出
  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);
    }
  }
}

最后,附上并发编程需要掌握的核心技能知识图,祝大家在学习并发编程时,少走弯路。

JAVA 内存溢出案例汇总

以上就是JAVA 内存溢出案例汇总的详细内容,更多关于JAVA 内存溢出的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/binghe001/p/12952772.html

延伸 · 阅读

精彩推荐