OutOfMemoryError when using java Clip

advertisements

Solved: I solved it!! I had to increase heap size of my project properties in netbeans ide, not in the netbeans ide configuration itself

I have a game application which is very simple, I use a Clip object from javax.sound.sampled package to play a background music(an mp3 format file that is almost 9m long and 8mb in size) I converted it to a wav file and it became 87mb >_<. And I have small wav files I use for buttons. The problem is I get an OutOfMemoryError each time I terminate my program. I have made an app that imitates somewhat the same problem only if you click the short clips multiples times after clicking the long wave file first and then click terminate button to end it and then I receive that error. However I do not know how to provide the same wav files for others to try and test this sample

import javax.swing.*;
import javax.sound.sampled.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class ClipClass extends JPanel implements ActionListener{
private JButton longClip, shortClip1,shortClip2,quit;
public ClipClass(){
    setLayout(new GridLayout(2,2,0,0));
    longClip = new JButton("Play long wav");
    shortClip1 = new JButton("Play short wav1");
    shortClip2 = new JButton("Play short wav2");
    quit = new JButton("Terminate");
    add(longClip);
    add(shortClip1);
    add(shortClip2);
    add(quit);
    longClip.addActionListener(this);
    shortClip1.addActionListener(this);
    shortClip2.addActionListener(this);
    quit.addActionListener(this);
}
public synchronized void playSound(final String url) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                Clip clip = AudioSystem.getClip();
                AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
                clip.open(inputStream);
                clip.start();
                clip.addLineListener(new LineListener() {
                    @Override
                    public void update(LineEvent evt) {
                        if (evt.getType() == LineEvent.Type.STOP) {
                            evt.getLine().close();
                        }
                    }
                });
            } catch (Exception e) {
            }
        }
    });
}
public static void main(String[] args){
    JFrame frame = new JFrame("SampleSoundOOME");
    ClipClass pane = new ClipClass();
    frame.setContentPane(pane);
    frame.setVisible(true);
    frame.pack();
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==longClip){
        playSound("C:/Users/Sony/Documents/NetBeansProjects/Sphere Break/Trance.wav");
    }if(e.getSource()==shortClip1){
        playSound("C:/Users/Sony/Documents/NetBeansProjects/Sphere Break/KDE_Window_Sticky.wav");
    }if(e.getSource()==shortClip2){
        playSound("C:/Users/Sony/Documents/NetBeansProjects/Sphere Break/KDE_Window_Iconify.wav");
    }if(e.getSource()==quit){
        System.exit(0);
    }
}
}


The reason behind this is that your program uses too much memory that exceeds the heap size you have assigned (or default size). Memory leak is can also be responsible for this. You can help the Java Garbage Collector by nullifying all your reference. Otherwise, you can set the heap size through -Xmx256m where m = Megabyte.