title: How to improve xerces parser performance when large comments are present in xml file?
body: <p>I'm using the following code to parse an xml file using xerces 2.11:</p>
  
  <pre><code>@Test
  public void testXercesPerformance() throws IOException, SAXException, ParserConfigurationException
  {
      final SAXParserFactory spf = SAXParserFactory.newInstance();
      final SAXParser parser = spf.newSAXParser();
      final XMLReader xmlReader = parser.getXMLReader();
      final InputSource inputSource = new InputSource(new BufferedInputStream(new FileInputStream(new File("./some.xml")), 8192));
      xmlReader.parse(inputSource);
  }
  </code></pre>
  
  <p>However the performance is very poor when the xml file just contains a few xml elements at the beginning and a large comment at the end (total file size about 10MB). In the course of parsing the parser successively allocates new Strings ending up at a total of 1.3TB of allocated strings (not all allocated at the same time). The parsing itself took 4 minutes to complete.</p>
  
  <p>The file I used for testing started with:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;artifactId&gt;helloworld-secure&lt;/artifactId&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-servlet&lt;/artifactId&gt;
              &lt;version&gt;7.4.5.v20110725&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-security&lt;/artifactId&gt;
              &lt;version&gt;7.4.5.v20110725&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;appassembler-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.1.1&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                          &lt;goals&gt;&lt;goal&gt;assemble&lt;/goal&gt;&lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;assembleDirectory&gt;target&lt;/assembleDirectory&gt;
                              &lt;programs&gt;
                                  &lt;program&gt;
                                      &lt;mainClass&gt;HelloWorld&lt;/mainClass&gt;
                                      &lt;name&gt;webapp&lt;/name&gt;
                                  &lt;/program&gt;
                              &lt;/programs&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3.2&lt;/version&gt;
                  &lt;configuration&gt;
              &lt;source&gt;1.6&lt;/source&gt;
                      &lt;target&gt;1.6&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  &lt;!-- 
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;artifactId&gt;helloworld-secure&lt;/artifactId&gt;
      &lt;dependencies&gt;
  </code></pre>
  
  <p>It then repeats the dependencies from the uncommented part hundreds of times until it reaches a size of nearly 10MB and ends with:</p>
  
  <pre><code>    &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;appassembler-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.1.1&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;package&lt;/phase&gt;
                          &lt;goals&gt;&lt;goal&gt;assemble&lt;/goal&gt;&lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;assembleDirectory&gt;target&lt;/assembleDirectory&gt;
                              &lt;programs&gt;
                                  &lt;program&gt;
                                      &lt;mainClass&gt;HelloWorld&lt;/mainClass&gt;
                                      &lt;name&gt;webapp&lt;/name&gt;
                                  &lt;/program&gt;
                              &lt;/programs&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3.2&lt;/version&gt;
                  &lt;configuration&gt;
              &lt;source&gt;1.6&lt;/source&gt;
                      &lt;target&gt;1.6&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  --&gt;
  </code></pre>
  
  <p>What's the cause of this poor performance and how should I configure the parser to improve performance?</p>