You probably well know redirect after post pattern. Using Spring Framework you have few ways to implement it, let's focus on one of them, returning the target URL as the String with redirect: prefix.
Suppose that we have controller using this method of redirecting, and we have to pass some parameters during the redirect, let it be some entity ID for example:
@RequestMapping(method = RequestMethod.POST)
public String onPost(...) {
...
return "redirect:form.html?entityId=" + entityId;
}
As you see, our rock doesn't look dangerous, it even doesn't look suspicious :) - What the heck is wrong with that?! - you may ask. Well, to explain that, we have to take a look at the way how Spring Framework handles the value returned by you.
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.
How soon that will kill my application? - you may ask. That depends on the amount of memory assigned to JVM, and the number of performed redirects :) - I've made some tests using: -Xmx64M option, with simple application build from only one controller - see this example. After about 76400 redirects the application died with OutOfMemoryError: Java heap space.
Follow-ups:
This article has been republished on Java Code Geeks (12/07/2012), and on Dzone's Javalobby (12/10/2012).
Have your reported this issue to Spring Commiters?
ReplyDeleteYes, I did - see: AbstractCachingViewResolver - caching redirect views leads to memory leak
Deleteregards
What an informative topic that was! But I would like to help you and shed more light on the topic. I would like to recommend a company that provides reliable and flexible online pharmacy app development solutions as per different business requirements. Alteza, a leading company that provides readymade and custom solutions for different pharmacy business ideas
ReplyDelete