Sound leak

Good morning,
Im having a problem playing sounds, there is somewhere a memmory leak, but i cant seem to find out where it is. I have closed everything and catched everything off. Can anybody see my problem?
This is how i play a sound:

Sound sound = new Sound("NotenoughMoney.wav",5000);

and this is my sound class:

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;


public class Sound extends Thread{
	boolean playing;
	int length, time;
	Clip clip = null;
	AudioInputStream sound = null;
	File soundFile = null;
	DataLine.Info info = null;
	
	Sound(String location, int length){
		soundFile = new File(location);
		this.length = length;
		playing = true;
		this.start();
	}
	
	private void play(){
		try {
			sound = AudioSystem.getAudioInputStream(soundFile);
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	    info = new DataLine.Info(Clip.class, sound.getFormat());
		try {
			clip = (Clip) AudioSystem.getLine(info);
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		}
			try {
			clip.open(sound);
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		    clip.addLineListener(new LineListener(){public void update(LineEvent event){}});
		    clip.start();      
	}
	
	public void run(){
		play();
		while(playing){
			time++;
			try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
			if(time>=length)
				playing = false;
		}
		System.out.println("CLOSE");
		close();
	}
	
	private void close(){
		clip.stop();
		soundFile.delete();
		clip.close();
		try {
			sound.close();
		} catch (IOException e) {e.printStackTrace();}
	}
}

Does anybody see my problem here? ???

bump bump bump. still with this problem.
somebody help me? :-*

What are the symptoms of having a memory leak? How are you measuring this? Have you looked at the heap usage with jvisualvm or looked at a heap dump with the Eclipse heap profiler?

not so much at the same time! :expressionless:
Im measuring this with the most useful tool ever made, windows task manager.
Im just looking at the memory costs of my game, then as soon as a sound starts memory size increases and decreases again. But the amount that it decreases is smaller then the increasement! :frowning:

That’s normal. Are you actually running out of memory? Is it growing continuously?

try Runtime.freeMemory() too see if its constantly growing (as this method is for free memory, it should shrink of course)
also dont use Java Sound =)

Nah i dont thats normal lol. It should give me back all the memmory, and more!
No but seriously, it keeps continueing with each sound play.
Also cero, what do you suggest i use for sound then?

Why not using Paul Lamb Sound Library with one of its OpenAL (JOAL, LWJGL) plug-ins?

sproingie’s point is that if you’re not receiving OutOfMemoryErrors, there’s likely nothing to worry about. The JVM isn’t freeing every byte of memory as soon as it’s no longer needed. It’s using fancy-smancy algorithms to try to optimize the user experience. This often results in the heap growing over time, which makes programmers looking at Task Manager think “I have a memory leak!” when they really don’t; it’s just the JVM choosing to hold onto memory for some (legitimate) reason. If if needed to free the memory, it would.

PS: It seems odd that you’re deleting the sound file from the disk after you play it. Are you sure you want to do that?

Java Sound is horrible anyway
use FMOD (Native FMOD Ex for java), but to use FMOD commercial costs money; or OpenAL (you can use the Slick Util which makes it easy)

Hi elamre -

Your use of the Clip is kind of goofy, but I made the same mistake as do lots of other people. The “normal” way to use a Clip is to load it ONLY ONCE, and if you play multiple times, setFramePosition() or setMicrosecondPosition() back at the start of the audio. If you reload it and close it every time you play it, you might as well use a SourceDataLine as it starts quicker. A Clip won’t play until the entire sound file has been loaded into memory.

http://docs.oracle.com/javase/tutorial/sound/playing.html – see the section on Clip.

Who knows what Microsoft does when asked to do all those Clip reloads. I don’t. But I could see that thrashing as maybe causing the TaskManager to show some bloating.

Another thing, a bit of example code on the same tutorial, down a couple paragraphs, gives a last step of setting the line to null to release it, but you don’t do this. I don’t know for sure how necessary this is, but it is a step that I always include.

BTW, just a heads up but the way you load the AudioInputStream will most likely fail in Java 7 via a Mark/Reset error. Better to use the following form instead:

      URL url = AudioMixer.class.getResource(fileName);
      AudioInputStream ais = 
         AudioSystem.getAudioInputStream(url);

ra4king helped me debug that one!