在老外http://vard-lokkur.blogspot.com/2012/12/springs-web-mvc-redirect-to-memory-leak.html一文中,谈到了他发现的一个spring mvc 3以及之前版本可能存在的一个
redirect引起的内存泄露问题。例子为:
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
次,没报错,但就停止了,很奇怪,大家可以探讨尝试下。