Skip to content

Commit 510ea87

Browse files
author
chenweijie
committed
add jvm学习
1 parent a7c5985 commit 510ea87

9 files changed

+151
-0
lines changed

java_pid725.hprof

26.8 MB
Binary file not shown.

java_pid731.hprof

26.8 MB
Binary file not shown.

java_pid827.hprof

26.8 MB
Binary file not shown.

java_pid840.hprof

26.8 MB
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.chen.api.util.jvm.chapter2;
2+
3+
import org.springframework.cglib.proxy.Enhancer;
4+
import org.springframework.cglib.proxy.MethodInterceptor;
5+
import org.springframework.cglib.proxy.MethodProxy;
6+
7+
import java.lang.reflect.Method;
8+
9+
/**
10+
* 使用cglib似的方法区出现内存溢出异常
11+
* <p>
12+
* -verbose:gc -Xms20M -Xmx20M -Xmn10M -Xss2M -XX:PermSize=10M -XX:MaxPermSize=10M
13+
*
14+
* @author : chen weijie
15+
* @Date: 2018-10-23 8:39 PM
16+
*/
17+
public class CGLibStackOverflowError {
18+
19+
20+
public static void main(String[] args) {
21+
22+
while (true) {
23+
Enhancer enhancer = new Enhancer();
24+
enhancer.setSuperclass(OOMObject.class);
25+
enhancer.setUseCache(false);
26+
enhancer.setCallback(new MethodInterceptor() {
27+
@Override
28+
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
29+
return methodProxy.invoke(o, args);
30+
}
31+
});
32+
enhancer.create();
33+
}
34+
}
35+
36+
37+
static class OOMObject {
38+
39+
}
40+
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chen.api.util.jvm.chapter2;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* -verbose:gc -Xms20M -Xmx20M -Xmn10M -Xss2M -XX:PermSize=10M -XX:MaxPermSize=10M
8+
* @author : chen weijie
9+
* @Date: 2018-10-23 11:09 AM
10+
*/
11+
public class DistantsPoolMemoryStackOverflowDemo {
12+
13+
14+
public static void main(String[] args) {
15+
//使用list保持常量池引用,避免full GC回收常量池行为
16+
List<String> list = new ArrayList<>();
17+
int i = 0;
18+
for (; ; ) {
19+
list.add(String.valueOf(i++).intern());
20+
}
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.chen.api.util.jvm.chapter2;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* java 堆用于存储对象实例,只要不断的创建,并且保证GC ROOTs到对象之间有可达路径来避免垃圾回收机制清楚这些对象,name对象达到最大堆的容量限制后就会产生内存溢出异常
8+
*
9+
* -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+HeapDumpOnOutOfMemoryError
10+
* @author : chen weijie
11+
* @Date: 2018-10-23 9:20 AM
12+
*/
13+
public class OutOfMemoryErrorDemo {
14+
15+
static class OOMObject {
16+
}
17+
18+
public static void main(String[] args) {
19+
List<OOMObject> list = new ArrayList<>();
20+
while (true) {
21+
list.add(new OOMObject());
22+
}
23+
}
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.chen.api.util.jvm.chapter2;
2+
3+
/**
4+
* 创建线程导致内存溢出异常
5+
* 需要将线程栈设置的大些 -Xss2M
6+
* @author : chen weijie
7+
* @Date: 2018-10-23 10:22 AM
8+
*/
9+
public class OutOfMemoryErrorForEachDemo {
10+
11+
int i =0;
12+
13+
private void dontStop(){
14+
while (true){
15+
i++;
16+
System.out.println(i);
17+
}
18+
}
19+
20+
public void stackLeakByThread(){
21+
while (true){
22+
Thread thread =new Thread(new Runnable() {
23+
@Override
24+
public void run() {
25+
dontStop();
26+
}
27+
});
28+
thread.start();
29+
}
30+
}
31+
32+
public static void main(String[] args) {
33+
OutOfMemoryErrorForEachDemo oom = new OutOfMemoryErrorForEachDemo();
34+
oom.stackLeakByThread();
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.chen.api.util.jvm.chapter2;
2+
3+
/**
4+
* 虚拟机栈和本地方法栈内存溢出测试
5+
*
6+
* -verbose:gc -Xms20M -Xmx20M -Xmn10M -Xss300k
7+
* @author : chen weijie
8+
* @Date: 2018-10-23 10:00 AM
9+
*/
10+
public class StackOverFlowErrorDemo {
11+
12+
private int stackLength = 1;
13+
14+
public void stackLeak() {
15+
stackLength++;
16+
stackLeak();
17+
}
18+
19+
public static void main(String[] args) throws Throwable {
20+
StackOverFlowErrorDemo oom = new StackOverFlowErrorDemo();
21+
try {
22+
oom.stackLeak();
23+
} catch (Throwable e) {
24+
System.out.println("stack length:" + oom.stackLength);
25+
throw e;
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)