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.

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)
- Search Start for Edit the system environment variables and open it.
- Click Environment Variables.
- Under User variables, click New.
- Name:
JAVA_TOOL_OPTIONS. Value:-Xmx4g. - 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
-Xmxhigher 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 -versionand 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
- Check the current limit with
-XX:+PrintFlagsFinal. - Add
-Xmx4g(or your value) when starting the program. - Use
JAVA_TOOL_OPTIONSto apply it to all Java programs. - Set it in your IDE, build tool, server, or launcher where applicable.

Kermit Matthews is a freelance writer based in Philadelphia, Pennsylvania with more than a decade of experience writing technology guides. He has a Bachelor’s and Master’s degree in Computer Science and has spent much of his professional career in IT management.
He specializes in writing content about iPhones, Android devices, Microsoft Office, and many other popular applications and devices.