Which JVM command-line option is used to set the maximum heap size?

The story behind the answer

The JVM command-line option used to set the maximum heap size is -Xmx.

The option limits how much memory the Java heap can grow to, with values commonly written using units such as m for megabytes or g for gigabytes. For example, -Xmx2g permits a maximum heap size of 2 gigabytes. The equivalent HotSpot form is -XX:MaxHeapSize.

-Xmx is often confused with -Xms, but they control different limits: -Xms sets the initial heap size, while -Xmx sets the upper bound. A JVM may begin with a smaller heap and expand it as an application allocates objects, up to the -Xmx ceiling.

Heap size is only one part of a Java process's memory use. Metaspace, thread stacks, native libraries, direct buffers, and the operating system itself also consume memory. Setting -Xmx too high can leave too little memory for those areas; setting it too low can cause frequent garbage collection or an OutOfMemoryError.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: