Java Sound; multipul clips

Hi, I'm trying to have a thread that will be able to load multiple clips and add them to a list of clips.

When I run my program my first clip loads with no problem, however when I invoke clip.open(AudioInputStream) for the second clip i get the following error: java.lang.OutOfMemoryError: Java heap space

My code is below:

/**

* This class is a Thread and is responsible for loading each clip, one at a time

* so the threading is so that it does not interfer with playback.

**/

import javax.sound.sampled.*;

import java.io.*;

import java.util.*;

publicclass SongLoaderextends LinkedList<File>implements Runnable{

List<Clip> clipList;

boolean running =false;

public SongLoader(List<Clip> cList){

super();

clipList = cList;

}

publicboolean offer(File f){

boolean result = super.offer(f);

if(!running){

new Thread(this).start();

running =true;

}

return result;

}

publicvoid run(){

while(size() != 0){

System.out.println("Loading: " + peek().toString());

System.out.println("\tFree Memory: " + Runtime.getRuntime().freeMemory());

AudioInputStream aInput = getAudioInputStream(remove());

System.out.println("\t\tGot AudioInputStream");

try{

Clip loading = AudioSystem.getClip();

System.out.println("\t\tGot Clip from AudioSystem");

loading.open(aInput);

System.out.println("\t\tLoading Started");

clipList.add(loading);

System.out.println("\tLoading Complete; Free Memory: " + Runtime.getRuntime().freeMemory());

aInput.close();

System.out.println("\t\tClosed AudioInputStream");

}catch(Exception e){

System.out.println("Error in SongLoader: " + e);

}

}

running =false;

}

private AudioInputStream getAudioInputStream(File f){

AudioInputStream aIn =null;

try{

BufferedInputStream dataIn =new BufferedInputStream(new

FileInputStream(f));

AudioFileFormat fileFormat =

AudioSystem.getAudioFileFormat(dataIn);

AudioFormat format = fileFormat.getFormat();

aIn =new AudioInputStream(dataIn, format, fileFormat.getByteLength());

}catch(Exception e){

System.out.println("Error in Songloader: " + e);

System.exit(0);

}

return aIn;

}

}

Thank you for your help.

[4397 byte] By [webaf409java] at [2007-9-30 22:12:24]
# 1
(title should read "multiple clips")
webaf409java at 2007-7-7 11:25:30 > top of Java-index,Administration Tools,Sun Connection...
# 2

When you start your program, use the -Xmx option on the java command to allow more heap space to be used.

-Xmxn

Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. Examples:

-Xmx83886080

-Xmx81920k

-Xmx80m

Do not allocate too much, as Java and other programs running also need theire share of memory. A rule-of -thumb is no more than 50 - 70 % of the computer's physical memory. Try upping the value from 64MB in 64MB increments and see what happens.

ChuckBing at 2007-7-7 11:25:30 > top of Java-index,Administration Tools,Sun Connection...
# 3

Hi, I am still having the same problem although I have used -Xmx128m and -Xmx512m.

Currently the two files I am trying to open in my thread total about 32MB, which is not too large, so this error is a little supprising. Is there anything in my code that I can't do? Also, I'm using a beta of 1.5, so i'll try to get that up to date soon, but I looked at the fixed bugs between the beta and the official release and didn't see anything that seems to be the problem.

webaf409java at 2007-7-7 11:25:30 > top of Java-index,Administration Tools,Sun Connection...
# 4

Do you know at what point in the program that the error is occurring? Do your println statements tell you anything?

Is it possible that some code is loading duplicates of the clips?

I agree, it's unlikely that the problem is a simple lack of heap memory, rather there's a problem somewhere. I woul definitely get off of the beta Java.

You may need to run a profiler to see what's happening. If so, just search the forums for names.

ChuckBing at 2007-7-7 11:25:30 > top of Java-index,Administration Tools,Sun Connection...
# 5
It'll be a couple of days before I can upgrade, but thank you for the help. I'm gonna give you half the dukes and see how it goes when I get onto the real Java 1.5
webaf409java at 2007-7-7 11:25:30 > top of Java-index,Administration Tools,Sun Connection...