title: "java.lang.OutOfMemoryError: Java heap space" When calling FileUtils.readFileToString on xml files
body: <p>My program iterates over a directory and uses the following function, from my Recoder class, to read a file using <code>FileUtils.readFileToString</code> on a encoding and overwrites it using <code>FileUtils.write</code> on another encoding.</p>
  
  <p>However, when it tries to do this with an xml file, I get the exception at the title, otherwise, it works fine (i have tested on a dir with .java, .js, .css, .html, .jsp...).</p>
  
  <pre><code>public class Recoder {
      private static Charset fromCharset;
      private static Charset toCharset;
  
      public static void recodeToUTF(File f, boolean verbose){
          try{
              if(verbose){
                  System.out.println("Convertendo "+f.getAbsolutePath()+" para UTF-8");
              }
              toCharset = Charset.forName("UTF-8");
              String content = FileUtils.readFileToString(f);
              FileUtils.write(f,content, toCharset);
          }catch(Exception e){    
          }
      }
  
      public static void recodeFile(File f, String de, String para, boolean verbose){
          try{
              if(verbose){
                  System.out.println("Convertendo "+f.getAbsolutePath()+" de "+ de + " para "+ para);
              }
  //          CharsetDetector cd= new CharsetDetector();
  //          FileInputStream fis = new FileInputStream(f);
  //          cd.setText(fis);
  //          fis.close();
  //          CharsetMatch cm = cd.detect();
  
  //          if(cm!=null){
  //              fromCharset = Charset.forName(cm.getName());
  //          }else{ fromCharset = Charset.forName(de);}
              fromCharset = Charset.forName(de);
              toCharset = Charset.forName(para);
              String content = FileUtils.readFileToString(f,fromCharset);
              FileUtils.write(f,content,toCharset);
              content=null;
          }catch(Exception e){    
          }
      }
  
      public static String removeAcentos(String str) {
          str = Normalizer.normalize(str, Normalizer.Form.NFD);
          str = str.replaceAll("[^\\p{ASCII}]", "");
          return str;  
      }
  
  }
  </code></pre>
  
  <p>The CharsetDetector stuff is from ICU4J, and for some reason it hangs when calling cd.detect() so, for now, I just left it in comments.</p>
  
  <p>This is the main that calls it:</p>
  
  <pre><code>public static void main( String[] args ){
          DecoderArguments decArgs = new DecoderArguments();
          JCommander jc = new JCommander(decArgs, args);
          try {
              if(args.length&gt;0){
                  for(String s : decArgs.files){
                      File file;
                      if (decArgs.recursive){
                          System.out.println("Executando Recursivamente em: "+ s);
                          file = new File(s);
                          if(file.isDirectory()){
                              Collection&lt;File&gt; files = FileUtils.listFiles(file,FileFileFilter.FILE, DirectoryFileFilter.DIRECTORY);
                              for (File f : files){
                                  boolean exec=true;
                                  for(String excl : decArgs.excludes){
                                      if (f.getAbsolutePath().contains(excl)) exec=false;
                                  }
                                  if (file.exists() &amp;&amp; exec){
                                      if("".equals(decArgs.fromEncoding)){
                                          Recoder.recodeToUTF(f, decArgs.verbose);
                                      }else {
                                          Recoder.recodeFile(f, decArgs.fromEncoding, decArgs.toEncoding, decArgs.verbose);
                                      }
                                      System.gc();
                                  }                           
                              }
                          }else{
                              System.out.println("Por favor, informe um diretorio para ler recursivamente.\n"
                                      + "Uso: java -jar decoder.jar &lt;-r&gt; Caminho|Arquivo");
                          }       
                      }else{
                          System.out.println("Convertendo arquivo: "+ s);
                          file = new File(s);
                          boolean exec=true;
                          for(String excl : decArgs.excludes){
                              if (file.getAbsolutePath().contains(excl)) exec=false;
                          }
                          if (file.exists() &amp;&amp; exec){
                              if("".equals(decArgs.fromEncoding)){
                                  Recoder.recodeToUTF(file, decArgs.verbose);
                              }else {
                                  Recoder.recodeFile(file, decArgs.fromEncoding, decArgs.toEncoding, decArgs.verbose);
                              }
                          }
                      }
                  }
              }else if (args.length==0){
                  System.out.println("Sintaxe incorreta.\n");
                  jc.usage();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>Things to note:</p>
  
  <ul>
  <li>I'm using the following VM arguments on Eclipse -Xms1024m -Xmx2048m</li>
  <li>JDK version 1.7.0_75</li>
  <li>I've experimented with some System.gc() but no avail</li>
  <li>It only happens when the main is running recursively(<code>decArgs.recursive=true</code>), the same file that throws the exception is recoded smoothly when</li>
  <li>Already checked the file encoding on bash and hardcoded it for a test, no luck there also.</li>
  </ul>
  
  <p>Any ideas on why this is happening?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong>
  Using <code>Recoder.recodeToUTF</code> method instead of <code>Recoder.recodeFile</code> causes no OutOfMemory to be thrown. Maybe trying to open with an wrong encoding causes the memory leak.</p>
  
  <p>Analysing the .hprof generated, the (messed up) 300Mb xml file was using around 500Mb of the heap. However, the heap is set to an maximum size of 2Gb</p>