Skip to content

Commit fe3fd09

Browse files
committed
四种JVM内存溢出演示
1 parent 6208130 commit fe3fd09

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.prd.jvm;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 演示常量池溢出问题
8+
* -verbose:gc -Xms10M -Xmx10M -Xss128k -XX:+PrintGCDetails
9+
*/
10+
public class HelloConstantOutOfMemory {
11+
12+
public static void main(String[] args) {
13+
14+
List<String> stringList = new ArrayList<>();
15+
int item = 0;
16+
while (true) {
17+
stringList.add(String.valueOf(item++).intern());
18+
}
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.prd.jvm;
2+
3+
import java.nio.ByteBuffer;
4+
5+
/**
6+
* 通过NIO测试直接内存溢出
7+
* -verbose:gc -Xms10M -Xmx10M -XX:MaxDirectMemorySize=10M -Xss128k -XX:+PrintGCDetails
8+
*/
9+
public class HelloDirectMemoryOutOfMemory {
10+
private static final int ONE_MB=1024*1024*1024;
11+
12+
private static int count=1;
13+
14+
public static void main(String[] args) {
15+
while(true) {
16+
ByteBuffer buffer = ByteBuffer.allocateDirect(ONE_MB);
17+
count++;
18+
}
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.prd.jvm;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 堆内存溢出样式
8+
* -verbose:gc -Xms10M -Xmx10M -XX:MaxDirectMemortSize=5M
9+
* -Xss128k -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./heapdump.hprof
10+
*/
11+
public class HelloHeapOutOfMemory {
12+
13+
private static class Person{}
14+
15+
public static void main(String[] args) {
16+
System.out.println("HelloHeapOutOfMemory");
17+
List<Person> personList = new ArrayList();
18+
int nums = 0;
19+
while(true) {
20+
personList.add(new Person());
21+
System.out.println("构造第"+(++nums)+"个实例");
22+
}
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.prd.jvm;
2+
3+
/**
4+
* 演示栈空间溢出
5+
* -verbose:gc -Xms10M -Xmx10M -Xss128k -XX:+PrintGCDetails
6+
* Stack栈,是线程私有的
7+
*/
8+
public class HelloStackOverFlow {
9+
private int nums;
10+
11+
public void count() {
12+
nums++;
13+
count();
14+
}
15+
16+
public static void main(String[] args) {
17+
System.out.println("HelloStackOverFlow");
18+
HelloStackOverFlow flow = new HelloStackOverFlow();
19+
flow.count();
20+
}
21+
}

0 commit comments

Comments
 (0)