异常现象
异常信息如下
java.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2374) at org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:846) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1313) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1172) at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2625) at java.lang.Class.getDeclaredMethods(Class.java:1868) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:383) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:321) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:830) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:493) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5278) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3859) at org.apache.catalina.startup.HostConfig.reload(HostConfig.java:1410) at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1383) at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1619)
剖析原因
嗯。。。很眼熟吧,这个是jdk1.8以下特有的异常了(jdk1.8换成了meta space了(类似于ArrayList会自动扩容),缺点是会占用其他内存)。我们用jvisualvm查看下
点击监视勾上内存,从堆上切换到PermGen(永久代)上。(稍微解释一下,jvm数据区分为堆和方法区。堆是存储对象的,方法区是存储类信息、常量池、静态变量之类的。从JMM上来看,年轻代和老年代是堆,永久带是方法区)
发现PermGen的使用大小和PermGen大小一样。这种情况就是方法区(永久代)不够用了。
解决办法
开发中
如果是在开发中,没有stop直接redeploy,你会发现PermGen会直线上升。这种情况直接stop就好了,因为在线程启动时类信息就被加载到方法区中,线程不死之前的类信息就一直存在。
线上
如果是在线上跑着出现这种问题,直接设置方法区初始大小-XX:PermSize和方法区最大大小-XX:MaxPermSize