views:

240

answers:

3

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?


Reference: Java

A: 

Xmx 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

Mike Thomsen
+3  A: 

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.

harigm
My practice is that when I find I have to raise mx for some reason, I make ms match it since it IS hitting that limit (or you wouldn't have had to raise mx). I'm not sure if this matters, but why not have it allocate it all up front if you know you are going to need it? If you don't need the high mx for every run though you might leave ms low.
Bill K
@Bill yeap, nowadays RAM is a something you can just throw in into a machine. This was more for a time when the app needed to play nice with the rest. If you have the RAM you can give it to java upfront.
OscarRyz
Well, my thought was that under the assumption that you need the ram and have to use your -mx variable anyway, then it follows that you have the ram to use -ms and will eventually need it so where is the negative to setting it up front?
Bill K
@Bill. earlier Days we require to worry about this factor
harigm
+1  A: 

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.

OscarRyz