Setting the JVM memory alocation for tomcat should be trivial in theory we only need to pass the : -Xmx1024m parameter to JAVA_OPTS during tomcat startup. However that not the case. I’ve been looking for month how to change this Tomcat JVM paramater. Finally i got the answer tonight and i want to share my experience 🙂
Note : This was intended to use with the tomcat that come with ubuntu bundle (apt-get install tomcat)
In Debian the JAVA_OPTS paramater we need to change is located in this file /etc/default/tomcatX.X.
Change the JAVA_OPTS with this parameter :
JAVA_OPTS=”-Djava.awt.headless=true -Xmx1024m -XX:MaxPermSize=256m”
-Xmx : Set the maximal heap memory alocated for a tomcat server, a low value of this parameter can lead to OutOfMemoryExceptions.
-XX:MaxPermSize : The PermGen space is used for things that do not change (or change often). e.g. Java classes
The thing that we must be aware of is that both -Xmx and -XX:MaxPermSize takes different memory alocation. So if in the example we put 1024 Mb on Heap space and 256 Mb on PermGen we allowed a total of 1024 + 256 to be alocated on a single tomcat Instance.
After change these setting we can restart tomcat.
If we want to make sure that the setting is taking effect , we can make a simple servlet which check the memory parameter using this code :
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Runtime
runtime
=
Runtime
.
getRuntime
(
)
;
NumberFormat
format
=
NumberFormat
.
getInstance
(
)
;
long
maxMemory
=
runtime
.
maxMemory
(
)
;
long
allocatedMemory
=
runtime
.
totalMemory
(
)
;
long
freeMemory
=
runtime
.
freeMemory
(
)
;
Writer
wr
=
resp
.
getWriter
(
)
;
StringBuilder
sb
=
new
StringBuilder
(
)
;
sb
.
append
(
"<h1>Server Info </h1>"
)
;
sb
.
append
(
"<br><b>Free memory: </b>"
+
format
.
format
(
freeMemory
/
1024
)
+
" MB "
)
;
sb
.
append
(
"<br><b>Allocated memory : </b>"
+
format
.
format
(
allocatedMemory
/
1024
)
+
" MB "
)
;
sb
.
append
(
"<br><b>Max memory : </b>"
+
format
.
format
(
maxMemory
/
1024
)
+
"MB "
)
;
sb
.
append
(
"<br><b>Total free memory : </b> "
+
format
.
format
(
(
freeMemory
+
(
maxMemory
-
allocatedMemory
)
)
/
1024
)
+
" MB "
)
;
wr
.
write
(
sb
.
toString
(
)
)
;
|
Hope thats help !
Reference :
http://rimuhosting.com/knowledgebase/linux/java/-Xmx-settings
http://codintips.blogspot.com.au/2010/01/add-more-jvm-memory-in-tomcat-on-ubuntu.html