How to set, change, increase or decrease heap size in tomcat server and eclipse to avoid OutOfMemoryError ?




Contents of page :
  • 1) When to change heap size in tomcat server?
  • 2) How to set or change heap size in tomcat server?
  • 3) Where is exactly catalina.bat file located in windows?
  • 4) Where is exactly catalina.bat file located in linux/ Mac OS?
  • 5) How to change heap size in tomcat server in windows, linux and Mac platform >
    • 5.1) How to change heap size in tomcat server in windows platform >
    • 5.2) How to change heap size in tomcat server in Linux platform >
    • 5.3) How to change heap size in tomcat server in Mac OS platform >
  • 6) You may use following VM (JVM) PARAMETERS to set up heap memory >
  • 7) You must also know how to make changes in permgen memory (or permanent generation or permanent space) JVM parameters >
  • 8) How to set or change heap size in eclipse?


1) When to change heap size in tomcat server?
Generally when we are facing java.lang.OutOfMemoryError - Java heap space then we need to change heap of tomcat.


2) How to set or change heap size in tomcat server?
For setting, changing, increasing or decreasing heap size in tomcat server you need to make changes values in the Tomcat Catalina start file. Change CATALINA_OPTS option in the file.
It will help us in avoiding avoiding java.lang.OutOfMemoryError - Java heap space

3) Where is exactly catalina.bat file located in windows?
tomcatServerHome\bin\catalina.bat

4) Where is exactly catalina.bat file located in linux/ Mac OS?
tomcatServerHome\bin\catalina.sh



5) How to change heap size in tomcat server in windows, linux and Mac platform >

5.1) How to change heap size in tomcat server in windows platform >
Open or create setenv.bat file (Location of setenv.bat file is tomcatServerHome\bin\setenv.bat)

set CATALINA_OPTS=-server -Xms512m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=1024m

Best practices while setting heap size in tomcat server in windows platform >
You should not modify tomcatServerHome\bin\catalina.bat. You should create a new file in tomcatServerHome\bin\setenv.bat to keep your custom environment configurations separate from default tomcat server configurations.



5.2) How to change heap size in tomcat server in Linux platform >
Open or create setenv.sh file (Location of setenv.bat file is tomcatHome\bin\setenv.sh)

export CATALINA_OPTS="$CATALINA_OPTS=-server -Xms512m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=1024m


Best practices while setting heap size in tomcat server in linux platform >
You should not modify tomcatServerHome\bin\catalina.sh. You should create a new file in tomcatServerHome\bin\setenv.sh to keep your custom environment configuartions seperate from defult tomcat server configurations.


5.3) How to change heap size in tomcat server in Mac OS platform >
Open or create setenv.sh file (Location of setenv.bat file is tomcatHome\bin\setenv.sh)

export CATALINA_OPTS="$CATALINA_OPTS=-server -Xms512m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=1024m"


Best practices while setting heap size in tomcat server in Mac OS platform >
You should not modify tomcatServerHome\bin\catalina.sh. You should create a new file in tomcatServerHome\bin\setenv.sh to keep your custom environment configuartions seperate from defult tomcat server configurations.


6) You may use following VM (JVM) PARAMETERS to set up heap memory >
Learn how to pass vmargs (VM parameters) to java program in eclipse?

-Xms : Xms is minimum heap size which is allocated at initialization of JVM.

Examples of using -Xms VM (JVM) option in java >

Example1 of using -Xms VM (JVM) option in cmd  in java >
java -Xms512m MyJavaProgram
It will set the minimum heap size of JVM to 512 megabytes.

Example2 of using -Xms VM (JVM) option in cmd  in java >
java -Xms1g MyJavaProgram
It will set the minimum heap size of JVM to 1 gigabyte.


For more explanation and example - Read : What are -Xms and -Xmx JVM parameters and differences between them



-Xmx : Xmx is the maximum heap size that JVM can use.

Examples of using -Xmx VM option in java >

Example1 of using -Xmx VM (JVM) option in cmd  in java >
java -Xmx512m MyJavaProgram
It will set the maximum heap size of JVM to 512 megabytes.

Example2 of using -Xmx VM (JVM) option in cmd  in java >
java -Xmx1g MyJavaProgram
It will set the maximum heap size of JVM to 1 gigabyte.



What is the maximum heap size value you can set on your system, if you aren’t sure about the system configurations?
Try out for -Xmx:256m, if it works then try -Xmx:512m, if it works then try -Xmx:1024m and so on. Be cautious before going beyond 8g.







7) You must also know how to make changes in permgen memory (or permanent generation or permanent space) JVM parameters >

Read : How to set or change permgen size in tomcat server, eclipse?






8) How to set or change heap size in eclipse?
Open eclipse.ini file, it is located in root eclipse directory. there you can change -Xms and -Xmx parameter to change heap size.
  • -Xms (-Xms is minimum heap size which is allocated at initialization of JVM),
  • -Xmx (-Xmx is the maximum heap size that JVM can use.)

Read : What is eclipse.ini file? How to pass vmargs to java program in eclipse? Changing the eclipse setting, What are best eclipse setting?







SUMMARY>
So in this core java tutorial we learned how to set, change, increase or decrease heap size in tomcat server and eclipse to avoid OutOfMemoryError : Java heap space.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>




JVM in detail - Garbage collection & heap structure >

>JVM Heap memory (Hotspot heap structure) with diagram in java


Young, Old (tenured) and Permanent Generation >

>What are Young, Old (tenured) and Permanent Generation in JVM in java


Minor, Major and Full garbage collection >

>What are Minor, Major and Full garbage collection in JVM in java



Important VM parameters >

>Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java

>What are -Xms and -Xmx JVM parameters in java, And differences between them with examples

>-Xmn JVM parameters in java with examples - Setting young generation size

>What is -XX:NewRatio JVM parameters in java with examples - Setting young and old generation ratio

>What are -XX:NewSize and -XX:MaxNewSize JVM parameters in java

>-XX:SurvivorRatio JVM parameters in java with examples - Setting survivor spaces size


Eclipse and VM parameters >

>What is eclipse.ini file? How to pass vmargs to java program in eclipse? Changing the eclipse setting, What are best eclipse setting?



Monitor, analyze garbage collection and fix MEMORY LEAK >

>How to monitor and analyze the garbage collection in 10 ways in java

>Detecting and fixing memory leak in java


Pass VM para through CMD, eclipse to java program and to Apache tomcat >

>How to write java program to pass VM/JVM parameters through CMD

>How to pass vmArgs(JVM parameters) to java program in eclipse

>How to pass VM argument to tomcat in eclipse



eEdit
Must read for you :