6 Replies Latest reply: Aug 10, 2010 10:03 AM by Miguel Angel RSS

    Out ot memory

    Miguel Angel
      I got this error: Java lang out of memory when i am saving audio in aByteArrayOutputStream,i can only record about 8 minutes. The initial size of memory is: 256m
      My code is:
      public void run(){
      
             int frameSizeInBytes = frtoData.getFrameSize();
             int bufferLengthInFrames = line.getBufferSize() / 8;
             bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
             bufferTemp = new byte[bufferLengthInBytes];
      
             while (!stopped) {
                      if (paused) {
                          synchronized (this) {
                              while (paused) {
                                  try {
                                      position = (int) (line.getMicrosecondPosition() / 1000000);
                                      this.wait();
                                  } catch (InterruptedException ex) {
                                      Logger.getLogger(Audio.class.getName()).log(Level.SEVERE, null, ex);
                                  }
                              }
                          }
                      }
                      if ((numBytesRead = linea.read(bufferTem, 0, bufferLengthInBytes)) == -1) {
                          break;
                      }
                       rewind.write(bufferTemp 0, numBytesRead);
              }
      code}
      
      rewind is an object of a class: RewindAudio that extends from ByteArrayOutputStream.
      I can save more data audio,about 17 minutes, if i increment the size of memory with this: -Xmx450m, but i have to get to at least 1 hour.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: Out ot memory
          843802
          Why do you need to store all the sound in an array?
          • 2. Re: Out ot memory
            Miguel Angel
            Because:
            1) I can playback the recording without saving and opening from a file directory .
            2) I need to make some volume analysis when recording. So i need and array of bytes.
            • 3. Re: Out ot memory
              843802
              a05207811 wrote:
              Because:
              1) I can playback the recording without saving and opening from a file directory .
              Why is this an advantage? Since you are running out of memory it would seem to be a distinct disadvantage.
              2) I need to make some volume analysis when recording. So i need and array of bytes.
              This does not make sense to me. I do volume analysis without holding all the sound in memory.
              • 4. Re: Out ot memory
                Miguel Angel
                About the analysis of volumen can you tell what is the logic that you use, because i have a method that needs and array of bytes to make the analysis:
                public void setVolume(byte[] b, int off, int length) {
                
                        int finalBuffer = off + b.length;
                        int sampleRate = (frtoDatos.getSampleSizeInBits() + 7) / 8;
                        int max = 0;
                        if (sampleRate == 1) {
                            //8 bits:
                            for (; off < finalBuffer; off++) {
                                int muest = (byte) (b[finalBuffer] + 128);
                                if (muest< 0) {
                                    muest = -muest;
                                }
                                if (muestreo > max) {
                                    max = muest;
                                }
                            }
                            maxVolume = max;
                        } else if (sampleRate == 2) {
                            if (frtoDatos.isBigEndian()) {
                                // 16-bit big endian
                                for (; off < finalBuffer; off += 2) {
                                    int muest = (short) ((b[off] << 8) | (b[off + 1] & 0xFF));
                                    if (muest< 0) {
                                        muest = -muest;
                                    }
                                    if (muest > max) {
                                        max = muest;
                                    }
                                }
                            } else {
                                // 16-bit little endian
                                for (; off < finalBuffer; off += 2) {
                                    int muest = (short) ((b[off + 1] << 8) | (b[off] & 0xFF));
                                    if (muest < 0) {
                                        muest = -muest;
                                    }
                                    if (muest > max) {
                                        max = muest;
                                    }
                                }
                            }
                            maxVolume= max >> 8;
                        } else {
                            maxVolume = -1;
                        }
                        prgVolumen.setValue(maxVolume);  // setting the maxVolume in a JProgressBar
                    }
                I need the arrary because I m reading from the line, an saved what i 've got in an array of bytes.

                I will continue posting tomorrow, because know i got go to my classes.
                • 5. Re: Out ot memory
                  captfoss
                  a05207811 wrote:
                  what is the logic that you use
                  If you store your sound data in a file, and then read from it using a RandomAccessClass, you can dynamically refill a buffer of any length from anywhere within the file... you can also dynamically overwrite sections of the file from anywhere within the file.

                  [http://download.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html]

                  So you can be recording to the file in one thread, setting the volume on another thread, running your analysis in a 3rd thread... assuming you open the file with the correct mode, handle the thread syncrononization correctly, etc...
                  • 6. Re: Out ot memory
                    Miguel Angel
                    ok i ll try that.