viernes, enero 27, 2006

Tomcat Memory Allocation

Well, in fact, change the memory settings in tomcat is so easy. Just you have to change catalina file in the $CATALINA_HOME/bin directory.

Just add for Linux the next two lines to catalina.sh:


# ----- Change memory settings ----------------------
JAVA_OPTS="$JAVA_OPTS "-Xmx30M" "-Xms38M


And for Windows the next two lines to catalina.bat:


rem ----- Change memory settings ----------------------------------------------
set JAVA_OPTS=%JAVA_OPTS% -Xmx30M -Xms38M


That line adds to JAVA_OPTS two parameters more, one for stablish the start memory for tomcat (with -Xms parameter) and the other stablish the maximun number of MB to assing to tomcat (with -Xmx parameter). The next value for the parameters is the number of MB to be assigned. Your must to change that as you prefer.

NOTE: If you use tomcat.exe for start it, then you have to give the parameters of memory in the command line.

For getting more information about how you can add other non standar parameters for java just type java -X in your console.

I was a little of fun trying to prove the memory assigment for java with the next code:


public class arreglo{
public static void main(String[] args) {
if(args.length>0) {
try {
int size = Integer.parseInt(args[0]);
String[] arr = new String[size];
for(int i=0; i < size; i++)
arr[i] = "tipo="+i;
} catch (Exception ouch){}
} else {
String[] arr = new String[500];
for(int i=0; i < 500; i++)
arr[i] = "tipo="+i;
}
}
}


That code has some funny results:


grecia@adso:~/pruebas$ java -Xmx1M arreglo 10000
grecia@adso:~/pruebas$ java -Xmx1M arreglo 100000
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
grecia@adso:~/pruebas$ java -Xmx2M arreglo 100000
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
grecia@adso:~/pruebas$ java -Xmx10M arreglo 100000

http://www.blogger.com/img/gl.link.gif

For more information about this topics please refer to Tomcat FAQ memory.