-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPerm.java
More file actions
56 lines (44 loc) · 1.66 KB
/
Copy pathTestPerm.java
File metadata and controls
56 lines (44 loc) · 1.66 KB
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
package com.icanx.jvm.OOM;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
/*
运行参数:-XX:PermSize=1M -XX:MaxPermSize=2M
-verbose -verbose:gc-XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled
永久区域
-XX:PremSize 初始大小
-XX:MaxPremSize 最大可分配大小
使用CGLIB等库的时候,可能会产生大量的类,这些类,有可能撑爆永久区导致OOM
CGLIB 库 依赖ASM 导入JAR时 要注意版本匹配 否则报错
本次试验
CGLIB 3.1
ASM 4.2
*
* */
public class TestPerm {
public static void main(String args[]) {
while (true){
createProxy(TestPerm.class);
HashMap hashMap = new HashMap();
ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
ThreadLocal<String> threadLocal = new ThreadLocal<String>();
threadLocal.set("11111");
threadLocal.get();
}
}
public static Object createProxy(Class <?> targetClass) {
Enhancer en = new Enhancer();
en.setSuperclass(targetClass); //传入的被代理类
en.setUseCache(false); // 关闭CGLib缓存,否则总是生成同一个类
en.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
return methodProxy.invokeSuper(o,args);
}
}); //
return en.create();
}
}