简体   繁体   中英

Java Out of Heap Space

In my java program I am using text to speech to speak out numbers, after it speaks up around 150 numbers it goes out of heap space (when opened from JAR) I did try to increase the heap space too but still the problem persists.

What all I tried:

Using batch scripting:

java -Xmx1024m -jar Super_Brain Academy.jar
pause

[ Super_Brain Academy.jar is the file name] but this still doesn't solve my problem as it increases the heap space only for the main JAR file and not the TTS(freeTTS).

I also tried to make a fat Jar and then do this, but the problem is when I make the fat JAR the TTS stops working and I am unable to listen to anything. I tried to make the fat JAR by this method. I also tried to make fat JAR from other methods given on stackoverflow but they were confusing and I could not understand them. I am a newbie(well you might have figured that out till now) and using Netbeans 8.

Code

According to my knowledge I think that the error must be coming this code block which is in a separate java file:

   private static final String VOICENAME = "kevin16";

   VoiceManager voiceManager = VoiceManager.getInstance();

//Some code here..........

private void RandomjBActionPerformed(java.awt.event.ActionEvent evt) {                                         
//When the button is clicked it generates one random number according to the conditions and speaks it up
    Voice voice;
    voice = voiceManager.getVoice(VOICENAME);
    voice.allocate();

    //Some variable declarations and initializations along with set visible commmands were here(removed as irrevelant for the problem)

    for (g = 0; g==0;) {
        ran = (int) (Math.random() * (c)); // Random Number Creation Starts
      if(ran>=c/10){

           g = 1;
           //Some logic statements here

      }//IF ENDS  
      if(g!=0){
          if(num<0){
              num = num * (-1); // MAKING NUM ALWAYS +ve
          }
      }//if(g!-0) ends here  
    }//for loop ends
    d = d+ran;
    t4.setText("" + ran);
    voice.speak(t4.getText());
    listenanum.setText("" + d);
    listenanum.setVisible(false);
}                                       

This randomjB is clicked again and again and that's when the problem occurs. (I have removed many lines from the above code so it may not practically make sense to you... but I have left only those lines which I feel are important)

For full code of randomjB check this

As per you comments I think it is an issue with the memory management.

I found this link very helpful. You can follow the same wherever it is applicable to make your program block memory.

After optimizing aslo if you get the same error message, you can go through this link to learn how to increase the heap size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM