The following code creates a new audio clip, plays it, sleeps for 3 seconds and then closes it when it is finished playing. Despite the call to close(), I am watching the memory usage of the jvm go up by the size of the sound clip every time the while loop is run.
I'm participating in a game coded in java, and am handling the sound. I cannot have the memory i'm using increase everytime a sound is played.
What am I missing?
Thanks! John
import java.io.File;
import javax.sound.sampled.*;
public class ClipLeak{
public static void main(String[] args) throws Exception{
while(true){
File soundFile = new File("./sound.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
sound.close();
clip.addLineListener(new LineListener(){
public void update(LineEvent event){
if(event.getType() == LineEvent.Type.STOP){
event.getLine().close();
}
}
});
clip.start();
Thread.sleep(2000);
}
}
}