[java] JVM heap parameters

After reading already asked question on the subject and a lot of googling I am still not able to have a clear view of -Xms option

My question is: what's the difference between java -Xms=512m -Xmx=512m and java -Xms=64m -Xmx=512m?

For now I have the following answer:

The only difference is in the number of garbage collections that will be run during my application's run and the number of memory allocations. Am I right ?

Here are my reasons for this answer:

Setting the -Xms option to 512m doesn't result in my application using really 512M of physical memory after startup. I guess this is related to modern OS virtual memory management and lazy pages allocations. (I noticed that setting -Xms to 512M or to 64M doesn't change at all the initial used memory reported either by top on Linux or by the task manager on windows)

Can someone help me to understand the impact of this Xms option or point me to links that will help me to understand it?

Thanks in advance

Manu

This question is related to java jvm-arguments

The answer is


The JVM resizes the heap adaptively, meaning it will attempt to find the best heap size for your application. -Xms and -Xmx simply specifies the range in which the JVM can operate and resize the heap. If -Xms and -Xmx are the same value, then the JVM's heap size will stay constant at that value.

It's typically best to just set -Xmx and let the JVM find the best heap size, unless there's a specific reason why you need to give the JVM a big heap at JVM launch.

As far as when the JVM actually requests the memory from the OS, I believe it depends on the platform and implementation of the JVM. I imagine that it wouldn't request the memory until your app actually needs it. -Xmx and -Xms just reserves the memory.


I created this toy example in scala, my_file.scala:

object MyObject {

    def main(args: Array[String]) {
        var ab = ArrayBuffer.empty[Int]

        for (i <- 0 to 100 * 1000 * 1000) {
            ab += i
            if (i % 10000 == 0) {
                println("On : %s".format(i))
            }
        }
    }
}

I ran it with:

scala -J-Xms500m -J-Xmx7g my_file.scala

and

scala -J-Xms7g -J-Xmx7g my_file.scala

There are certainly noticeable pauses in -Xms500m version. I am positive that the short pauses are garbage collection runs, and the long ones are heap allocations.


Apart from standard Heap parameters -Xms and -Xmx it's also good to know -XX:PermSize and -XX:MaxPermSize, which is used to specify size of Perm Gen space because even though you could have space in other generation in heap you can run out of memory if your perm gen space gets full. This link also has nice overview of some important JVM parameters.


The JVM will start with memory useage at the initial heap level. If the maxheap is higher, it will grow to the maxheap size as memory requirements exceed it's current memory.

So,

  • -Xms512m -Xmx512m

JVM starts with 512 M, never resizes.

  • -Xms64m -Xmx512m

JVM starts with 64M, grows (up to max ceiling of 512) if mem. requirements exceed 64.


if you wrote: -Xms512m -Xmx512m when it start, java allocate in those moment 512m of ram for his process and cant increment.

-Xms64m -Xmx512m when it start, java allocate only 64m of ram for his process, but java can be increment his memory occupation while 512m.

I think that second thing is better because you give to java the automatic memory management.