What are Xmx and Xms parameters in java

In this post, we will see about Xms and Xmx parameter in java.

-Xmx specifies maximum memory size for Java virtual machine (JVM), while -Xms specifies the initial memory size.

It means JVM will be started with Xms amount of memory and JVM will be able to use maximum of JVM amount of memory.

Let’s understand this with help of example.

java -Xms512m -Xmx1024m

So java process will start with 512 MB of memory heap and can use upto 1024 MB of memory heap.

-Xmx and -Xms can be defined in different size such as kilobytes, megabytes and gigabytes.

-Xmx2048k
-Xmx1024m
-Xmx2g

If Java process exceeds -Xmx memory size, then you will java.lang.OutOfMemoryError.

Default initial size is allocated on the based of ergonomics algorithm.

-Xms and -Xmx are the options for JVM’s heap and JVM can use more memory than size allocated to heap.

If you want to learn about all -X option, you can use java -X command.

Apples-MacBook-Pro-5:~ apple$ java -X-Xbatch
disable background compilation-Xbootclasspath/a:
append to end of bootstrap class path

-Xcheck:jni
perform additional checks for JNI functions

-Xcomp
forces compilation of methods on first invocation

-Xdebug
provided for backward compatibility

-Xdiag
show additional diagnostic messages

-Xfuture
enable strictest checks, anticipating future default

-Xint
interpreted mode execution only

-Xinternalversion
displays more detailed JVM version information than the
-version option

-Xloggc:<file>
log GC status to a file with time stamps

-Xmixed
mixed mode execution (default)

-Xmn<size>
sets the initial and maximum size (in bytes) of the heap
for the young generation (nursery)

-Xms<size>
set initial Java heap size

-Xmx<size>
set maximum Java heap size

-Xnoclassgc
disable class garbage collection

-Xrs
reduce use of OS signals by Java/VM (see documentation)

-Xshare:auto
use shared class data if possible (default)

-Xshare:off
do not attempt to use shared class data

-Xshare:on
require using shared class data, otherwise fail.

-XshowSettings
show all settings and continue

-XshowSettings:all
show all settings and continue

-XshowSettings:locale
show all locale related settings and continue

-XshowSettings:properties
show all property settings and continue

-XshowSettings:vm
show all vm related settings and continue

-XshowSettings:system
(Linux Only) show host system or container configuration and continue

-Xss<size>
set [java thread](https://java2blog.com/java-thread-example/ “java thread”) stack size

-Xverify
sets the mode of the bytecode verifier

–add-reads <module>=<target-module>(,<target-module>)*
updates <module> to read <target-module>, regardless
of module declaration.
<target-module> can be ALL-UNNAMED to read all unnamed
modules.

–add-exports <module>/<package>=<target-module>(,<target-module>)*
updates <module> to export <package> to <target-module>,
regardless of module declaration.
<target-module> can be ALL-UNNAMED to export to all
unnamed modules.

–add-opens <module>/<package>=<target-module>(,<target-module>)*
updates <module> to open <package> to
<target-module>, regardless of module declaration.

–illegal-access=<value>
permit or deny access to members of types in named modules
by code in unnamed modules.
<value> is one of “deny”, “permit”, “warn”, or “debug”
This option will be removed in a future release.

–limit-modules <module name>[,<module name>…]
limit the universe of observable modules

–patch-module <module>=<file>(:<file>)*
override or augment a module with classes and resources
in JAR files or directories.

–disable-@files
disable further argument file expansion

–source <version>
set the version of the source in source-file mode.

These extra options are subject to change without notice.

The following options are Mac OS X specific:
-XstartOnFirstThread
run the main() method on the first (AppKit) thread

-Xdock:name=<application name>
override default application name displayed in dock

-Xdock:icon=<path to icon file>
override default icon displayed in dock

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

Please note that it is quite important to set -Xms and -Xmx parameters correctly. Java 13 has introduced a new feature in Z garbage collector where it will uncommit the memory to CPU but it won’t uncommit below -Xms parameter, so if you put -Xms and -Xmx as equal, it will basically disable this feature. This feature is quite useful where memory footprint is a concern.

That’s all about Java’s -Xmx and -Xms parameters.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *