title: OutOfMemoryError: Java heap space when trying to create ArrayList
body: <p>I am trying to write a program which converts all G1 lines of a G code to lines which say <code>MOVX (x-coordinate of G1 command)</code></p>
  
  <p>Eg. <code>G1 X0.1851</code> should become <code>MOVX(0.1851)</code></p>
  
  <p>At the moment the program is just appending the text file that has been read and printing the new code below the old one in the same text file.</p>
  
  <p>The problem is that when I try to create an array list of the number after the <code>X</code> in the <code>G-Code</code>, I get a problem with the memory in the heap space overflowing.</p>
  
  <p>I have added a <code>clear()</code> statement after each iteration of a line of the <code>G-Code</code> to try to prevent the array list becoming larger and larger but it keeps overflowing.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>package textfiles;
  import java.io.IOException;
  import java.util.ArrayList;
  
  
  public class FileData {
  
      public static void main(String[] args) throws IOException {
  
          String file_name = "C:/blabla";
  
          try {
              ReadFile file = new ReadFile(file_name);
              WriteFile data = new WriteFile(file_name, true);
              String[] aryLines = file.OpenFile();
  
              int i;
              int j;
              int y;
  
              for (i=0; i &lt; aryLines.length; i++ ) { //goes through whole text file
                  System.out.println( aryLines[ i ]);
  
                  if (i == 0) {
                      data.writeToFile("");
                      System.lineSeparator();
                  }
  
                  char[] ch = aryLines[ i ].toCharArray();
                  ArrayList&lt;Character&gt; num = new ArrayList&lt;Character&gt;();
                  String xCo = null;
  
  
                  boolean counterX = false;
  
                  if ((ch[0]) == 'G' &amp;&amp; ch[1] == '1') {
  
                      for (j = 0; j &lt; ch.length; j++) { //goes through each line of text file
  
                          for (y = 0; counterX == true; y++) {
                              num.add(ch[j]);
                          }
  
                          if (ch[j] == 'X') {
                              counterX = true;
                          }
  
                          else if (ch[j] == ' ') {
                              counterX = false;
                          }
                      }
                      xCo = num.toString();
                      data.writeToFile("MOVX (" + xCo + ")");
                  }
                  num.clear();
              }
          }
          catch (IOException e) {
              System.out.println( e.getMessage() );
          }
  
          System.out.println("Text File Written To");
  
      }
  }
  
  </code></pre>