寫在前面
作為程序員,多多少少都會(huì)遇到一些內(nèi)存溢出的場(chǎng)景,如果你還沒(méi)遇到,說(shuō)明你工作的年限可能比較短,或者你根本就是個(gè)假程序員!哈哈,開(kāi)個(gè)玩笑。今天,我們就以Java代碼的方式來(lái)列舉幾個(gè)典型的內(nèi)存溢出案例,希望大家在日常工作中,盡量避免寫這些low水平的代碼。
定義主類結(jié)構(gòu)
首先,我們創(chuàng)建一個(gè)名稱為BlowUpJVM的類,之后所有的案例實(shí)驗(yàn)都是基于這個(gè)類進(jìn)行。如下所示。
1
2
|
public class BlowUpJVM { } |
棧深度溢出
1
2
3
|
public static void testStackOverFlow(){ BlowUpJVM.testStackOverFlow(); } |
棧不斷遞歸,而且沒(méi)有處理,所以虛擬機(jī)棧就不斷深入不斷深入,棧深度就這樣溢出了。
永久代內(nèi)存溢出
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常量池堆滿,沒(méi)想到失敗了,JDK1.7后常量池放到了堆里,也能進(jìn)行垃圾回收了。
然后換種方式,使用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(); } } |
虛擬機(jī)成功內(nèi)存溢出了,那JDK動(dòng)態(tài)代理產(chǎn)生的類能不能溢出呢?
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; } }); } } |
事實(shí)表明,JDK動(dòng)態(tài)代理差生的類不會(huì)造成內(nèi)存溢出,原因是:JDK動(dòng)態(tài)代理產(chǎn)生的類信息,不會(huì)放到永久代中,而是放在堆中。
本地方法棧溢出
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() { } }); } } } |
這個(gè)的原理就是不斷創(chuàng)建線程池,而每個(gè)線程池都創(chuàng)建10個(gè)線程,這些線程池都是在本地方法區(qū)的,久而久之,本地方法區(qū)就溢出了。
JVM棧內(nèi)存溢出
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(); } } |
線程的創(chuàng)建會(huì)直接在JVM棧中創(chuàng)建,但是本例子中,沒(méi)看到內(nèi)存溢出,主機(jī)先掛了,不是JVM掛了,真的是主機(jī)掛了,無(wú)論在mac還是在windows,都掛了。
溫馨提示,這個(gè)真的會(huì)死機(jī)的。
堆溢出
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對(duì)象,堆滿了就直接溢出了。
測(cè)試案例完整代碼
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(); } } //不會(huì)引起永久代溢出 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內(nèi)存溢出 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); } } } |
最后,附上并發(fā)編程需要掌握的核心技能知識(shí)圖,祝大家在學(xué)習(xí)并發(fā)編程時(shí),少走彎路。
以上就是JAVA 內(nèi)存溢出案例匯總的詳細(xì)內(nèi)容,更多關(guān)于JAVA 內(nèi)存溢出的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://www.cnblogs.com/binghe001/p/12952772.html