ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

Redeploy memory leaks

xmedekoTop Contributor
16 Nov 2009 07:01:37 GMT
16 Nov 2009 07:01:37 GMT

Hi,

I am observing memory leaks when redeploying my ZK web app in JBoss 5.1.0GA (no Spring). The ZK uses ThreadLocal data extensively, but do not clean them in ServletContextListener.contextDestroyed() and I guess it might be the main source of the problem.

Check similar Spring issue:
http://opensource.atlassian.com/confluence/spring/display/DISC/Memory+leak+-+classloader+won%27t+let+go

Also, the other source of the problem may be commons-logging (maybe SLF4J fixed this problem).

Did somebody else experience memory leaks after redeploy? Is this ZK bug or the problem is somewhere else?

tomyeh
16 Nov 2009 19:55:34 GMT
16 Nov 2009 19:55:34 GMT

Technically, ZK cleans up each ThreadLocal variable after serving a request. To really know the real cause, we have to use jprofiler or other tools to verify.

Regarding ServletContextListener, we clean up the application when DHtmlLayoutServlet's destroy method is called. It shall have the same effect.

xmedekoTop Contributor
17 Nov 2009 11:11:07 GMT
17 Nov 2009 11:11:07 GMT

And do you observe memory leaks after redeploy, too?

xmedekoTop Contributor
21 Jan 2010 08:35:51 GMT
21 Jan 2010 08:35:51 GMT

Hi,

I have used Eclipse MAT and found, that there are some CacheMap objects in the ThreadLocal variables. I suspect the class ThreadLocalCache - it never calls ThreadLocal.remove() method.

Again, I do not see ZK using ThreadLocal.remove() in many other cases, too. So, how and where does ZK clean ThreadLocal variables?

tomyeh
21 Jan 2010 18:56:55 GMT
21 Jan 2010 18:56:55 GMT

Hi Ondrej,

Could you provide the log of Eclipse MAT? As far as I know we don't have memory leak issue when redeploying on Tomcat. However, I am not sure if the redeployment is going thru Eclipse -- I don't trust Eclipse's memory use. I don't have Eclipse but many of others have to restart Eclipse once a while to clean up.

ZK doesn't call ThreadLocal.remove(). It is supposed to be garbage collected if all classes are unloaded completely (so all references to it are gone)

xmedekoTop Contributor
22 Jan 2010 02:30:44 GMT
22 Jan 2010 02:30:44 GMT

Hi Tom,

I use JBoss tool in Eclipse to redeploy my web app. This tool just makes a simple file copy. The problem remains also when I redeploy my web app manually. I use exploded deployment. to reproduce the bug, try:
1. copy exploded deployment,
2. run the app (point browses to the proper url)
3. delete the deployment
4. repeat 1. - 3. few times


Maybe the problem is caused by this?

Probem with static ThreadLocal (ZK uses that):
- http://blog.arendsen.net/index.php/2005/02/22/threadlocals-and-memory-leaks-revisited/
- http://www.jroller.com/tackline/entry/fixing_threadlocal
Problem with inner classes and ThreadLocal: http://crazybob.org/2006/02/threadlocal-memory-leak.html

How to clean ThreadLocals in a servlet: http://www.dewavrin.info/?p=196

My heap dump is at:
http://dl.dropbox.com/u/1372480/dump_simple.hprof (206MB, will be deleted later).

I just have started Eclipse MAT and used the function: "Duplicate Classes: Detect classes loaded by multiple class loaders."

tomyeh
22 Jan 2010 05:29:28 GMT
22 Jan 2010 05:29:28 GMT

Interesting! You're probably right. I'll investigate it a bit more later. Thanks.

xmedekoTop Contributor
2 Mar 2010 02:43:52 GMT
2 Mar 2010 02:43:52 GMT

I have made a bug for this issue Bug ID 2961772

tomyeh
2 Mar 2010 04:38:51 GMT
2 Mar 2010 04:38:51 GMT

Thanks. We will follow it there.

PeterKuo
15 Mar 2010 01:27:42 GMT
15 Mar 2010 01:27:42 GMT

@xmedeko
Tom has modified the code lately.
And the issue should be resolved.

Due to some soft reference may not be garbage collected while redeploy,
you may add following setup to jboss vm arguments.
XX:SoftRefLRUPolicyMSPerMB=1

xmedekoTop Contributor
16 Mar 2010 03:29:47 GMT
16 Mar 2010 03:29:47 GMT

@PeterKuo
I have downloaded the latest ZK 3.6.4 and do not see the Bug ID: 2961772 in the release-notes. Has it been fixed in 3.6.4, too, or just in the 5.0.1?
Thx

PeterKuo
16 Mar 2010 06:19:35 GMT
16 Mar 2010 06:19:35 GMT

@xmedeko
Thanks for your notification,
It has been fixed in both versions.

xmedekoTop Contributor
17 Mar 2010 07:49:57 GMT
17 Mar 2010 07:49:57 GMT

Hi Peter,
I still see memory leaks with ZK 3.6.4 after I redeploy an application (duplicate classes in Eclipse MAT). I suspect the ScalableTimer in the SmartAuWriter. Is it the possible cause of a memory leak or should I look somewhere else?

Thanks

PeterKuo
17 Mar 2010 19:43:10 GMT
17 Mar 2010 19:43:10 GMT

Though it's fixed, but may not be updated to release so fast?

BTW, have you gc soft reference before? As I mentioned
"Due to some soft reference may not be garbage collected while redeploy,
you may add following setup to jboss vm arguments.
XX:SoftRefLRUPolicyMSPerMB=1"

PeterKuo
17 Mar 2010 20:42:05 GMT
17 Mar 2010 20:42:05 GMT

@xmedeko
Would you please turn off ScalableTimer and try again?
Please refer to
http://docs.zkoss.org/wiki/Developer_reference_Appendix_B._WEB-INF/zk.xml_Overview_The_client-config_Element#The_resend-delay_Element
and set the value to -1

Also, can you provide a most easy sample to let us reproduce the problem?

xmedekoTop Contributor
18 Mar 2010 03:33:40 GMT
18 Mar 2010 03:33:40 GMT

@PeterKuo
Yes, with this parameter the redeploy seems has no memory leaks.

The sample is the same as before: deploy an ZK app with SmartAuWriter turned on, hit the app URL in the browser, undeploy the app. Repeat a few times. Start jconsole, click garbage collecting a few times (to be sure soft references are gone), dump live objects (jps;jmap -dump:live,file=dump.hprof PID), see the dump.hprof in Eclipse MAT.

Maybe the thread of the ScalableTimer has to be destroyed before undeploying the application? I think it has something to do with the Java classloader hell and static variables.

tomyeh
18 Mar 2010 06:38:38 GMT
18 Mar 2010 06:38:38 GMT

Ondrej, I think you're right. I'll fix it later. Meanwhile, if it is an issue to you, disable the timer as Peter suggested.

xmedekoTop Contributor
18 Mar 2010 08:30:59 GMT
18 Mar 2010 08:30:59 GMT

Thanks Tom, our project is still in developing phase, so I keep timer disabled, no problem.

tomyeh
19 Mar 2010 04:13:26 GMT
19 Mar 2010 04:13:26 GMT

Fixed but wait for our next freshly release: detail.

aktejo
23 Mar 2010 19:27:20 GMT
23 Mar 2010 19:27:20 GMT

Is this memory leak issue related to "premgen space" ?
afaik, this is in the jvm level memory management, people suggest to use jrockit form oracle, the said it has better memory mgt.
cmiiw

1 2