How to avoid “java.lang.OutOfMemoryError: Java heap space” error?

java heap

Temporary Solutions :

  • Open catalina.sh from tomcat/bin.
  • Set or change JAVA_OPTS to

 JAVA_OPTS=”-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m -Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC” 
  • Restart the tomcat server.
Increasing the heap size is not a fix it is a plaster work, 100% temporary. It will crash again in somewhere else,to avoid these issues, write high performance code.

Best Solution:

  • Use local variables wherever possible.
  • Make sure you select the correct object .
  • Example: Selection between String, StringBuffer and StringBuilder).
  • Use a good code system for your program.
  • Example: Using static variables VS non static variables.
  • Other stuff which could work on your code.
  • Try to use Multi Threading.

About Manohar

I, Manohar am the founder and chief editor of ClarifyAll.com. I am working in an IT professional.

View all posts by Manohar →

Leave a Reply