1

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);
    }
}
}
katrina
  • 11
  • 2
  • Rather than use all the if statements, which isn't very OO, you could use nested classes or a new ActionLister, then overwrite the actionPerformed method. – maffo Jan 29 '13 at 07:11
  • try to use DiskFile instead of File. I'm not sure about this case, but in some other cases it saves lot of memory. – dinesh707 Jan 29 '13 at 07:22
  • @PoiXen: Thanks for the suggestions(I will try this in smaller apps next time) but in my real application, I have a button of array(its for game mechanics) so I use if statements inside for loops in mouseClicked method. – katrina Jan 29 '13 at 07:25

2 Answers2

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.

Michael 'Maik' Ardan
  • 4,213
  • 9
  • 37
  • 60
  • Same as the answer above and I have the same comment – katrina Jan 29 '13 at 07:29
  • What was the heap size have you allocated? – Michael 'Maik' Ardan Jan 29 '13 at 07:33
  • this is the lines that is where I am supposed to change the heapsize?-> netbeans_default_options="-J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.zip.disableMemoryMapping=true" ->>>> what am I missing or what do I have to change(By the way I already removed the change I made which was like this:-J-XX-MaxPermSize=500m – katrina Jan 29 '13 at 07:51
0

please check the JVM heap size, based on your .wav file sizes memory allocated to the JVM heap might not be sufficient.

Increase jvm heap using: java -Xmx** - where is memory you can allocat

Also, on exception :

catch (Exception e) {
           //close `inputStream` 
           // close `clip`
}
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • I have tried a solution using this:http://wiki.netbeans.org/FaqSettingHeapSize but it didnt work – katrina Jan 29 '13 at 07:28
  • I have tried a solution using this:http://wiki.netbeans.org/FaqSettingHeapSize but it didnt work. Also, I had to take out those 2 objects as a public member of the class to be able to close it in the catch. What exactly will that do to the process? Also the part where I added a LineListener is a code I do not fully understand but I know that it solved my OutOfMemoryError that crashes the application. – katrina Jan 29 '13 at 07:45