when you are using tomcat as you application server for not small application you can easy get (out of memory exception). this is because the default heap size tomcat use is small and suitable only for small web applications.
to set the start and maximum heap size run the following command before starting your tomcat:
export CATALINA_OPTS=”-Xms256m -Xmx512m”
or
export JAVA_OPTS=”-Xms256m -Xmx512m”
this will create environment variable called CATALINA_OPTS or JAVA_OPTS contains the required options to make tomcat start heap size 256M and maximum heap size 512M.
September 22, 2008 at 10:01 am
I am new for Tomcat6 and I am trying to increase my heap size. I have the Windows NT.
But I do not know how to run this command stated above? From where ?
I also looked for the Catalina.sh or Catalina.bat files ın the Tomcat ınstall directory ın order to change the environment varıable manually, but these fıles do not exist. Can you plese help me ?
September 22, 2008 at 11:09 am
Dear Fatih,
to set the memory heap size in windows you have to create environment variable with name CATALINA_OPTS and value -Xms256m -Xmx512m of course you can change the 256 and the 512 to whatever values you want.
regarding the catalina.sh or catalina.bat you can find them on the bin directory in your tomcat cat folder.
if u have any other problems regarding this issue i’ll be glad to hear from you.
October 6, 2008 at 9:29 pm
I am using Tomcat as a web application server, on Linux and I have the following in my startup.sh for configuring the heap-size.
Is this wrong? I had found this on some web page but can’t locate that page now. I am wondering if the out of memory errors that I am getting is because the settings below are wrong.. If it is right, must I have the DXms setting too? If the settings below are OK, what does the D in the parameter denote?
JAVA_OPT=”-DXmn1536m -DXmx2560m”; export JAVA_OPT
Joe
October 7, 2008 at 11:02 am
Hello Joe,
the catalina start up script looks for environment variable JAVA_OPTS but you are using JAVA_OPT without the S at the end.
October 14, 2008 at 11:46 am
I have Windows XP and how can I set the memory heap size ?
Do you know the correct command to change heap size ?
I tried to use the following command but it did not work :
set JAVA_OPTS=”-Xms256m -Xmx512m”
October 14, 2008 at 1:39 pm
Dear Fatih,
for setting the JAVA_OPTS variable in windows you don’t need to type a command. all you need is to set the environment variable as following:
- right click on my computer icon and click select properties.
- select the advanced tab.
- click on environment variables button.
- on system variables part :
- click on new button.
- in the variable name type : JAVA_OPTS
- in the variable value type: -Xms256m -Xmx512m
- click ok and ok.
now you have the environment variable JAVA_OPTS set to -Xms256m -Xmx512m.
October 14, 2008 at 2:12 pm
Thanks Mostafa but I already know that way
But I need the correct cmd for my batch file
October 14, 2008 at 2:14 pm
what batch file you mean here?
October 14, 2008 at 2:23 pm
After installation of my program on my customer’s computer a batch file automatically runs at the end of the installation.
This batch file contains necessary commands to set the necessary parameters for my program.
And I should also add the “setting JAVA_OPTS environment variable” command to this batch file to make my program working better.
Because my program uses tomcat as the application server, I need to increase heap size automatically after the installation.
October 14, 2008 at 3:38 pm
Fatih,
i think this command can help you in this case : http://www.ss64.com/nt/setx.html
but i really recommend you to use linux on your customer platform instead of windows.
November 9, 2008 at 3:07 pm
Hi all,
I am new with Tomcat….
I have a question that I am facing every day.
I need to increase the tomcat memory Xms and Xmx … OS is windows .
Server Ram is 8G … I need to increase Xms to 1536m and Xmx to 4000m . because users need to view more than 18000 thumbnails in the page… and he system keeps giving me OutOfMemory Error … Plz … I need your help
November 9, 2008 at 3:58 pm
Dear Feras,
please read this thread from the beginning and you will find how to set the heap size for tomcat just replace the amount i used with the amounts you need.
also displaying 18000 thumbnails in one page is not a good idea at all you can use paging to minimize the amount of thumbnails you display in a single page.
i wish i helped you
and wait your comments.
January 17, 2009 at 7:08 pm
Hello,
I am getting OutofMemory error everyday and I need to restart the tomcat and thereafter it works fine. I have already set the JAVA_OPTS environment variable in WINDOWS System Variable specifying the maximum heap size of 1024.
But this option didnot work out and still the problem persist.
Can you please help me out from this problem.
Thanks,
Sandeep
January 17, 2009 at 7:35 pm
Hello Sandeep,
try to set both xmx and xms.
if it didn’t work, so u need to review your application because there is something wrong there. such as creating and loading recursive objects or so. if u like i may help u in that.
Regards
Mostafa
January 18, 2009 at 1:11 am
Hi Mostafa,
These are the options i tried
1) setting -Xms1024m -Xmx1024m -XX:MaxPermSize=128m as JAVA_OPTS. Should i try this same setting in catalina.bat file instead as system environment variable?
2) Introspector.flushCaches(); in the destroy() method of servelet.
Second option worked for a day. Server didn’t go down.But very next day it did. The weird thing is, site don’t go down on weekends. Let’s see for today as today is a Saturday.
What we have observed is, when the site is about to go down, java.exe usage remains high and after the tomcat restart it goes down. Does this say anything?
Thanks for your reply,
Sandeep
January 18, 2009 at 1:33 am
dear Sandeep,
remove the -xx from the java_opts just use -Xms1024m -Xmx1024m
second the reason for not being down in weekends this is normal as usually server hits is much less than normal in weekends.
we can communicate by email if u want more support. please send me your email.
Regards
Mostafa
January 18, 2009 at 1:52 am
Hi Mostafa,
I will try the option by removing XX.
Thanks,
Sandeep
January 18, 2009 at 1:57 am
Hi Mostafa
So now my JAVA_OPTS option looks like this
-Xms1024m -Xmx1024m :MaxPermSize=128m
I made a change now. Also i wrote an email address in the Mail field.
Thanks,
Sandeep
January 20, 2009 at 2:48 am
Hi Mostafa,
Making a JAVA_OPTS change in system environment variable didn’t work out. Does it make any difference if i make the same change in catalina.bat file?
Or can you suggest me some other option?
Awaiting for your reply,
Thanks,
Sandeep
January 20, 2009 at 3:13 am
Hello Sandeep,
first you need to be sure if the xmx and xms options really executed or not to do so.
in your application insert the following code:
System.out.prinln(“max memory: ” + Runtime.getRuntime().maxMemory() / (1024*1024));
System.out.prinln(“available memory: “+Runtime.getRuntime().freeMemory() / (1024*1024));
System.out.prinln(“total memory : “+ Runtime.getRuntime().totalMemory() / (1024*1024));
this will print on the console the needed information.
Regards
Mostafa
January 20, 2009 at 5:10 am
Hi Mostafa,
The output is coming is out be 128,1,1 for the above 3 sysouts.
Thanks,
Sandeep
January 20, 2009 at 5:31 am
Hi Sandeep,
from the the output i see that the xms and xmx didn’t apply for your tomcat.
try to add
SETX JAVA_OPTS=”-Xms256m -Xmx512m”
in your catalina.bat
and reply me with the output after that.
Regards
January 20, 2009 at 7:34 pm
Hi Mostafa,
Please ignore the above output. Sorry about that. I mistakenly tested with other tomcat.
Current situation:JAVA_OPTS environment variable is:-Xms512m -Xmx1024m
And the latest values are coming out to be:832,21,338 for the above mentioned sysout.
Thanks,
Sandeep
January 21, 2009 at 1:31 am
Hi Sandeep,
so the problem isn’t setting the heap size for the tomcat, it may be bad resource management in the application it self.
here is my email address if u want support: mostafa_hashem@hotmail.com
Regards
Mostafa
January 21, 2009 at 10:43 pm
Hi Mostafa,
Thanks for your support. I will contact you soon.
Thanks,
Sandeep
February 5, 2009 at 7:32 pm
Hi,
Mostafa
I am going with the same problem that sandeep having.I gone through your discussion.but still not able to solve the heap problem.
I have linux server with apache tomcat 5.5.25 install and the jdk is jdk1.6.0
Already done with setting CATALINA _OPTS and JAVA_OPTS in catalina.bat. I tested by the code
System.out.prinln(”max memory: ” + Runtime.getRuntime().maxMemory() / (1024*1024));
System.out.prinln(”available memory: “+Runtime.getRuntime().freeMemory() / (1024*1024));
System.out.prinln(”total memory : “+ Runtime.getRuntime().totalMemory() / (1024*1024));
still i m getting out put as 63 4 4
can you please refer to this?I am in big trouble please help me out of this.
Regards
Tushar
February 10, 2009 at 9:21 am
Hi mostafa
THanks for your reply ,I am giving “-Xms786m -Xmx786m” for CATALINA_OPTS as well as JAVA_OPTS if I go with command like
java -Xms786m -Xmx786m helloworld it works but i can’t go with that way as i have to set the heap size for entire webserver where many sites are hosted and each site can have many classes in it
regards
Tushar
February 17, 2009 at 6:46 am
;
April 10, 2009 at 9:05 am
THanks fantastic
April 11, 2009 at 3:08 am
Dear The YPI,
thanks for visiting my blog
April 15, 2009 at 10:20 am
Hi Mostafa,
I have WIN2K3 server and using tomcat6 as app server.
the conf or bin folder of tomcat doesn’t have any catalina.bat or there is nothing like JAVA_OPTS is declared any where can you guide me the way to resize the JVM heap other then system verables.
Thanks,Atul
April 28, 2009 at 6:18 am
Hi All,
What is the maximum memory that I can allocate to tomcat (i.e. for Xmx)? I am running tomcat in a 64bit linux machine with 16GB RAM and wondering if I can allocate around 3GB for tomcat.
June 9, 2009 at 8:37 pm
In windows, the easiest way to do it is to install the Tomcat Monitor.
Right click the icon in the Systray
Choose Configure
Click the Java Tab
Set the Initial and Maximum memory pool opttions
Click ok
Restart Tomcat
You can verify taht the settings are correct with a tool such as Lamba Probe
http://www.lambdaprobe.org
June 30, 2009 at 9:03 am
Hi Mostafa..
i’m using MacOSX.. and my applicaion is on a remote machine on the tomcat server.. i use JDBC to retrieve data.. when the result set has too many data then the heap size error is shown.. i have tried adding the ‘-export CATALINA_OPTS = -Xms256m -Xmx512m” in the catalina.sh and the startup.sh both the files. and when i start the server it starts but then shows an error saying ‘invalid command ‘export: = ‘
invalid command ‘export: -Xms256m’
invalid command ‘export: -Xms512m’
what do i do??? can i add the statement anywhere in the .sh file or some specific place only??
Thanks…
August 28, 2009 at 6:59 pm
In my catalina.sh file, I am using the following command:
JAVA_OPTS=”-Xmx1024m -Xms1024m”
I see where others are using “export” before the JAVA_OPTS. So I have two questions:
1. What is the difference between using “export” and not using “export”?
2. Does it matter?
Thanks!
September 9, 2009 at 4:17 pm
Hi,
i have tried all methods, but my heap size doesn’t go up.
My output is:
max memory: 63
available memory: 22
total memory : 63
I’m using windowXP, and Tomcat 5.5.15
Any help is appreciated.
September 10, 2009 at 3:33 pm
I solved the problem. In the section paparameters in tun configuration in eclipse you put the Xms Xmx you need.
September 16, 2009 at 8:09 pm
Hi all,
I suggest you to use Jconsole to monitor tomcat.
You need to set the memory size according to your traffic pattern. If you reserve high memory the gc will rarely and this will allow to tomcat to handle request more easy, but when gc will run it will take much time some you are using a large amount of memory.
Also, use the same value to Xms and Xmx and let the VM to adjust the MaxPermSize according to its needs.
Ramzi
October 27, 2009 at 5:39 pm
Thank you so much.your article is very useful.My issue got fixed