spring-mvc - 为什么 Spring Context 没有优雅地关闭?

标签 spring-mvc memory-leaks permgen servlet-listeners servletcontextlistener

停止取消部署/重新部署基于Spring框架3.0.5的Web应用程序时,Tomcat7的catalina中会记录以下错误.out:

SEVERE: The web application [/nomination##1.0-qa] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@4f43af8f]) and a value of type [org.springframework.security.core.context.SecurityContextImpl] (value [org.springframework.security.core.context.SecurityContextImpl@ffffffff: Null authentication]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

我最初想到在那里实现 ServletContextListenerclose() 上下文。然而,发现实现了 ServletContextListenerContextLoaderListener 是像 web.xml 中这样设置的:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

来自 Javadocs:

**contextDestroyed**

public void contextDestroyed(ServletContextEvent event)
Close the root web application context.
Specified by:
contextDestroyed in interface ServletContextListener

所以,我的问题是为什么 ThreadLocal 没有被 ContextLoaderListener.contextDestroyed() 干净地释放?

我们遇到了 PermGen 错误,并在调查时发现了这一点。有几个地方有类似下面的代码:

ApplicationContext context = WebApplicationContextUtils
            .getWebApplicationContext(se.getSession().getServletContext());

MyBeanClass x = context.getBean(
            "myBean", MyBeanClass.class);
x.someMethod(); 

我想知道上面的代码片段是否阻止了干净的关闭? 任何指点将不胜感激。

最佳答案

hotdeploy 有很多问题需要您注意:

1-注销数据库驱动程序 see here .

2-在多任务应用程序中关闭任务:您可以在开发模式下延迟近 1 小时的重新启动。

3-杀死 spring 的上下文:上面已经做了,但注意不要导入 XML 形式的 XML 。

4-Kill JVM内存中存在的缓存对象:make小对象,你正在初始化吗 构造函数中的bean?将其更改为无构造函数以继续在方法范围内杀死它们! 类中有多少个方法调用bean?如果许多方法调用bean不希望java在从作用域出来后杀死对象,jvm将保留它以提高性能......,所以让你的类保持小!。

你的代码怎么样?您是否将变量声明为循环外?使用后你是=null列表还是对象?

5-您可以增加tomcat的启动时间和停止时间。

也可以将rebel或springboot项目视为助手。

关于spring-mvc - 为什么 Spring Context 没有优雅地关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16464420/

相关文章:

java - 如何在 Eclipse 中创建 Spring MVC 项目?

c - 那段代码会不会泄漏内存?

java - 生产服务器上的 Tomcat、PermGen 和重新部署

java - 高 PermGen,但堆使用率低

c - 动态数组和结构 C 的 Valgrind 内存泄漏

java - 如何减少 web-app 中用于 perm gen 的内存

java - Spring MVC 中的数据表

javascript - Spring RESTful GET方法具有相同的参数和不同的返回类型

spring - Spring启动服务器初始化后,抛出InvocationTargetException和ApplicationContextException

c - 词频程序内存泄漏