title: excessive memory use by java
body: <p>In a project of mine I constantly compress little blocks of data.
  Now I find out that the jvm then grows to 6GB of ram (resident (RES) RAM, not shared or virtual or so) and then die because of out of memory.
  It is as if the garbage collector never runs or so.
  I've pulled out the relevant code and pasted it below. When I run it (java6, 32 bit linux) it grows to 1GB of ram.
  Anyone got an idea how to reduce the memory usage?</p>
  
  <pre><code>import java.util.Random;
  import java.util.zip.Deflater;
  import java.util.zip.Inflater;
  
  class test  {
      int blockSize = 4096;
      Random r = new Random();
  
      public test() throws Exception {
          blockSize = 4096;
          byte [] data = new byte[blockSize];
          for(int index=0; index&lt;blockSize; index++)
              data[index] = (byte)r.nextInt();
  
          for(long cnt=0; cnt&lt;1000000; cnt++) {
              byte [] result = compress(data);
              if (result != null)
                  data[0] = result[0];
          }
      }
  
      byte [] compress(byte [] in) {
          assert in.length == blockSize;
  
          Deflater compresser = new Deflater();
          compresser.setInput(in);
          compresser.finish();
          byte [] out = new byte[in.length];
          int outLen = compresser.deflate(out);
  
          if (outLen &lt; blockSize) {
              byte [] finalOut = new byte[outLen];
              System.arraycopy(out, 0, finalOut, 0, outLen);
              return finalOut;
          }
  
          return null;
      }
  
      public static void main(String [] args) throws Exception {
          new test();
      }
  }
  </code></pre>