Profile Image

What is Maximum Heap Size: -Xmx?

What is Maximum Heap Size: -Xmx? Have you used this JVM argument before? What are the pros & cons of using this Java argument? Can you share your perspective/experience in using this JVM argument?

  • jvm-argument

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky


-XmsNNN where NNN is a number indicating the amount of memory, including
an optional character indicating the unit. For example -Xms256m indicates a
maximum heap size of 256 megabytes.

The units can be specified as:
• k, K – kilobytes
• m, M – megabytes
• g, G – gigibytes
• t, T – terabytes

WARNING: If a unit is not specified then the number indicates an exact
number of bytes, e.g. -Xms64 means just 64 bytes.


Set the inital heap to 256 megabytes:
java -Xms256m
Set the initial heap to 16 gigabytes:
java -Xms16g


The -Xms argument controls the initial and minimum size of the Java heap. This
is the amount of memory that is immediately reserved for the heap by the JVM
at start-up, and the size below which the heap will not be allowed to drop. Note
that this is not the minimum total size of the JVM process in memory because
of the other types of memory mentioned above (stack, JVM internals, etc.).

Most applications do not need to specify -Xms and will work fine without it.

However, if you know that your application will need at least a certain amount
of heap immediately after starting, it may be beneficial to reserve at least that
much at start time. If this is not done then the JVM will have to incrementally
request more memory from the operating system each time the heap needs to
grow, which can be slower in aggregate than requesting all of the memory at

For this reason it is a generally recommended to set -Xms to the application’s
stable heap usage level, based on historical measurements of the application in
production usage.


Default Value:

The -Xms parameter does not have a fixed default value. If not specified then the
value is chosen at runtime appropriately by the JVM, depending on the amount
of physical memory available, the CPU architecture, the JVM mode (i.e. server
or client), and the JVM vendor, implementation and version.

To determine the default settings of a JVM instance, the Print Flags option can
be used. It will show the initial heap size (-Xms):

java -XX:+PrintCommandLineFlags -version

Example output of this command on a device with 16GB of RAM:

-XX:ConcGCThreads=2 -XX:G1ConcRefinementThreads=8 -XX:GCDrainStackTargetSize=64 -XX:InitialHeapSize=268435456 -XX:MarkStackSize=4194304 -XX:MaxHeapSize=4294967296 -XX:MinHeapSize=6815736 -XX:+PrintCommandLineFlags -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)
OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode)

This shows the initial heap size as 268435456 bytes, or 256MB


The value of -Xms cannot be higher than -Xmx (maximum heap size). The
following error results if this is tried:

Error occurred during initialization of VM
Initial heap size set to a larger value than the maximum heap size

The value of -Xms cannot be higher than the total available physical memory in
the device. The following error results when setting -Xms512T (i.e. 512 terabytes)
on a device with only 16Gb RAM:

Error occurred during initialization of VM
Could not reserve enough space for 549755813888KB object heap


Arguments Related to -Xmx:


TODO: link to -Xms

Related Posts:

• To learn about different memory regions in the Java Virtual Machine, see
this video clip.
• IBM documentation regarding -Xmx.
• Azul documentation: Recommended Heap Size.
• Benefits of setting initial and maximum heap size to the same value.


If you have additional comments, interesting experiences or even point of disagreement with this JVM argument description, please leave a comment. Your
insights will help the entire 10+ million java developer community to develop
one standard source of documentation for all the JVM arguments.

Got something else on mind? Post Your Question

Not the answer you're looking for? Browse other questions tagged
  • jvm-argument