JVM heap parameters
Asked Answered
I

6

67

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?

In answered 8/7, 2009 at 14:33 Comment(0)
C
34

To summarize the information found after the link: The JVM allocates the amount specified by -Xms but the OS usually does not allocate real pages until they are needed. So the JVM allocates virtual memory as specified by Xms but only allocates physical memory as is needed.

You can see this by using Process Explorer by Sysinternals instead of task manager on windows.

So there is a real difference between using -Xms64M and -Xms512M. But I think the most important difference is the one you already pointed out: the garbage collector will run more often if you really need the 512MB but only started with 64MB.

Cagliostro answered 8/7, 2009 at 14:45 Comment(5)
I think the real syntax is -Xms64m and not -Xms=64m. If I am mistaken please roll back!Styles
Came across this when researching some of the finer points of jvm config. The accepted answer here is very different from my understanding of these values. Yes, most OS will only make physical pages availabe when a minor page fault is tripped as the app tries to access memory it has malloc'ed - but Xms specifies the initial amount of memory malloced by the jvm - which places an upper bound on the number of pages which can be assigned without the app malloc'ing more memory - and in the case of Java, it wil only malloc more memory (up to Xmx) if insufficient memory is made available by gcSlurp
@Slurp I am confused. It seems your understanding is not really different from what is in the answer. You are just using different words, but are describing the exact same behavior.Cagliostro
The upper cap on memory usage is set by Xmx, not Xms.Slurp
@Slurp the question was about different behavior of the different Xms values and why the OS doesn't show a difference. As per my answer (and confirmed in your comment) the OS only allocates the physical memory that is needed and not the whole memory the application allocated initially. The fact that Java will allocate more memory until the value in Xmx is reached was not at all questioned.Cagliostro
T
38

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.

Tuneless answered 8/7, 2009 at 14:40 Comment(5)
I know that. But as said in my previous comment what "Java will start with memory usage at the initial heap level" exactly means ? This memory seems not effectively allocated since my 512m bytes system is able to launch several other applications without swaping and reprot me that only few megabytes are used by my java application.In
Typically you would set the initial heap size to a larger value if you knew that an app on start up would consume a particular amount of memory. This prevents the JVM from having to take execution time on start up to resize the heap a number of times. That particular use is a very common optimization to make eclipse run faster by avoiding the costly allocation cycle.Griffiths
Thanks for this answer that leads me to this other question: Is there any "risk" or drawback to setting the Xms value to the same value than Xmx ?In
As I understand the only risk is that you run out of virtual memory because you allocate too much virtual memory - but if your application really needs the memory that will happen anyway.Cagliostro
I think the real syntax is -Xms64m and not -Xms=64m. If I am mistaken please roll back!Styles
C
34

To summarize the information found after the link: The JVM allocates the amount specified by -Xms but the OS usually does not allocate real pages until they are needed. So the JVM allocates virtual memory as specified by Xms but only allocates physical memory as is needed.

You can see this by using Process Explorer by Sysinternals instead of task manager on windows.

So there is a real difference between using -Xms64M and -Xms512M. But I think the most important difference is the one you already pointed out: the garbage collector will run more often if you really need the 512MB but only started with 64MB.

Cagliostro answered 8/7, 2009 at 14:45 Comment(5)
I think the real syntax is -Xms64m and not -Xms=64m. If I am mistaken please roll back!Styles
Came across this when researching some of the finer points of jvm config. The accepted answer here is very different from my understanding of these values. Yes, most OS will only make physical pages availabe when a minor page fault is tripped as the app tries to access memory it has malloc'ed - but Xms specifies the initial amount of memory malloced by the jvm - which places an upper bound on the number of pages which can be assigned without the app malloc'ing more memory - and in the case of Java, it wil only malloc more memory (up to Xmx) if insufficient memory is made available by gcSlurp
@Slurp I am confused. It seems your understanding is not really different from what is in the answer. You are just using different words, but are describing the exact same behavior.Cagliostro
The upper cap on memory usage is set by Xmx, not Xms.Slurp
@Slurp the question was about different behavior of the different Xms values and why the OS doesn't show a difference. As per my answer (and confirmed in your comment) the OS only allocates the physical memory that is needed and not the whole memory the application allocated initially. The fact that Java will allocate more memory until the value in Xmx is reached was not at all questioned.Cagliostro
S
15

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.

Shinberg answered 20/11, 2011 at 3:50 Comment(0)
S
5

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.

Selfgoverned answered 8/7, 2009 at 14:43 Comment(0)
S
4

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.

Solvency answered 8/7, 2009 at 14:38 Comment(5)
I know that. But what "Java allocate in those moment 512m" exactly means ? This memory seems not effectively allocated since my 512m bytes system is able to launch several other applications without swaping and reprot me that only few megabytes are used by my java application..In
Why was this downvoted? It is technically correct except for the last sentence which may or may not be correct. In a high performance world it is usually smarter to have java allocate all memory it will need with one single brk(). In other situations, allocating memory as you use it might be smarter.Shiri
I didn't downvoted this answer myself. Regarding your comment I have the same question than the one I asked to dan: Is there any "risk" or drawback to setting the Xms value to the same value than Xmx ? Thanks ManuIn
Setting them to the same value is the recommended way for optimal performance. The drawback is that you need to take a bit more extra care to not have them set too high as doing so may have an impact on the overall performance of the system.Shiri
I think the real syntax is -Xms64m and not -Xms=64m. If I am mistaken please roll back!Styles
L
0

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.

Lepidopteran answered 11/1, 2017 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.