Java Clip (Sound / Audio) Memory Leak after closin

2019-03-06 09:01发布

问题:

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.