探讨:spring mvc中redirect导致内存泄露?

来源:互联网 发布:sql max函数 编辑:程序博客网 时间:2024/06/04 18:06

转自:http://jackyrong.iteye.com/blog/1744342


在老外http://vard-lokkur.blogspot.com/2012/12/springs-web-mvc-redirect-to-memory-leak.html一文中,谈到了他发现的一个spring mvc 3以及之前版本可能存在的一个 

redirect引起的内存泄露问题。例子为: 

Java代码  收藏代码
  1. @RequestMapping(method = RequestMethod.POST)  
  2. public String onPost(...) {  
  3.     ...  
  4.     return "redirect:form.html?entityId=" + entityId;  
  5. }  


  You may start from reading Resolving views in Spring Framework documentation, and then take a closer look at the source code of AbstractCachingViewResolver, which is base class for many different View Resolvers in Spring, including: JSP, FreeMarker, Velocity, Jasper Reports, Tiles and XSLT view resolvers. 

When resolveViewName method is called on AbstractCachingViewResolver it uses HashMap based view cache to speed up view resolving in the future calls, and cache key is by default created using view name and current locale. 

Now to the clue - when you use the above method of redirecting, Spring Framework uses the whole String returned from your controller's method as the view name, including all parameters included in the target URL. Each time you perform the redirect, the parameters may vary, thus such a redirect will leave one additional entry in view cache of  AbstractCachingViewResolver, causing memory leak. 


    他认为,只要这个形式,多次的执行的话(他执行了7000多次),看JVM大小设置问题会导致内存泄露,原因他分析说,AbstractCachingViewResolver,这个代码是众多视图解析的父类了, 
当 resolveViewName 方法调用AbstractCachingViewResolver时,使用的是hashmap 
缓存去加速,而key是默认使用viewname和当前的locale.而spring当调用 
redirect这个时候,使用的是整个从controller方法中返回的字符串,做为view name,包括你的目标url,所以导致了memory leak(但为什么呢?好像原文作者没具体分析出来) 
      作者还在https://jira.springsource.org/browse/SPR-10065开了一个jira说明, 
有兴趣的读者可以去看下。他测试的DEMO的例子在: 
https://github.com/vardlokkur/webapp-02可以下载。 
   不过可惜我在自己的机器上,chrome下执行7000多次,没发现有问题,但IE下执行到1040 

次,没报错,但就停止了,很奇怪,大家可以探讨尝试下。 




这个问题确实存在,主要原因是因为 springmvc 会缓存 view 的key。关键代码如下:

Java代码  收藏代码
  1. public abstract class AbstractCachingViewResolver extends WebApplicationObjectSupport implements ViewResolver {  
  2.     /** Whether we should cache views, once resolved */  
  3.     private boolean cache = true;  
  4.   
  5.     /** Map from view key to View instance */  
  6.     private final Map<Object, View> viewCache = new HashMap<Object, View>();  
  7.   
  8.     //... set/get方法略  
  9.   
  10.     public View resolveViewName(String viewName, Locale locale) throws Exception {  
  11.         if (!isCache()) {  
  12.             return createView(viewName, locale);  
  13.         } else {   
  14.             Object cacheKey = getCacheKey(viewName, locale);  
  15.             synchronized (this.viewCache) {  
  16.                 View view = this.viewCache.get(cacheKey);  
  17.                 if (view == null && (!this.cacheUnresolved || !this.viewCache.containsKey(cacheKey))) {  
  18.                     // Ask the subclass to create the View object.  
  19.                     view = createView(viewName, locale);  
  20.                     if (view != null || this.cacheUnresolved) {  
  21.                         this.viewCache.put(cacheKey, view);  
  22.                         if (logger.isTraceEnabled()) {  
  23.                             logger.trace("Cached view [" + cacheKey + "]");  
  24.                         }  
  25.                     }  
  26.                 }  
  27.                 return view;  
  28.             }  
  29.         }  
  30.     }  
  31. }  

以上代码定义了一个 cache的标识位,默认为 true,并定义了一个 Map 来缓存 View,
如果在 controller 返回的 view 是不固定的,如:"redirect:form.html?entityId=" + entityId,由于 entityId 的值会存在 N 个,那么会导致产生 N 个 ViewName 被缓存起来。
由于内存大小是有限的,所以当 N 大到一定数量时,会产生内存溢出。

个人认为,这并不能算是 spring 的bug,更多的应当是我们的使用方式导致。
为了解决这个问题,spring 提供了其他的方案:http://static.springsource.org/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/reference/htmlsingle/#mvc-ann-redirect-attributes

方法修改如下:
Java代码  收藏代码
  1. @RequestMapping(method = RequestMethod.POST)    
  2. public String onPost(RedirectAttributes redirectAttrs) {    
  3.     // ...  
  4.     redirectAttrs.addAttribute("entityId", entityId)  
  5.     return "redirect:form.html?entityId={entityId}";    
  6. }   

如上,使用占位符后, viewName 的数量被限制为了1个。

原创粉丝点击