How to increase memory allocated to java? java.lang.OutOfMemoryError: Java heap space

In my system I have edited the file

/etc/init.d/tomcat 

with these rows

HEAP="-Xms2048m -Xmx2048m" 
export JAVA_OPTS="$HEAP"

After restart tomcat you can see in the status section the memory allocated


You can also do it in tomcat/bin/setenv.sh file and restart tomcat:

export JAVA_OPTS="-Xms512m -Xmx1024m"

The reason for the error is due to the way Java allocates memory. Your applications are allowed to use only limited amount of memory. This limit is specified during the application startup.

Allowed maximal size is set during the JVM launch by specifying JVM parameters such as -Xmx. If you do not explicitly set the sizes, platform-specific defaults will be used.

So – the “java.lang.OutOfMemoryError: Java heap space” error will be triggered when you try to add more data into the heap space area in memory, but the size of this data is larger than the JVM can accommodate in the Java heap space.

So the obvious way is to increase (or add if missing) the maximal possible heap size specified via -Xmx parameter. But in many cases you are just hiding symptoms instead of solving the underlying problem. To see the full explanation of the java.lang.OutOfMemoryError: Java heap space error, check out the Plumbr performance tuning site.