title: Error Memory VM java, java.lang.OutOfMemoryError: Java heap space
body: <p>Friends I need your help, I have to handle a very large file with information (about the file weighs 30 Mb). When processing this information file, java gives me this error:</p>
  
  <pre><code>
  Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
      at java.util.Arrays.copyOf(Arrays.java:2367)
      at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130)
      at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:114)
      at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:535)
      at java.lang.StringBuffer.append(StringBuffer.java:322)
      at java.io.StringWriter.write(StringWriter.java:94)
      at java.io.Writer.write(Writer.java:127)
      at com.sun.org.apache.xml.internal.serialize.Printer.printText(Printer.java:170)
      .....
  </code></pre>
  
  <p>My code is:</p>
  
  <pre><code>
  ...
  
      public Document generateXML(File file, String type) {
          try {
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder docBuilder = dbf.newDocumentBuilder();
  
              Document document = docBuilder.newDocument();
              Element rootElement = document.createElement("data");
              document.appendChild(rootElement);
  
              Element informacionElement = document.createElement("information");
              rootElement.appendChild(informacionElement);
  
              Element entityElement = document.createElement("entity");
              entityElement.appendChild(document.createTextNode("ANYTHING"));
              informacionElement.appendChild(entityElement);
  
              Element entityCode = document.createElement("code");
              entityCode.appendChild(document.createTextNode(0000));
              informacionElement.appendChild(entityCode);
  
              Element fileElement = document.createElement("FILENAME");
              fileElement.appendChild(document.createTextNode(file.getName()));
              informacionElement.appendChild(fileElement);
  
              Element detailElement = document.createElement("detail");
              rootElement.appendChild(detailElement);
  
              Path path = Paths.get(file.getAbsolutePath());
              Scanner tmpFile = new Scanner(path, StandardCharsets.UTF_8.name());
  
              String[] tmpTags = keyXml.get(type);
              int row = 1;
  
              while (tmpFile.hasNextLine()) {
  
                  Element rowElement = document.createElement("row");
                  filaElement.setAttribute("id", Integer.toString(row));
  
                  Scanner tmpLine = new Scanner(tmpFile.nextLine());
                  tmpLine.useDelimiter("\\|");
                  int index = 0;
                  while (tmpLine.hasNext()) {
                      Element element = document.createElement(tmpTags[index]);
                      String text = tmpLine.next().trim();
                      element.appendChild(document.createTextNode(text));
                      filaElement.appendChild(element);
                      ++index;
                  }
                  detailElement.appendChild(filaElement);
                  ++fila;
              }
              return document;
          } catch (ParserConfigurationException | IOException ex) {
              Logger.getLogger(HandlerXML.class.getName()).log(Level.SEVERE, null, ex);
          }
          return null;
      }
  ...
  </code></pre>
  
  <p>This code generates the following XML document:</p>
  
  <pre><code>    &lt;data&gt;
          &lt;information&gt;
              &lt;entity&gt;ANYTHING&lt;/entity&gt;
              &lt;code&gt;0000&lt;/code&gt;
              &lt;filename&gt;filename_XXX&lt;/filename&gt;
          &lt;/information&gt;
          &lt;detail&gt;
              &lt;row id="1"&gt;
                  &lt;tag1&gt;0099&lt;/tag1&gt;
                  &lt;tag2&gt;2014&lt;/tag2&gt;
                  &lt;tag3&gt;05&lt;/tag3&gt;
                  &lt;tag4&gt;0015&lt;/tag4&gt;
                  &lt;tag5&gt;06&lt;/tag5&gt;
                  &lt;tag6&gt;06&lt;/tag6&gt;
                  &lt;tag7&gt;000000000099907&lt;/tag7&gt;
                  &lt;tag8&gt;098026&lt;/tag8&gt;
                  &lt;tag9&gt;00000781.40&lt;/tag9&gt;
                  &lt;tag10&gt;E&lt;/tag10&gt;
                  &lt;tag11&gt;000000006804750&lt;/tag11&gt;
                  &lt;tag12&gt;xxx&lt;/tag12&gt;
                  &lt;tag13&gt;xxxx&lt;/tag13&gt;
                  &lt;tag14&gt;xxx&lt;/tag14&gt;
                  &lt;tag15&gt;&lt;/tag15&gt;
                  &lt;tag16&gt;&lt;/tag16&gt;
                  &lt;tag17&gt;xx&lt;/tag17&gt;
                  &lt;tag18&gt;xxx&lt;/tag18&gt;
                  &lt;tag19&gt;1014&lt;/tag19&gt;
                  &lt;tag20&gt;05&lt;/tag20&gt;
                  &lt;tag21&gt;11111&lt;/tag21&gt;
                  &lt;tag22&gt;1014&lt;/tag22&gt;
                  &lt;tag23&gt;00000000000000000000&lt;/tag23&gt;
                  &lt;tag24&gt;3&lt;/tag24&gt;
                  &lt;tag25&gt;20101&lt;/tag25&gt;
                  &lt;tag26&gt;00000000&lt;/tag26&gt;
                  &lt;tag27&gt;46671&lt;/tag27&gt;
                  &lt;tag28&gt;1&lt;/tag28&gt;
                  &lt;tag29&gt;N&lt;/tag29&gt;
                  &lt;tag30&gt;&lt;/tag30&gt;
                  &lt;tag31&gt;&lt;/tag31&gt;
                  &lt;tag32&gt;A&lt;/tag32&gt;
                  &lt;tag33&gt;&lt;/tag33&gt;
                  &lt;tag34&gt;&lt;/tag34&gt;
                  &lt;tag35&gt;&lt;/tag35&gt;
                  &lt;tag36&gt;&lt;/tag36&gt;
                  &lt;tag37&gt;&lt;/tag37&gt;
                  &lt;tag38&gt;&lt;/tag38&gt;
              &lt;/row&gt;
              ............   
          &lt;/detail&gt;
      &lt;/data&gt;
  </code></pre>
  
  <p>word where xml data obtained is:</p>
  
  <p>**0099|2014|05|0015|06|06|00000000003101|066671|000077777.00|E|000000002719415|xxx|xxxxxx                   |xxxxxxxx                |      |                         |xxxxxx                   |                         |1014|05|12254758|1014|00000000000000000000|3|20101|00000000|748596|1|X|               |   |Q|  | |         |         |   **</p>
  
  <p>Now imagine that the xml must be generated for 100000 lines of information, and if you are wondering, all the lines must be converted to XML format.</p>
  
  <p>The problem is, when you convert the object into a <strong>STRING</strong> to <strong>DOCUMENT</strong> I get the error that I showed earlier, the function I use to transform <strong>DOCUMENT</strong> to <strong>STRING</strong> is:</p>
  
  <pre><code>    private static String convertDocumentToString(Document doc) {
          TransformerFactory tf = TransformerFactory.newInstance();
          Transformer transformer;
          try {
              transformer = tf.newTransformer();
              StringWriter writer = new StringWriter();
              transformer.transform(new DOMSource(doc), new StreamResult(writer));
              return writer.getBuffer().toString();
          } 
          catch (TransformerException e) {}
          return "";
      }
  </code></pre>
  
  <p>Now this error is virtual memory and java VM workaround is to increase the memory with <strong>-Xmx####m</strong>, but I wonder if this is solved by another way, try to handle the data type with: String, StringBuilder and byte. Could anyone solve this question? </p>
  
  <p>Thanks in advance.</p>