ByteArrayOutputStream Out of memory error while recording in java sound
<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 bytes which is being recorded  and from those bytes i am drawing wave form succesfully on the UI.</p>

<p>during test i come to know that 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>so how do i know in advance the limit's of the byteArrayOutputStream ?</p>

<p>i checked with Intgeer.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>another thing 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>

<p>Thank You Again for all of your suggestions.
Mihir Parekh</p>
