title: ByteArrayOutputStream Out of memory error while recording in java sound
body: <p>I encountered a very strange problem. I am writing portion of the code below.</p>
  
  <pre><code>try {
      while (!stopCapture) {
          // Read data from the internal buffer of the data line.
          int cnt = this.recLine.read(tempBuffer, 0, tempBuffer.length);
          if (cnt &gt; 0) {
              // Save data in output stream object.
              byteArrayOutputStream.write(tempBuffer, 0, cnt);
              // System.out.println(" bytes " + tempBuffer[0]);
          }// end if
      }// ends while
  
      // AudioSystem.write(myAIS, targetType, outputFile);
      byteToWave(byteArrayOutputStream);
      byteArrayOutputStream.close();
  } catch (IOException e) {
      // TODO provide runtime exception to reach it to presentation layer
      e.printStackTrace();
  } catch (Exception ex) {
      ex.printStackTrace();
  }
  </code></pre>
  
  <p>recLine is the TargetDataLine from which I am recording sound in to byteArrayOutputStream. it works normally until 40 - 48 seconds very well in my test , but when it reaches 49 seconds every time it throws an exception below :</p>
  
  <pre><code>Exception in thread "Thread-5" java.lang.OutOfMemoryError: Java heap space
      at java.util.Arrays.copyOf(Arrays.java:2786)
      at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
      at com.actura.app.capture.ActuraRecorder.run(ActuraRecorder.java:109)
      at java.lang.Thread.run(Thread.java:619)
  </code></pre>
  
  <p>I am using this technique because I need the recorded bytes and from those bytes I am drawing a wave form succesfully on the UI.</p>
  
  <p>During test I come to know that an exception raised when the size of the byteArrayOutputStream is only 7.3 mb. </p>
  
  <p>Can I write this byteArrayOutputStream to random access file and then reset this byteArrayOutputStream everytime it reaches to its limit?</p>
  
  <p>How do I know in advance the limits of the byteArrayOutputStream?</p>
  
  <p>I checked with Integer.MAX_VALUE but as I said it just raised an exception at 7.3 mb so I can not reach to Integer.MAX_VALUE.</p>
  
  <p>This applet runs on the internet, so setting memory size will not help me. How can I set it to my client's computer?</p>