d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Avoid Memory Leak With Java Audio
Add Reply New Topic New Poll
Member
Posts: 7,635
Joined: Jul 3 2007
Gold: 243.00
Jun 3 2012 06:28pm
I call it memory leak, you can call it whatever you want :P

I'm making my first game for a school project but I have an Issue managing the Audio side.

Everytime I call Shoot, it starts the 1 second clip but it stays in the memory. The only way I can avoid this so far is to do the Clip.Close() but thats going to stop the audio right after starting it so it doesnt help.

Code
SoundManager sm = new SoundManager();
public synchronized void shoot() {
     
       if (tm.mesure() > 100) {
           pro.add(new Projectiles(position, projectileSpeed));
           sm.shoot();
           tm.mark();
       }


SoundManager class
Code
void shoot() {
       try {
           AudioInputStream audio = AudioSystem.getAudioInputStream(new File("impact.wav"));
           Clip clip = AudioSystem.getClip();
           clip.open(audio);
           clip.start();
           
         
       } catch (UnsupportedAudioFileException ex) {
           Logger.getLogger(SoundManager.class.getName()).log(Level.SEVERE, null, ex);
       } catch (IOException ex) {
           Logger.getLogger(SoundManager.class.getName()).log(Level.SEVERE, null, ex);
       } catch (LineUnavailableException ex) {
           Logger.getLogger(SoundManager.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 3 2012 07:18pm
1. use a line listener
2. if #1 doesn't work, the more general answer is to use a patterns to treat asynchronous methods as synchronous which solves this problem. alternatively, you can use a queue. create a wrapper for the clip so you can store when it started (or does it have a state that tells you when it's done?). when you start the clip, add to the queue. have a separate thread look at the queue and close/dequeue when enough time passes
3. if you "shoot" faster than the duration of the clip, what should happen? if you only want one sound file to run at any given time, i recommend keeping an object-level variable instead of recreating it every time you shoot.
4. you should have a single static logger at the class level. dont keep redeclaring it every time you use it.

This post was edited by carteblanche on Jun 3 2012 07:19pm
Member
Posts: 7,635
Joined: Jul 3 2007
Gold: 243.00
Jun 3 2012 08:30pm
Quote (carteblanche @ 3 Jun 2012 21:18)
1. use a line listener
2. if #1 doesn't work, the more general answer is to use a patterns to treat asynchronous methods as synchronous which solves this problem. alternatively, you can use a queue. create a wrapper for the clip so you can store when it started (or does it have a state that tells you when it's done?). when you start the clip, add to the queue. have a separate thread look at the queue and close/dequeue when enough time passes
3. if you "shoot" faster than the duration of the clip, what should happen? if you only want one sound file to run at any given time, i recommend keeping an object-level variable instead of recreating it every time you shoot.
4. you should have a single static logger at the class level. dont keep redeclaring it every time you use it.


thanks you for the reply. I was going to try line #1 but im going to get OpenAL instead ;) More tutorials and some cool features.
Member
Posts: 3,640
Joined: Dec 5 2005
Gold: 8,518.83
Jun 6 2012 07:10am
Maybe you can give more memory to the virtual machine so you can avoid that leak of it.
Member
Posts: 7,635
Joined: Jul 3 2007
Gold: 243.00
Jun 7 2012 05:38am
Quote (strator @ 6 Jun 2012 09:10)
Maybe you can give more memory to the virtual machine so you can avoid that leak of it.


???
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll