I'm beginning use the -Xmx
option on the java
command to allow my processes to use a little more memory (256Mb, though I think I'm currently using less than 128Mb). I've also noticed the -Xms
option for starting memory, with a default value of 2Mb. What should I set this value to and why?
views:
240answers:
3Xmx is the upper bound of the memory pool. Xms is the initial size. That's all there's to it. The appropriate size for each would depend on the complexity of your application. The main advantage of setting the initial size higher than 2mb would just be that the JVM would spend less time requesting more memory from the OS. If you've set an upper limit of 256mb, an initial size of 32mb-64mb wouldn't be unreasonable for most enterprise applications.
More here: http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html
The -Xmx argument defines the max memory size that the heap can reach for the JVM. You must know your program well and see how it performs under load and set this parameter accordingly. A low value can cause OutOfMemoryExceptions or a very poor performance if your program's heap memory is reaching the maximum heap size. If your program is running in dedicated server you can set this parameter higher because it wont affect other programs.
The -Xms argument sets the initial heap memory size for the JVM. This means that when you start your program the JVM will allocate this amount of memory instantly. This is useful if your program will consume a large amount of heap memory right from the start. This avoids the JVM to be constantly increasing the heap and can gain some performance there. If you don't know if this parameter is going to help you, don't use it.
It is good practice with server-side Java applications like Resin to set the minimum -Xms and maximum -Xmx heap sizes to the same value. You can set to 256 or 512Mb.
What should I set this value to and why?
Depends on your application of course.
If you know your application initially consumes up to, let's say 64 mb in the first minute, then using the default ( 2mb), will make the VM request memory several times until it reach this 64mb. These memory requests will slow down a little bit your application, because the garbage collector may run a number of times trying to free space before the VM ask for more.
If you know already that you'll use as start 64 mb, using -Xms
parameter will let you allocate that memory upfront.
If you're consuming 128mb and you have enough memory available you may use java -Xms128m
and don't worry about requesting more memory for a long time.
But again, it depends on what your application does, how is the memory used, when is needed. etc. etc.