How to Increase Java Heap Size in Windows 10 (-Xmx Guide)

When a Java program shows “java.lang.OutOfMemoryError: Java heap space,” it has used all the memory the Java Virtual Machine (JVM) was allowed. The fix is to raise the maximum heap size with the -Xmx option. How you pass that option depends on how the program starts: from the command line, an IDE, a build tool, a game launcher, or an environment variable. This guide covers each case on Windows 10 (and 11), how to check the current limit, and how much memory to allow.

Quick Answer

Start the program with -Xmx followed by the size, for example java -Xmx4g -jar yourapp.jar for 4 GB. Optionally add -Xms1g to set the starting heap. To apply a setting to every Java program, create a Windows environment variable named JAVA_TOOL_OPTIONS with the value -Xmx4g.

Command Prompt showing the default Java max heap size check and a program started with -Xms and -Xmx heap size options
Check the current maximum heap (1), then start the program with -Xms and -Xmx (2).

Heap Size Options

Option Meaning Example
-Xmx Maximum heap size -Xmx4g (4 GB) or -Xmx2048m (2048 MB)
-Xms Initial heap size -Xms512m
-XX:MaxRAMPercentage Maximum heap as a percentage of RAM -XX:MaxRAMPercentage=50

Use g for gigabytes and m for megabytes, with no space between the number and the letter.

Check the Current Maximum Heap

Open Command Prompt and run:

java -XX:+PrintFlagsFinal -version | findstr /i "MaxHeapSize"

The value is in bytes (4278190080 is about 4 GB). By default, recent Java versions allow up to about a quarter of your RAM.

Method 1: Command Line

java -Xms1g -Xmx8g -jar yourapp.jar

Put the options before -jar or the main class name. For a program started from a .bat file, edit the java line inside it.

Method 2: Environment Variable (All Java Programs)

  1. Search Start for Edit the system environment variables and open it.
  2. Click Environment Variables.
  3. Under User variables, click New.
  4. Name: JAVA_TOOL_OPTIONS. Value: -Xmx4g.
  5. Click OK, then open a new Command Prompt window. Java prints “Picked up JAVA_TOOL_OPTIONS” to confirm.

A program’s own -Xmx setting usually overrides this.

Method 3: IDEs, Build Tools, and Servers

Tool Where to set it
IntelliJ IDEA (the IDE itself) Help > Change Memory Settings
IntelliJ (your app) Run > Edit Configurations > Modify options > Add VM options > -Xmx4g
Eclipse Edit eclipse.ini and change the -Xmx line below -vmargs. For your app, use Run Configurations > Arguments > VM arguments.
Gradle org.gradle.jvmargs=-Xmx4g in gradle.properties
Maven Set the MAVEN_OPTS environment variable to -Xmx4g
Apache Tomcat Create bin\setenv.bat with set CATALINA_OPTS=-Xmx4g
Minecraft Java Edition Launcher > Installations > Edit > More options > JVM arguments, change -Xmx2G

How Much Heap Should You Allow?

  • Leave enough RAM for Windows and other apps. On a 16 GB PC, 4 to 8 GB is usually the most you’d give one Java app.
  • Setting -Xmx higher than needed doesn’t make programs faster, and can cause longer garbage collection pauses.
  • 32-bit Java is limited to roughly 1.5 GB of heap. Install 64-bit Java to use more. Check with java -version and look for “64-Bit.”

Troubleshooting

“Invalid maximum heap size” or “Could not reserve enough space for object heap”

The value is too large for your RAM, or you’re on 32-bit Java. Lower the value or install 64-bit Java.

The setting has no effect

The program’s launcher may pass its own -Xmx. Set it in the launcher, script, or IDE configuration instead.

Still getting OutOfMemoryError

The program may have a memory leak or genuinely need more data in memory. Monitor it with VisualVM or JDK Mission Control.

Related: Downgrade Java on Windows 10.

Frequently Asked Questions

What’s the difference between -Xms and -Xmx?

-Xms is the heap size at startup. -Xmx is the maximum it can grow to.

Does the Java Control Panel change heap size?

Its Runtime Parameters only affected browser applets and Java Web Start, which are obsolete. Use the methods above.

Summary

  1. Check the current limit with -XX:+PrintFlagsFinal.
  2. Add -Xmx4g (or your value) when starting the program.
  3. Use JAVA_TOOL_OPTIONS to apply it to all Java programs.
  4. Set it in your IDE, build tool, server, or launcher where applicable.