title: improve java code to avoid Heap out of memory exception
body: <p>I have the following piece of code where I am reading a large file using a stringBuffer object, performing some operations by creating temporary byte[] objects and because of this I get a out of memory exception when a have a large file size say 16 MB. </p>
  
  <pre><code>StringBuffer dataBuffer;
  ArrayList&lt;byte[]&gt; sourceFragments;
  ArrayList&lt;BitSet&gt; sourceBits = new ArrayList&lt;BitSet&gt;();
  dataBuffer = eHelper.readFile(encoder.getFileName());
  sourceFragments = eHelper.fragmentFile(dataBuffer.toString());
               /*
       * converting byte[] to BitSet
                the below loop is run 128 times
       */
      Iterator&lt;byte[]&gt; iter = sourceFragments.iterator();
      while (iter.hasNext()) {
          byte[] temp = iter.next();
                      // temp.length will return 128 KB
          sourceBits.add(eHelper.byteArrayToBitSet(temp));
      }
  </code></pre>
  
  <p>I am wondring if there is a way for me to prevent this out of memeory exception to occur. I am not considering the option of increasing heap space, I am using the default heap space on a 32 bit machine. Is there any way I can reduce the number of temp objects that are being created so that I can avoid the outOfMemory Exception</p>
  
  <p>edit1:</p>
  
  <p>I have made the following changes to the code where I do npt load the entire file into the memory as a String, I do not create byte[] array, but read directly from the file and convert it directly to an arrayList of bitSet. This has helped a bit where I am able to work with 20 MB files, I am wondering if can push this further some more to work with a max of 30 MB files ?</p>
  
  <p>edit2: </p>
  
  <p>I have modified the source code in as follows, I have removed any redundant data types that I have created 
          public ArrayList fragmentSourceData(File filename) {
          RandomAccessFile r ;
          ArrayList sourceBits = new ArrayList();</p>
  
  <pre><code>    try {
          r= new RandomAccessFile(filename, "r");
          System.out.println(r.length());
          encoder.setSourceFileLength((int)r.length());
          int fragmentSize = encoder.calculateFragmentSize();
          System.out.println(fragmentSize);
          encoder.setFragmentSize(fragmentSize);
          encoder.setParameters();
  
          byte[] b = new byte[fragmentSize] ;
  
              long new_pos=0;
              int i=0;
              while(new_pos&lt;=encoder.getSourceFileLength()){
                  i++;
                  r.read(b ,0, fragmentSize );
                   new_pos=fragmentSize*i;
                   r.seek(new_pos);
                   sourceBits.add(BitSet.valueOf(b));
                    }
                 r.close();
                 b=null;
  
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  
      return sourceBits;
  }
  </code></pre>
  
  <p>}</p>