title: Zipping - java.lang.OutOfMemoryError: Java heap space
body: <p>I have this code to zip folders, but when I zip large folders (10GB), I got an error with memory. It works fine with folders around 1GB.</p>
  
  <p>Probably some memory leak, but where in my code is the leak? How can I fix this?</p>
  
  <p>Thanks.</p>
  
  <p>This is the error:</p>
  
  <pre><code>Exception in thread "main" java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
  Caused by: java.lang.OutOfMemoryError: Java heap space
  at java.util.Arrays.copyOf(Arrays.java:2882)
  at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
  at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:572)
  at java.lang.StringBuilder.append(StringBuilder.java:203)
  at java.io.UnixFileSystem.resolve(UnixFileSystem.java:93)
  at java.io.File.&lt;init&gt;(File.java:207)
  at java.io.File.listFiles(File.java:1056)
  at Zipper.addDir(Zipper.java:27)
  at Zipper.addDir(Zipper.java:32)
  at Zipper.addDir(Zipper.java:32)
  at Zipper.addDir(Zipper.java:32)
  at Zipper.addDir(Zipper.java:32)
  at Zipper.addDir(Zipper.java:32)
  at Zipper.addDir(Zipper.java:32)
  at Zipper.addDir(Zipper.java:32)
  at Zipper.addDir(Zipper.java:32)
  at Zipper.zipDir(Zipper.java:17)
  at Schedule.runBackup(Schedule.java:128)
  at Machine.runBackup(Machine.java:108)
  at Person.main(Person.java:51)
  </code></pre>
  
  <p>This is the code:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipOutputStream;
  
  import javax.swing.JFrame;
  import javax.swing.JOptionPane;
  
  public class Zipper {
  
  static void zipDir(String zipFileName, String dir) throws Exception {
      File dirObj = new File(dir);
      try {
          ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
          addDir(dirObj, out);
          out.close();    
      } catch(Exception e) {
          JFrame frame = new JFrame();
          JOptionPane.showMessageDialog(frame, e.getMessage() + ". Is settings.xml correct?");
      }
  }
  
  static void addDir(File dirObj, ZipOutputStream out) throws IOException {
      try {
          File[] files = dirObj.listFiles();
          byte[] tmpBuf = new byte[1024];
          for (int i = 0; i &lt; files.length; i++) {
              try {
                  if (files[i].isDirectory()) {
                      addDir(files[i], out);
                      continue;
                  }
                  FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
                  out.putNextEntry(new ZipEntry(files[i].getAbsolutePath().replace(File.separatorChar,'/')));
                  int len;
                  while ((len = in.read(tmpBuf)) &gt; 0) {
                      try {
                          out.write(tmpBuf, 0, len);
                      }
                      catch(Exception e) {
                          System.out.println(e.getMessage());
                      }
                  }   
                  out.closeEntry();
                  in.close();
              }
              catch(Exception e) {
                  System.out.println(e.getMessage());
              }
          }
      }
      catch(Exception e) {
          System.out.println(e.getMessage());
      }
  }
  }
  </code></pre>