title: "java.lang.OutOfMemoryError: Java heap space" while reading huge zip files
body: <p>I have a program that load data into a DB thru namedpipes, very cool.
  This program was running for about 2 years and accept text files or gzip.</p>
  
  <p>But now appeared some zip to load and I want to improve it. But I can't put this to work, I'm getting an OutOfMemoryError.</p>
  
  <p><strong><em>(Of course, I'm calling this using -Xms512M -Xmx2048M)</em></strong></p>
  
  <p>Below is how I get the InputStream:</p>
  
  <p><strong>PipeLoader.java</strong></p>
  
  <pre><code>protected BufferedReader getBufferedReader(File file, String compression) throws Exception {
      BufferedReader bufferedReader = null;
  
      if(compression.isEmpty())   {
          bufferedReader = new BufferedReader(new FileReader(file), BUFFER);
      } else if(compression.equalsIgnoreCase("gzip")) {
          InputStream fileStream = new FileInputStream(file);
          InputStream gzipStream = new GZIPInputStream(fileStream);
  
          // Works fine
          Reader reader = new InputStreamReader(gzipStream);
          bufferedReader = new BufferedReader(reader, BUFFER);
      } else if(compression.equalsIgnoreCase("zip")){
          InputStream fileStream = new FileInputStream(file);
          ZipInputStream zipStream = new ZipInputStream(fileStream);
          zipStream.getNextEntry(); // For testing purposes I'm getting only the first entry
  
          Reader reader = new InputStreamReader(zipStream); // Works only with small zips
          bufferedReader = new BufferedReader(reader, BUFFER);
      }
  
      return bufferedReader;
  }
  </code></pre>
  
  <p>I'm also tried with <a href="https://truevfs.java.net/kick-start/tutorial.html" rel="nofollow noreferrer">TrueVFS</a> library:</p>
  
  <pre><code>// The same: works with small zip files, OutOfMemoryError with big zip files
  TFile tFile = new TFile(file);
  TFileInputStream tfis = new TFileInputStream(new TFile(tFile.getAbsolutePath(), tFile.list()[0]));
  
  Reader reader = new InputStreamReader(tfis);
  bufferedReader = new BufferedReader(reader, BUFFER);
  </code></pre>
  
  <p>And yes, I'm closing everything properly (remember, works with gz!).</p>
  
  <p>In this case I need to load some zip file with only 1 plain textfile inside (~4GB zipped, ~35GB unzipped)</p>
  
  <p>I got an OutOfMemoryError in the first file, in less than 1min from the start.</p>
  
  <p><em>PS.: This is not a duplicate from <a href="https://stackoverflow.com/questions/8653283/reading-a-huge-zip-file-in-java-out-of-memory-error">Reading a huge Zip file in java - Out of Memory Error</a>, he had the option to read each one of the small files from inside the zip, but I have only 1 big file.</em></p>
  
  <p>I ran with -XX:+HeapDumpOnOutOfMemoryError and readed the .hprof file with Memory Analyser, but it doesn't help me much =/:</p>
  
  <p><img src="https://i.stack.imgur.com/4cufB.png" alt="MemoryAnalyser.png"></p>
  
  <p>Please, I need help.</p>