Java方法区溢出

原文地址:http://blog.csdn.net/kevin_luan/article/details/22982255

方法区用于存放Class的相关信息,如:类名,访问修饰符,常量池,字符描述,方法描述等。对于这个区域的测试,基本思路是运行时产生大量的类去填满方法区,直到溢出。虽然直接使用Java SE API也可以动态产生类(如反射时的GeneratedConstructorAccessor和动态代理等),但在本次试验使用CGLIB直接操作字节码运行时,生成大量的动态类。

值得注意的是,当前主流的很多框架 如:Spring,Hibernate对类进行增强时,都会使用到类似CGLIB这类字节码技术,增强的类越多,就需要越大的方法区来保证动态生成的Class可以加载入内存。例如:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.lang.reflect.Method;  
  2.   
  3. import net.sf.cglib.proxy.Enhancer;  
  4. import net.sf.cglib.proxy.MethodInterceptor;  
  5. import net.sf.cglib.proxy.MethodProxy;  
  6. /** 
  7.  * VM args -XX:PermSize=10M -XX:MaxPermSize=10M 
  8.  * 
  9.  */  
  10. public class JavaMethodAreaOOM {  
  11.     public static void main(String[] args) {  
  12.         while (true) {  
  13.             Enhancer enhancer = new Enhancer();  
  14.             enhancer.setSuperclass(OOM.class);  
  15.             enhancer.setUseCache(false);  
  16.             enhancer.setCallback(new MethodInterceptor() {  
  17.   
  18.                 @Override  
  19.                 public Object intercept(Object obj, Method arg1, Object[] args, MethodProxy proxy) throws Throwable {  
  20.                     return proxy.invokeSuper(obj, args);  
  21.                 }  
  22.             });  
  23.             OOM oom = (OOM) enhancer.create();  
  24.             oom.sayHello("Kevin LUAN");  
  25.         }  
  26.     }  
  27.   
  28.     static class OOM {  
  29.         public String sayHello(String str) {  
  30.             return "HI " + str;  
  31.         }  
  32.     }  
  33. }  

Caused by: java.lang.OutOfMemoryError:PermGen space


JDK 1.7 64位运行结果如下:

java version "1.7.0_45"

Java(TM) SE Runtime Environment (build 1.7.0_45-b18)

Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)


Exception in thread "main" 

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"

方法区溢出也是一种常见的内存溢出异常,一个类如果要被垃圾回收器回收掉,判定条件非常苛刻,在经常动态生成大量Class的应用中,需要特别注意类的回收状况。这类场景除了上面提到的程序使用GCLIB字节码增强外,常见的还用JSP或动态产生JSP文件的应用(JSP第一次运行时需要编译为JAVA类),基于OSGI的应用(即使是同一个类文件,被不同的加载器加载也会视为不同的类)等。

#增加JVM 参数来快速定位下 Class load ,来定位下。

-XX:+TraceClassLoading -XX:+TraceClassUnloading

输出格式:[Loaded sun.rmi.server.LoaderHandler from /usr/local/java/jdk1.7.0/jre/lib/rt.jar]可以方便定位出增加的CLASS文件来源
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值