You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by sreekanth <sr...@egovernments.org> on 2016/07/07 05:57:06 UTC

Freemarker caching of BeanWrapper models causing Memory Leak

Hi,
     Freemarker caching of BeanWrapper model giving us a fantastic page load
performance (2X). This was enabled in strust2 via
/struts.freemarker.beanwrapperCache set to true/ in struts.xml, but now this
is causing our server going OOM due to no eviction policy applied to this
cache. When we profiled using jvisualvm its shows our Model objects
references are holds by this wrapper cache and its keep on growing. I hope
its a bug in cache implementation, or if there any workaround i can apply
without turning off the cache. 



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by sreekanth <sr...@egovernments.org>.
Any update please ? Forget to mention we are using Wildfly 10 Final / Ubuntu
OS. Please let me know if you required any additional details.



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719685.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by Lukasz Lenart <lu...@apache.org>.
2016-07-18 22:07 GMT+02:00 sreekanth <sr...@egovernments.org>:
> you mean to say i have to override equals n hashcode in our action classes?

Yes, I would try that as ModelCache is using ConcurrentHashMap behind
the scene which means it's need proper hashCode/equals to compare
objects (in other case each instance of an action will have a
different hashCode/equals)


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by sreekanth <sr...@egovernments.org>.
you mean to say i have to override equals n hashcode in our action classes?



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719806.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by Lukasz Lenart <lu...@apache.org>.
2016-07-18 15:51 GMT+02:00 sreekanth <sr...@egovernments.org>:
> No we are not overriding equals and hashcode in our action class, and from
> debugging the BeanWrapper Cache found its using System.identityHashCode to
> generate cache key

It's a default behaviour of hashCode from Object class (when not
overrode), which means it will return different hashCode even for
objects that has the same values.


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by sreekanth <sr...@egovernments.org>.
No we are not overriding equals and hashcode in our action class, and from
debugging the BeanWrapper Cache found its using System.identityHashCode to
generate cache key



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719799.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by Lukasz Lenart <lu...@apache.org>.
2016-07-14 8:09 GMT+02:00 sreekanth <sr...@egovernments.org>:
> Hi Lukazs,
>                Yesterday we have tried to check the source code and found
> its implementing SoftReference, its true that it will get garbage collected
> before OOM kicks in, right now we allocated Xmx 24 GB  so i doubt if we
> throw more memory will solve the issue, but my gut feeling is, from
> SoftReference javadoc, *As long as the referent of a soft reference is
> strongly reachable, that is, is actually in use, the soft reference will not
> be cleared*.
>
> We have no idea, why objects in cache grows even if we refresh the same
> page, on each refresh its creating new cache and adding similar set of
> object again, it seems like the cache holder itself is getting created
> multiple time even for the same page up on refresh (or reopen).

Just to clarify one thing, actions in Struts2 are created per each
request, so it means you must implement hashCode/equals for each
action to allow cache to properly distinguish new object from the same
object. Just to be sure your hashCode/equals implementations are using
all the fields that's gonna change during request.


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by sreekanth <sr...@egovernments.org>.
Hi Lukazs,
               Yesterday we have tried to check the source code and found
its implementing SoftReference, its true that it will get garbage collected
before OOM kicks in, right now we allocated Xmx 24 GB  so i doubt if we
throw more memory will solve the issue, but my gut feeling is, from
SoftReference javadoc, *As long as the referent of a soft reference is
strongly reachable, that is, is actually in use, the soft reference will not
be cleared*.

We have no idea, why objects in cache grows even if we refresh the same
page, on each refresh its creating new cache and adding similar set of
object again, it seems like the cache holder itself is getting created
multiple time even for the same page up on refresh (or reopen).



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719712.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by Greg Huber <gr...@gmail.com>.
If you are not doing anything fancy on your ui try the java templates
plugin as it is much faster!

On 15 July 2016 at 11:35, sreekanth <sr...@egovernments.org> wrote:

> Even tried upgrading to struts v 2.5.2 GA , but no luck . As you said this
> probably a freemarker issue with BeansModelCache. We have no choice now
> other than disabling this feature.
>
>
>
> --
> View this message in context:
> http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719772.html
> Sent from the Struts - Dev mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
>
>

Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by sreekanth <sr...@egovernments.org>.
Even tried upgrading to struts v 2.5.2 GA , but no luck . As you said this
probably a freemarker issue with BeansModelCache. We have no choice now
other than disabling this feature.



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719772.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by Lukasz Lenart <lu...@apache.org>.
Hi,

FreeMarker's ModelCache is using SoftReferences to handle entries. The
only option is to add more RAM as "Virtual machine implementations
are, however, encouraged to bias against clearing recently-created or
recently-used soft references." or disable the cache.

https://docs.oracle.com/javase/7/docs/api/java/lang/ref/SoftReference.html
http://stackoverflow.com/questions/299659/what-is-the-difference-between-a-soft-reference-and-a-weak-reference-in-java


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

2016-07-12 12:21 GMT+02:00 sreekanth <sr...@egovernments.org>:
> I have tried with latest freemarker as well but the result is same.
>
>
>
> --
> View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719701.html
> Sent from the Struts - Dev mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by sreekanth <sr...@egovernments.org>.
I have tried with latest freemarker as well but the result is same.



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719701.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by Lukasz Lenart <lu...@apache.org>.
2016-07-11 10:23 GMT+02:00 sreekanth <sr...@egovernments.org>:
> No I didn't try latest struts, If latest freemarker is supported by the
> struts version i'm using then i can try that. Since its in production i have
> no choice to upgrade struts2 from the one i am using (seems there is some
> breaking changes in the recent version ?).

Yes, latest FreeMarker should work without problems,, but I think the
same should be true for Struts


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by sreekanth <sr...@egovernments.org>.
No I didn't try latest struts, If latest freemarker is supported by the
struts version i'm using then i can try that. Since its in production i have
no choice to upgrade struts2 from the one i am using (seems there is some
breaking changes in the recent version ?).



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719686.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by Lukasz Lenart <lu...@apache.org>.
2016-07-08 9:05 GMT+02:00 sreekanth <sr...@egovernments.org>:
> Struts 2 Version - 2.3.24.1
> FreeMarker Version - 2.3.22

Have you tried to use the latest version of Struts or at least
FreeMarker? It looks like pure FreeMarker issue


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by sreekanth <sr...@egovernments.org>.
Struts 2 Version - 2.3.24.1
FreeMarker Version - 2.3.22

Config as follows

/<struts>
	
	<constant name="struts.devMode" value="false" />
	<constant name="struts.freemarker.templatesCache" value="true" />
	<constant name="struts.xslt.nocache" value="false" />
	<constant name="struts.ognl.logMissingProperties" value="false" />
	<constant name="struts.el.throwExceptionOnFailure" value="false" />
	<constant name="struts.ognl.enableExpressionCache" value="true" />
	<constant name="struts.serve.static" value="true" />
	<constant name="struts.i18n.reload" value="false" />
	<constant name="struts.configuration.xml.reload" value="false" />
	*<constant name="struts.freemarker.beanwrapperCache" value="true" />*
	
	<constant name="struts.convention.exclude.parentClassLoader" value="true"
/>
	<constant name="struts.convention.action.fileProtocols"
value="jar,vfsfile,vfszip,vfs,zip" />
	<constant name="struts.convention.result.path" value="/WEB-INF/jsp/" />
	<constant name="struts.custom.i18n.resources"
value="InfraApplicationResources,custom" />
	<constant name="struts.locale" value="en_IN" />
	<constant name="struts.multipart.maxSize" value="10485760" />
	<constant name="struts.multipart.parser" value="jakarta" />
	<constant name="struts.convention.action.mapAllMatches" value="true" />
	<constant name="struts.convention.default.parent.package" value="app" />
	
	<constant name="struts.convention.action.includeJars" value=".*app-.*" />
	
	<constant name="struts.action.extension" value="action"/>
	<constant name="struts.mapper.class"
value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
	<constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts"/>
	
	<constant name="struts.convention.package.locators" value="actions" />
	
	<bean type="org.apache.struts2.convention.ActionNameBuilder"
name="defaultActionNameBuilder"
class="org.apache.struts2.convention.DefaultActionNameBuilder" />
	<constant name="struts.convention.actionNameBuilder"
value="defaultActionNameBuilder" />
	<constant name="struts.convention.action.name.lowercase" value="true"/>
	
	<constant name="struts.action.excludePattern" value="/rest/.*" />
	<package name="app" extends="struts-default">
		<interceptors>
			<interceptor name="app-validator"
class="org.app.web.struts.interceptors.ValidationInterceptor" />
			<interceptor name="app-trim"
class="org.app.web.struts.interceptors.TrimInterceptor" />
			<interceptor name="app-exception"
class="org.app.web.struts.interceptors.ExceptionInterceptor" />
			<interceptor name="app-cahecontrol"
class="org.app.web.struts.interceptors.CacheControlInterceptor"/>
			<interceptor-stack name="app-interceptors">
				
				<interceptor-ref name="app-exception">
					true
					ERROR
				</interceptor-ref>
				<interceptor-ref name="alias" />
				<interceptor-ref name="params">
				
(^|\\%\\{)((#?)(top(\\.|\\['|\\[\")|\\[\\d\\]\\.)?)(dojo|struts|session|request|response|application|servlet(Request|Response|Context)|parameters|context|_memberAccess)(\\.|\\[).*","^(action|method):.*
				</interceptor-ref>
				<interceptor-ref name="servletConfig" />
				<interceptor-ref name="prepare" />
				<interceptor-ref name="i18n" />
				<interceptor-ref name="chain" />
				<interceptor-ref name="scopedModelDriven" />
				<interceptor-ref name="modelDriven" />
				<interceptor-ref name="checkbox" />
				<interceptor-ref name="staticParams" />
				<interceptor-ref name="actionMappingParams" />
				<interceptor-ref name="app-trim" />
				<interceptor-ref name="fileUpload" />
				<interceptor-ref name="params">
				
(^|\\%\\{)((#?)(top(\\.|\\['|\\[\")|\\[\\d\\]\\.)?)(dojo|struts|session|request|response|application|servlet(Request|Response|Context)|parameters|context|_memberAccess)(\\.|\\[).*","^(action|method):.*
				</interceptor-ref>
				<interceptor-ref name="conversionError" />
				<interceptor-ref name="validation">
					input,back,cancel,browse
				</interceptor-ref>
				<interceptor-ref name="app-validator" />
				<interceptor-ref name="app-cahecontrol"/>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="app-interceptors" />
		<global-results>
			<result name="genericError">/error/genericError.jsp</result>
			<result name="accessDeniedError">/error/accessDenied.jsp</result>
			<result name="invalid.token">/error/invalidRequest.jsp</result>
		</global-results>
		<global-exception-mappings>
			<exception-mapping exception="java.lang.Exception" result="genericError"
/>
			<exception-mapping exception="org.hibernate.HibernateException"
result="genericError" />
			<exception-mapping
exception="org.springframework.security.access.AccessDeniedException"
result="accessDeniedError" />
		</global-exception-mappings>
		<action name="CatchAll">
			<result>/error/genericError.jsp</result>
		</action>
	</package>
</struts>
/



--
View this message in context: http://struts.1045723.n5.nabble.com/Freemarker-caching-of-BeanWrapper-models-causing-Memory-Leak-tp5719662p5719682.html
Sent from the Struts - Dev mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: Freemarker caching of BeanWrapper models causing Memory Leak

Posted by Lukasz Lenart <lu...@apache.org>.
2016-07-07 7:57 GMT+02:00 sreekanth <sr...@egovernments.org>:
> Hi,
>      Freemarker caching of BeanWrapper model giving us a fantastic page load
> performance (2X). This was enabled in strust2 via
> /struts.freemarker.beanwrapperCache set to true/ in struts.xml, but now this
> is causing our server going OOM due to no eviction policy applied to this
> cache. When we profiled using jvisualvm its shows our Model objects
> references are holds by this wrapper cache and its keep on growing. I hope
> its a bug in cache implementation, or if there any workaround i can apply
> without turning off the cache.

It would be good if could provide more details, exact Apache Struts
version, FreeMarker version, configuration, etc.


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org