问题:生产服务器,运行段时间后会导致微服务内存溢出,服务不可用,通过查看hprof文件,发现是由于类加载器导致的内存溢出
解决:
<dependency>
<groupId>se.jiderhamn.classloader-leak-prevention</groupId>
<artifactId>classloader-leak-prevention-servlet3</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>se.jiderhamn</groupId>
<artifactId>classloader-leak-prevention</artifactId>
<version>1.15.4</version>
</dependency>
启动类注入
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new ClassLoaderLeakPreventor());
return servletListenerRegistrationBean;
}
具体原理可参考以下博客:
classloader-leak-prevention01
classloader-leak-prevention02
spring boot开发项目,部署到tomcat,启动报错
两款内存分析工具:
mat:mat
IBM HeapAnalyzer:IBM HeapAnalyzer