[ 
https://issues.apache.org/jira/browse/SHINDIG-1132?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12736934#action_12736934
 ] 

Jed Wesley-Smith commented on SHINDIG-1132:
-------------------------------------------

Unfortunately, the problem is that the instance inside the threadlocal contains 
 a strong reference to the XmlUtil class. Making it a new instance each time or 
using the Guide threadlocal do not change this at all and will not fix the 
problem.

The simplest fix would be to set the error handler to null after using it 
(example doing this in the parse method):

Index: XmlUtil.java
===================================================================
--- XmlUtil.java        (revision 33230)
+++ XmlUtil.java        (working copy)
@@ -282,23 +282,29 @@
    * Attempts to parse the input xml into a single element.
    * @param xml
    * @return The document object
-   * @throws XmlException if a parse error occured.
+   * @throws XmlException if a parse error occurred.
    */
   public static Element parse(String xml) throws XmlException {
     try {
-      DocumentBuilder builder = getBuilder();
-      InputSource is = new InputSource(new StringReader(xml.trim()));
-      Element element = builder.parse(is).getDocumentElement();
-      return element;
-    } catch (SAXParseException e) {
-      throw new XmlException(
-          e.getMessage() + " At: (" + e.getLineNumber() + ',' + 
e.getColumnNumber() + ')', e);
-    } catch (SAXException e) {
-      throw new XmlException(e);
+      final DocumentBuilder builder = getBuilder();
+      try {
+        InputSource is = new InputSource(new StringReader(xml.trim()));
+        Element element = builder.parse(is).getDocumentElement();
+        return element;
+      } catch (SAXParseException e) {
+        throw new XmlException(
+            e.getMessage() + " At: (" + e.getLineNumber() + ',' + 
e.getColumnNumber() + ')', e);
+      } catch (SAXException e) {
+        throw new XmlException(e);
+      } catch (IOException e) {
+        throw new XmlException(e);
+      } finally {
+        if (builder != null) {
+          builder.setErrorHandler(null);
+        }
+      }
     } catch (ParserConfigurationException e) {
       throw new XmlException(e);
-    } catch (IOException e) {
-      throw new XmlException(e);
     }
   }
 }

> ClassLoader memory leak caused by XmlUtil ThreadLocal
> -----------------------------------------------------
>
>                 Key: SHINDIG-1132
>                 URL: https://issues.apache.org/jira/browse/SHINDIG-1132
>             Project: Shindig
>          Issue Type: Bug
>          Components: Java
>    Affects Versions: 1.0
>         Environment: When trying to unload the ClassLoader that loaded 
> Shindig, for instance in an OSGi environment
>            Reporter: Jed Wesley-Smith
>
> The class org.apache.shindig.common.xml.XmlUtil caches a 
> javax.xml.parsers.DocumentBuilder in the ThreadLocal reusableBuilder 
> variable. These instances are created with the static ErrorHandler instance 
> which creates the strong reference to the XmlUtil class that prevents the 
> ClassLoader from being reclaimed.
> Currently the only way to turn off this behaviour is for 
> DocumentBuilder.reset() to throw an exception.
> We need a way to turn off this caching. Maybe the caching aspect could be 
> injected via Guice?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to