Uploaded image for project: 'OFBiz'
  1. OFBiz
  2. OFBIZ-5395

Introduce Tomcat's JreMemoryLeakPreventionListener and why




      After reading few articles on possible memory leaks when using ThreadLocal variable with custom classes in application server context where a thread pool is used, I checked OFBiz code. There is only 2 custom classes concerned: CompilerMatcher and RollbackOnlyCause (JDK classes are not concerned by ThreadLocal memory leaks).

      First I must tell, that the memory leak problem is more clearly described in those articles when you use an external Application Server (like Tomcat) and you deploy/undeploy applications. It seems there are no major issues when you use OFBiz OOTB (with Tomcat embedded). Nevertheless, it's a concern by and large.

      I have not investigated RollbackOnlyCause, only CompilerMatcher. But, after some profiling, I believe both should only generate small amouts of memory leaks, almost not noticeable even after several deploy/undeploy cycles.

      Nevertheless I tried to find a good way to get rid of CompilerMatcher possible leaks. I thought about 3 ways:

      1. Reverts CompilerMatcher related changes done for OFBIZ-4107 (introduction of CompilerMatcher) by using Perl5Compiler.READ_ONLY_MASK which guarantees thread safety
      2. Uses ThreadLocal<CompilerMatcher> local variables instead of private static members
        • Pros: no need to worry about thread safety
        • Cons: performance, local ThreadLocal local variables creation removes the patterns-compiled-cache ThreadLocal<CompilerMatcher> offers when used as a private static member
      3. Introduces Tomcat's JreMemoryLeakPreventionListener. What it does (in less than a minute)? Why JreMemoryLeakPreventionListener was created? History (29 pages presentation). How it does it? Read code!
        • Pros:
          • no changes related to CompilerMatcher, performance enhancement the cache introduces kept
          • prevents memory leaks when an external Application Server is used or at least warn about them (Note: this can only be done in OFBiz internally, for app server the best is to document with a section in the related wiki page.
        • Cons: none, this should had any noticeable effects when OFBiz is used OOTB (Tomcat embedded)

      I decided to go with the JreMemoryLeakPreventionListener solution. Another reason for that is that when I profiled OFBiz trunk using the demo site I found that we were having a large bloc of memory retained by sun.awt.AppContext. I think we should not have such a thing, the web truk demo does not use AWT at all! Fortunately jreMemoryLeakPreventionListener.setAppContextProtection prevents this, even if I have still no ideas from where this comes (certainly something like explainded in page 14 of the link from "History (29 pages presentation)" above)

      I'm also considering to replace the current uses of java.util.regex.Pattern by CompilerMatcher in cases of a static pattern is used. When the CompilerMatcher cache makes sense (patterns do not change). The interest is the better performance of the oro framework, at least so far (Java 1.6, see below).

      Some interesting references I noted while analysing this issue:


        1. OFBIZ-5395-CompilerMatcher.patch
          7 kB
          Jacques Le Roux



            jleroux Jacques Le Roux
            jleroux Jacques Le Roux
            0 Vote for this issue
            2 Start watching this issue