When you are using JVM in 32-bit mode, the maximum heap size that can be allocated is 1280 MB. So, if you want to go beyond that, you need to invoke JVM in 64-mode.

You can use following:

$ java -d64 -Xms512m -Xmx4g HelloWorld

where,

  • -d64: Will enable 64-bit JVM
  • -Xms512m: Will set initial heap size as 512 MB
  • -Xmx4g: Will set maximum heap size as 4 GB

You can tune in -Xms and -Xmx as per you requirements (YMMV)

A very good resource on JVM performance tuning, which might want to look into: http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html

Answer from mohitsoni on Stack Overflow
🌐
Medium
medium.com › @maheshwar.ramkrushna › understanding-heap-size-and-its-impact-on-java-application-performance-d4c312bbd13c
Understanding Heap Size and its Impact on Java Application Performance | by Ramkrushna Maheshwar | Medium
May 25, 2023 - We recommend increasing the maximum heap allocation to 512 MB or 1024 MB when dealing with discovery ranges equivalent to a class B subnet, or in excess of 30,000 addressable devices.
Discussions

java - how to choose the jvm heap size? - Stack Overflow
What i usually do concerning the jvm heap size is setting the max value really high to avoid the infamous OutOfMemoryException. However, this strategy (or lack of strategy) doesn't seem to be really More on stackoverflow.com
🌐 stackoverflow.com
Essential JVM Heap Settings: What Every Java Developer Should Know
An upcoming JEP intends to make heap sizing a thing of the past, first for ZGC, and later maybe for other collectors, too: Automatic Heap Sizing for ZGC . It didn't make JDK 25, but fingers crossed for JDK 26. More on reddit.com
🌐 r/java
22
133
August 4, 2025
Strategies for predicting JVM heap dump size
Don't you set a memory limit on your containers? That would be an upper limit. Then it depends on the flags you're using to run java. More on reddit.com
🌐 r/java
11
14
October 4, 2024
Large or small heap size
Couple of thoughts here. One missing aspect for the "why" of a large heap is total time garbage collecting. One of the key advantages of a huge heap is that overall, you'll spend less time in GC. This is because by the time you've allocated enough to trigger a GC, you are more likely to have a bunch of dead objects which, in turn means they don't go through the GC cycle multiple times (less objects promoted to old gen). If you have an application that is focused on throughput, then a large heap is the way to go. The presenter also mentions various GCs, but fails to mention the fact that all of the newer GCs in the JDK are focused on being able to handle large heaps. G1, ZGC, etc. They are all more than capable of handling super large heaps without a lot of tuning. Finally, if you are looking at pure cost efficiency, there really isn't a much more efficient use of heap and CPU than having multiple WARs deployed on a single tomcat instance. I'd hate maintaining such an architecture because deployment to such systems are annoying. However, it's hard to debate that they aren't more efficient on resources. You might guess and say "Hey, this app might need around 1gb and this one 2gb" and for the most part you are likely to overshoot the requirements as padding. On the flip side, having a big JVM running 100 different apps will be more efficient both in terms of CPU utilization and heap utilization. Some apps may only need a burst of memory every once in a while but rest at 100mb. In that scenario, your JVM can more than happily allocate all they need during the burst and free it up when done. It's a bit like thinking about running 1 app in a vm vs running 10 apps in a VM. If all those apps only consume minor amounts of memory and CPU time, you get a lot better cost efficiency if you load 'em up. Consequentially, that's sort of the whole reason k8s exist. I say all this as a justification for why you might want one big VM running many applications, not as a suggestion :D. I've actually maintained such systems and they aren't a whole lot of fun when things go wrong (noisy neighbors suck!). But if the consideration is pure cost and CPU/memory/GC efficiency, then a single big JVM running many apps is hard to beat. More on reddit.com
🌐 r/java
3
0
April 19, 2021
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › java-xmx-xms-memory-heap-size-control
How to control Java heap size (memory) allocation (xmx, xms) | alvinalexander.com
January 31, 2026 - The default size is 64M. (The -server flag increases the default size to 128M.) The maximum heap limit is about 2 GB (2048MB).
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-is-the-default-max-java-heap-size-determined
How is the default max Java Heap size determined? - GeeksforGeeks
July 23, 2025 - Server JVM heap configuration ergonomics are now the same as the Client, except that the default maximum heap size for 32-bit JVMs is 1 gigabyte, corresponding to a physical memory size of 4 gigabytes, and for 64-bit JVMs is 32 gigabytes, ...
🌐
Bell Software
bell-sw.com › blog › guide-to-jvm-memory-configuration-options
List of JVM memory configuration flags
March 14, 2024 - To limit the total RAM consumption, use MaxRam flags. The heap size will be adjusted accordingly. For instance, if you have 1 GB of memory, setting -XX:MaxRAMPercentage=50 (or -XX:MaxRAMFraction=2) will make the JVM allocate approx.
🌐
IBM
ibm.com › support › pages › recommended-maximum-heap-sizes-32-and-64-bit-websphere-java-instances
Recommended Maximum Heap Sizes on 32 and 64 bit WebSphere Java instances
Ideally, you will need to test ... in general, the recommended Maximum Heap range for WebSphere Application Server (WAS), would be between (1024M - 1536M) or (1G - 1.5G); higher values will most likely eventually result in Native Memory contention....
🌐
DataStax
docs.datastax.com › en › dse › 6.9 › managing › operations › change-heap-size.html
Changing heap size parameters | DataStax Enterprise | DataStax Docs
By default, DataStax Enterprise (DSE) sets the Java Virtual Machine (JVM) heap size from 1 to 32 GB depending on the amount of RAM and type of Java installed. The cassandra-env.sh automatically configures the min and max size to the same value ...
Find elsewhere
🌐
Blogger
javarevisited.blogspot.com › 2013 › 04 › what-is-maximum-heap-size-for-32-bit-64-JVM-Java-memory.html
What is the maximum Heap Size of 32 bit or 64-bit JVM in Windows and Linux?
Memory is nothing to do with a signed or unsigned bit as there is no negative memory address. So the theoretical limit for maximum heap size on 32 bit JVM is 4GB and for 64 bit JVM it's 2^64.
Top answer
1 of 6
25

My question is how to choose the min and max values, and the difference between the two (should max-min be small or big?)

Short answer: don't guess, profile your application.

jconsole can give you useful high-level data such as a feeling for the main resident set vs. the transient data that we normally allocate and garbage collect. What you'll see if you look at the memory tab of that display is usually something like a sawtooth. The lower corner of the sawteeth is about where I would normally set the heap minimum whereas I would use the peak or slope of the sawteeth to experiment with a heap maximum. If your teeth are very steep, you might consider a big heap just to delay the garbage collection. However, if they aren't, you could try a smaller heap maximum to see if that might leave more resources for other processes on your machine (for example).

You should also consider the server VM as that will cause different garbage collection behavior.

All that said, you should also use a more detailed tool such as jvisualvm to profile the memory usage of your process. It's possible that you have a memory leak or greedy allocator that you could tune or eliminate. That would completely change your heap needs.

2 of 6
6

You should enable GC logging and check to see where your OOM is ocurring.

-verbose:gc
-Xloggc:gc.log  
-XX:+PrintGCTimeStamps
-XX:+PrintGCDetails

You may be experiencing perm space limits, adjust via -XX:MaxPermSize=YYYm

Anyway to answer your question, I start with no minimums and set the maximum relatively high. I then graph the gc log and find out where my stead state is; visually choose an above-average size for the various generations. Read it like a financial chart, you'll want to see good spread in the new generations and a consistent growth and collection in the tenured generation. As mentioned also graph your perm space to make sure you're not constantly increasing.

GC tuning is an art, in no way a science.

🌐
Oracle
docs.oracle.com › cd › E15523_01 › web.1111 › e13814 › jvm_tuning.htm
5 Tuning Java Virtual Machines (JVMs)
See your vendor's documentation for platform-specific JVM tuning options. ... For example, when you start a WebLogic Server instance from a java command line, you could specify the HotSpot VM heap size values as follows: $ java -XX:NewSize=128m -XX:MaxNewSize=128m -XX:SurvivorRatio=8 -Xms512m -Xmx512m
🌐
Elastic
elastic.co › elasticsearch labs › blogs › elasticsearch heap size usage and jvm garbage collection
Elasticsearch heap size usage and JVM garbage collection - Elasticsearch Labs
December 31, 2025 - The best practices for managing heap size usage and JVM garbage collection in a large Elasticsearch cluster are to ensure that the heap size is set to a maximum of 50% of the available RAM, and that the JVM garbage collection settings are optimized ...
🌐
Mike my bytes
mikemybytes.com › 2022 › 11 › 15 › what-happens-when-you-only-limit-the-maximum-heap-size
What happens when you only limit the maximum heap size? | Mike my bytes
November 15, 2022 - Built-in container awareness makes the JVM respect various container-specific limits (e.g. CPU, memory). This means, that even when running an app with a dummy java -jar app.jar (which is usually not the best idea), everything should just work. That’s probably why the only memory-related option provided is often the -Xmx flag (or any of its equivalents). In other words, we tend to only limit the maximum heap size...
🌐
Baeldung
baeldung.com › home › java › jvm › guide to the most important jvm parameters
Guide to the Most Important JVM Parameters | Baeldung
January 8, 2024 - As per the Oracle guidelines, after total available memory, the second most influential factor is the proportion of the heap reserved for the Young Generation. By default, the minimum size of the YG is 1310 MB, and maximum size is unlimited.
🌐
CloudBees
docs.cloudbees.com › knowledge base › java heap settings best practice
Java Heap settings Best Practice
It is used to compute Java heap size only if your overall available memory’s size in the physical server (or in the container) is less than 250MB (approximately). A Host with 250 MB of memory would not been able to support the minimal requirement of Heap Size for CloudBees CI on Production ...
🌐
Coderanch
coderanch.com › t › 202911 › Performance › java › JVM-Max-Heap-Size-recommended
JVM Max Heap Size recommended (Performance forum at Coderanch)
July 10, 2007 - If we can have JVM max heap size in the range of 4 to 8 GB, we can enable multi threading in our application to process transactional data in parallel.
🌐
Oracle
docs.oracle.com › cd › E19159-01 › 819-3681 › abeii › index.html
Tuning the Java Heap (Sun Java System Application Server 9.1 Performance Tuning Guide)
The default NewRatio for the Server JVM is 2: the old generation occupies 2/3 of the heap while the new generation occupies 1/3. The larger new generation can accommodate many more short-lived objects, decreasing the need for slow major collections. The old generation is still sufficiently large enough to hold many long-lived objects. ... Decide the total amount of memory you can afford for the JVM. Accordingly, graph your own performance metric against young generation sizes to find the best setting.
🌐
IBM
ibm.com › docs › en › records-manager › 8.5.0
Setting the initial and maximum heap size for the JVM
Heap size specifies the amount of dynamic memory to be made available to the JVM code. For systems with less than 1 GB of physical memory, use a maximum heap size of 256 MB, and an initial heap size of 0 MB.
🌐
Azul
docs.azul.com › prime › Heap-Size
Recommended Heap Size
The -Xmx or -XX:MaxRAMPercentage command line parameters specifies the maximum Java heap size.
🌐
Red Hat
access.redhat.com › solutions › 20047
How much heap should be allocated to a Java instance? - Red Hat Customer Portal
August 7, 2024 - We are using 64-bit processing with over 10GB of RAM. I believe the standard JVM settings are 512MB for the AMQ brokers, so before we begin modifying these settings, we are asking if there are some recommended settings for JBoss Fuse ActiveMQ brokers on a 64-bit CPU.