1、运行时常量池是方法区的一部分,方法区用于存放class的相关信息,如类名、访问修饰符、常量池、字段描述、方法描述等。对于这个区域的测试,基本思路是运行时产生大量的类去填满方法区,知道溢出。虽然直接使用Java SE API也可以动态产生类(如反射时的GeneratedConstructorAccessor和动态代理等),但在本次试验使用CGLIB直接操作字节码运行时,生成大量的动态类。值得注意的是,当前主流的很多框架 如:Spring,Hibernate对类进行增强时,都会使用到类似CGLIB这类字节码技术,增强的类越多,就需要越大的方法区来保证动态生成的Class可以加载如内存。
测试代码:
public class JavaMethodAreaOOM {
public static void main(String[] args){
while (true){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(OOMObject.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
return methodProxy.invokeSuper(o,objects);
}
});
enhancer.create();
}
}
static class OOMObject{
}
}
修改虚拟机参数:
-XX:PermSize分配非堆最小内存,默认为物理内存的1/64;
-XX:MaxPermSize分配最大内存,默认为物理内存的1/4。
笔者尝试在java 1.8下运行发现,一直没有报错信息出现,直到电脑内存占完。
查询后得知:
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=10m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=10m; support was removed in 8.0
在java 1.8中取消了这两个参数。
然后笔者尝试在java 1.7下运行,测试结果如下:
Exception in thread "main"
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
报出内存溢出的错误,但是没有PermGen Space的说明,添加参数
-XX:+TraceClassLoading -XX:+TraceClassUnloading
可以查看增加的class文件来源:
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_159 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_160 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_161 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_162 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_163 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_164 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_165 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_166 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_167 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_168 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_169 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_170 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_171 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_172 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_173 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_174 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_175 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_176 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_177 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_178 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_179 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_180 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_181 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_182 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_183 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_184 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_185 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_186 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_187 from file:/E:/study/myCode/Jvm/target/classes/]
[Loaded com.example.outofmemoryerror.JavaMethodAreaOOM$OOMObject$$EnhancerByCGLIB$$df19c91_188 from file:/E:/study/myCode/Jvm/target/classes/]
可以看到,程序不断地加载一个类,同时会增加该类的各种信息,直至填充方法区,抛出错误。
方法区溢出也是一种常见的内存溢出异常,一个类要被垃圾收集器回收掉,判定条件是比较苛刻的。在经常动态生成大量Class的应用中,需要特别注意类的回收状况。这类场景除了上面提到的程序使用了CGLib字节码增强和动态语言之外,常见的还有:大量JSP或动态产生JSP文件的应用(JSP第一次运行时需要编译为Java类)、基于OSGi的应用(即使是同一个类文件,被不同的加载器加载也会视为不同的类)等。