问题
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?
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);
}
}
}
回答1:
Try this
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);
clip.addLineListener(new LineListener(){
public void update(LineEvent event){
if(event.getType() == LineEvent.Type.STOP){
event.getLine().close();
}
}
});
clip.start();
Thread.sleep(2000);
if (clip.isOpen()) {
clip.close();
sound.close();
}
}
}
}
回答2:
If you are sure there is a memory leak and not a garbage collect delay, you could start with closing and cleaning up all resources you allocate, removing the listener and setting all references to null
. (Move the File
object out of the loop too.)
If that doesn't help and the javadocs don't hint at what can be cleared you can trigger a heapdump to see what objects use the memory and what their path to root is.
来源:https://stackoverflow.com/questions/1816832/java-clip-sound-audio-memory-leak-after-closing-with-close