Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Sound leak  (Read 4180 times)
0 Members and 1 Guest are viewing this topic.
Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Posted 2012-02-07 09:57:41 »

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:
1  
Sound sound = new Sound("NotenoughMoney.wav",5000);

and this is my sound class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
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? Huh

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #1 - Posted 2012-02-16 18:47:04 »

bump bump bump. still with this problem.
somebody help me? Kiss

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline sproingie

JGO Kernel


Medals: 202



« Reply #2 - Posted 2012-02-16 19:28:57 »

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?


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #3 - Posted 2012-02-16 19:38:15 »

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! :|
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! Sad

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline sproingie

JGO Kernel


Medals: 202



« Reply #4 - Posted 2012-02-16 19:44:24 »

That's normal.  Are you actually running out of memory?  Is it growing continuously?
Offline Cero
« Reply #5 - Posted 2012-02-16 19:53:15 »

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 =)

Offline elamre

JGO Coder


Medals: 17
Projects: 1


hitar!


« Reply #6 - Posted 2012-02-16 19:56:04 »

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


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?

My projects:
Tower Defence!]http://www.java-gaming.org/topics/iconified/25690/view.html]Tower Defence! [lll.......] 30%!
Lightsnakerider! [llllll....] 60%!
Offline gouessej
« Reply #7 - Posted 2012-02-16 20:47:36 »

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?

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #8 - Posted 2012-02-16 20:53:20 »

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


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?

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? 
Offline Cero
« Reply #9 - Posted 2012-02-16 22:50:36 »

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)

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #10 - Posted 2012-02-19 14:37:52 »

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:

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


ra4king helped me debug that one!


"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (103 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!