Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Do I need to close an audio clip?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    Default Do I need to close an audio clip?

    have an application that processes real-time data and is supposed to beep when a certain event occurs. The triggering event can occur multiple times per second, and if the beep is already playing when another event triggers the code is just supposed to ignore it (as opposed to interrupting the current beep and starting a new one). Here is the basic code:

        Clip clickClip
        public void prepareProcess() {
            clickClip = null;
            try {
                clipFile = new File("C:/WINDOWS/Media/CHIMES.wav");
                ais = AudioSystem.getAudioInputStream(clipFile);
                clickClip = AudioSystem.getClip();
                fileIsLoaded = true;
            } catch (Exception ex) {
                clickClip = null;
                fileIsLoaded = false;
        public void playSound() {
            if (fileIsLoaded) {
                if ((clickClip==null) || (!clickClip.isRunning())) {
                    try {
                    } catch (Exception ex) {
                        System.out.println("Cannot play click noise");

    The prepareProcess method gets run once in the beginning, and the playSound method is called every time a triggering event occurs. My question is: do I need to close the clickClip object? I know I could add an actionListener to monitor for a Stop event, but since the event occurs so frequently I'm worried the extra processing is going to slow down the real-time data collection.

    The code seems to run fine, but my worry is memory leaks. The code above is based on an example I found while searching the net, but the example used an actionListener to close the Clip specifically "to eliminate memory leaks that would occur when the stop method wasn't implemented". My program is intended to run for hours so any memory leaks I have will cause problems.

    I'll be honest: I have no idea how to verify whether or not I've got a problem. I'm using Netbeans, and running the memory profiler just gave me a huge list of things that I don't know how to read. This is supposed to be the simple part of the program, and I'm spending hours on it. Any help would be greatly appreciated!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Do I need to close an audio clip?

    The simple answer is yes, close any files/streams you are using once you are done with them. Better safe than sorry

    Depending on the stream, you might be able to get away with not closing the stream, but it's a simple enough procedure that could prevent a potentially difficult problem to debug.

Similar Threads

  1. Help with java audio samples please!
    By Reg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2010, 08:41 AM
  2. close JDialog on button click
    By Christophe in forum AWT / Java Swing
    Replies: 4
    Last Post: April 4th, 2010, 11:04 PM
  3. Fill in rectangle partially (clip?)
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 5
    Last Post: March 27th, 2010, 01:38 PM
  4. sun.audio
    By bguy in forum Java SE APIs
    Replies: 3
    Last Post: November 12th, 2009, 01:17 AM
  5. Java program to open and close computer CD/DVD drive
    By JavaPF in forum Java Programming Tutorials
    Replies: 0
    Last Post: January 28th, 2009, 12:04 PM

Tags for this Thread