1181

Please explain the use of Xms and Xmx parameters in JVMs. What are the default values for them?

Hearen
  • 6,019
  • 2
  • 36
  • 50
Pankaj
  • 12,220
  • 3
  • 15
  • 22
  • 2
    when using `-Xmx128m -Xms64m` it can peak around `275m` RES mem, but when using `-Xmx128m -Xms128m` it can peak around `550m` RES mem Using Java 8 Best thing is to stress the GC and look at what happens ... – Christophe Roussy Feb 20 '18 at 16:03
  • Just being curious, the 275m and 550m - How were they calculated? – Manuel Jordan Jan 14 '21 at 12:38

6 Answers6

1557

The flag Xmx specifies the maximum memory allocation pool for a Java Virtual Machine (JVM), while Xms specifies the initial memory allocation pool.

This means that your JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory. For example, starting a JVM like below will start it with 256 MB of memory and will allow the process to use up to 2048 MB of memory:

java -Xms256m -Xmx2048m

The memory flag can also be specified in different sizes, such as kilobytes, megabytes, and so on.

-Xmx1024k
-Xmx512m
-Xmx8g

The Xms flag has no default value, and Xmx typically has a default value of 256 MB. A common use for these flags is when you encounter a java.lang.OutOfMemoryError.

When using these settings, keep in mind that these settings are for the JVM's heap, and that the JVM can and will use more memory than just the size allocated to the heap. From Oracle's documentation:

Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.

Dónal Boyle
  • 2,909
  • 2
  • 21
  • 38
hexacyanide
  • 76,426
  • 29
  • 148
  • 154
  • 14
    So is it like when memory usage exceeds beyond Xmx we get jvm out of memory exception. – Pankaj Feb 07 '13 at 23:41
  • 66
    Yes, that's correct. When it tries to exceed that, although it *may* collect garbage to try to free up enough memory. If there still isn't enough memory to satisfy the request and the heap has already reached the maximum size, an `OutOfMemoryError` will occur. – David Conrad Oct 10 '13 at 17:38
  • 2
    When I'm using the ForkJoin framework my computer crashes because it's taking up too much memory. Is it possible that on OpenJDK there is no hard limit on the memory by default? – Christophe De Troyer May 26 '14 at 13:33
  • 8
    As clarified at [Does java -Xmx 1G mean 1 GB or 2^30 B?](http://stackoverflow.com/questions/32855984/does-java-xmx-1g-mean-1-gb-or-230-b/32858015#32858015), the unambiguous way to express how much memory you start with via `-Xms256m` is "256 MiB", not "256 MB", since it goes by binary powers rather than powers of ten. See https://en.wikipedia.org/wiki/Binary_prefix. In addition, because some unusable space is set aside for an extra pool of Survivor space, the amount of memory actually available according to `Runtime.getRuntime().maxMemory()` is less than the value specified via -Xmx – nealmcb Sep 30 '15 at 05:20
  • 2
    A useful guide of common errors relating to heap size: http://blog.paulgu.com/java/6-common-errors-in-setting-java-heap-size/ – ctrlplusb Dec 15 '15 at 15:32
  • -Xmx8g what does g stand for ? – Arun Bertil Jun 13 '16 at 11:54
  • "Xmx typically has a default value of 256MB" - very unlikely, that would crash most of the apps - See this: http://stackoverflow.com/questions/4667483/how-is-the-default-java-heap-size-determined – Jack Apr 10 '17 at 08:10
  • Why I set xmx=xms=8g, my java process does not consume 8g but a much smaller memory when it starts, which I observe from top -->RES. Shouldn't it be allocated by at least the xms memory? – JaskeyLam Oct 17 '17 at 03:47
  • @Jack I think the 256MB was the default value for Java 6 or 7. Java 8+ uses different default values. – Robert Oct 27 '17 at 10:46
  • small precision, it's about the heap (not the stack, not metaspace) – Lho Ben Dec 14 '19 at 21:51
  • @David Conrad, I tried the same approach and added Xmx 1GB. Now, I have another two OutOfMemory errors. `Java.lang.OutOfMemoryError: Java heap space` and `Java.lang.OutOfMemoryError: GC overhead limit exceeded`. Is this related to Xmx? which I updated to 1GB. I tried with Xmx512 as well but still, it has the same error. Once I start a server after period of time it shows the same error again. – Harshad Chhaiya Feb 22 '20 at 08:13
  • @ChhaiyaHarshad You should submit a question with details of what your application is doing and the exact command used to start it. It sounds like you have a memory leak. – David Conrad Feb 22 '20 at 17:40
  • 1
    This information is no longer up to date: "The Xms flag has no default value, and Xmx typically has a default value of 256 MB". It depends if Java considers the machine to be a client or server, and for servers, Xmx can be up to 32GiB. Take a look at https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/ergonomics.html and https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/parallel.html#default_heap_size – Daniel Jun 18 '20 at 19:35
350

Run the command java -X and you will get a list of all -X options:

C:\Users\Admin>java -X
-Xmixed           mixed mode execution (default)
-Xint             interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by ;>
                      set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by ;>
                      append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by ;>
                      prepend in front of bootstrap class path
-Xdiag            show additional diagnostic messages
-Xnoclassgc       disable class garbage collection
-Xincgc           enable incremental garbage collection
-Xloggc:<file>    log GC status to a file with time stamps
-Xbatch           disable background compilation
-Xms<size>        set initial Java heap size.........................
-Xmx<size>        set maximum Java heap size.........................
-Xss<size>        set java thread stack size
-Xprof            output cpu profiling data
-Xfuture          enable strictest checks, anticipating future default
-Xrs              reduce use of OS signals by Java/VM (see documentation)
-Xcheck:jni       perform additional checks for JNI functions
-Xshare:off       do not attempt to use shared class data
-Xshare:auto      use shared class data if possible (default)
-Xshare:on        require using shared class data, otherwise fail.
-XshowSettings    show all settings and continue
-XshowSettings:all         show all settings and continue
-XshowSettings:vm          show all vm related settings and continue
-XshowSettings:properties  show all property settings and continue
-XshowSettings:locale      show all locale related settings and continue

The -X options are non-standard and subject to change without notice.

I hope this will help you understand Xms, Xmx as well as many other things that matters the most. :)

Hearen
  • 6,019
  • 2
  • 36
  • 50
Rupesh Yadav
  • 14,336
  • 5
  • 53
  • 68
19

You can specify it in your IDE. For example, for Eclipse in Run ConfigurationsVM arguments. You can enter -Xmx800m -Xms500m as

Enter image description here

Hearen
  • 6,019
  • 2
  • 36
  • 50
14

The question itself has already been addressed above. Just adding part of the default values.

As per http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html

The default value of Xmx will depend on platform and amount of memory available in the system.

Hearen
  • 6,019
  • 2
  • 36
  • 50
Abhishek Aich
  • 141
  • 1
  • 2
  • 8
    For other JVMs (besides JRockit), see this post to determine default Xmx/Xms values of your JVM: http://stackoverflow.com/questions/4667483/how-is-the-default-java-heap-size-determined – Ogre Psalm33 Aug 21 '14 at 14:13
12

-Xms initial heap size for the startup, however, during the working process the heap size can be less than -Xms due to users' inactivity or GC iterations. This is not a minimal required heap size.

-Xmx maximal heap size

lazylead
  • 346
  • 3
  • 9
  • Could you please give more details about "due to users' inactivity or GC iterations"? And what is the difference between initial and minimal heap sizes then? – Tony Jan 24 '21 at 07:18
0

very simple

Xss used to set stack memory size

Xms startup heap size

Xmx Maximum heap size if not set 256MB used

Nagappa L M
  • 1,240
  • 3
  • 16
  • 28