Search code examples
javajvmjvm-arguments

Starting memory allocation for JVM


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


Solution

  • 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 an OutOfMemoryException or very poor performance if your program's heap memory is reaching the maximum heap size. If your program is running on a dedicated server you can set this parameter higher because it wont affect other programs.

    The -Xms argument sets the initial and minimum 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 needing to regularly increase the heap size and so you 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.