Measured on JDK 23 with MaxRAM sweeps: 25 percent above 504 MiB, a 126 MiB floor, 50 percent on small machines. Size heap and native headroom.
The heap you get when you set nothing
A Java service started without -Xmx still gets a maximum heap. HotSpot computes it at startup from the memory it believes the machine has, using three flags most teams never set: MaxRAMPercentage, MinRAMPercentage and InitialRAMPercentage. On a laptop the result is rarely noticed. In a container with a 512 MiB limit, or on a small virtual machine, it decides whether the service has room for its working set, whether the garbage collector runs constantly, and how much memory is left for everything that is not heap.
This guide reads the rule from the HotSpot source, measures it on JDK 23.0.2 by sweeping -XX:MaxRAM, and turns the result into a sizing method. It is careful about one limit. The measurements were taken on Windows, where -XX:MaxRAM tells the JVM how much memory to assume. On Linux, the same calculation starts from the container's memory limit when container support is active, which is the default. In os_linux.cpp, os::physical_memory() returns the container memory limit when the process is containerized and a limit is set. That path was read in the source and not run here, because no container runtime was available for the measurement.
The running example is a catalogue search service at Belgavi's Book Store that Tanishka is moving from a 4 GiB virtual machine to containers with a 512 MiB limit, with no JVM flags set.
The rule in the source
In arguments.cpp, Arguments::set_heap_size computes the maximum heap when -Xmx (MaxHeapSize) is not set on the command line. In outline, with phys_mem being the memory the JVM assumes:
reasonable_max = phys_mem * MaxRAMPercentage / 100(default 25 percent).reasonable_min = phys_mem * MinRAMPercentage / 100(default 50 percent).- If
reasonable_minis smaller than the defaultMaxHeapSize, the machine counts as small and the heap isreasonable_min. - Otherwise the heap is the larger of
reasonable_maxand the defaultMaxHeapSize.
The default MaxHeapSize is ScaleForWordSize(96*M), which on a 64-bit JVM is 126 MiB. So MinRAMPercentage is not a minimum in the everyday sense. It is the percentage used only on small machines, where half of memory is still below 126 MiB, that is below 252 MiB. Between 252 MiB and 504 MiB the 126 MiB floor applies. Above 504 MiB the 25 percent rule takes over. The initial heap is computed similarly from InitialRAMPercentage, 1.5625 percent by default.
Measured on JDK 23.0.2
The companion example jvm_heap_ergonomics runs java -XX:MaxRAM=<size> -XX:+PrintFlagsFinal -version for a range of sizes and reads MaxHeapSize and InitialHeapSize.
| Assumed memory | Default flags | MaxRAMPercentage=75 | MinRAMPercentage=80 |
|---|---|---|---|
| 64 MiB | 32 MiB | 32 MiB | 52 MiB |
| 128 MiB | 64 MiB | 64 MiB | 104 MiB |
| 200 MiB | 100 MiB | 100 MiB | 126 MiB |
| 256 MiB | 126 MiB | 192 MiB | 126 MiB |
| 512 MiB | 128 MiB | 384 MiB | 128 MiB |
| 1 GiB | 256 MiB | 768 MiB | 256 MiB |
| 4 GiB | 1,024 MiB | 3,072 MiB | 1,024 MiB |
| 16 GiB | 4,096 MiB | 12,288 MiB | 4,096 MiB |
Every cell matches the rule once the result is rounded up to the 2 MiB heap alignment, which is why 80 percent of 64 MiB shows as 52 MiB rather than 51.2. At 128 MiB the small-machine branch gives 50 percent, 64 MiB. At 256 MiB half of memory is 128 MiB, which is not below 126 MiB, so the default branch applies and the 126 MiB floor wins over 25 percent. From 512 MiB up the heap is exactly a quarter. Below 252 MiB, raising MaxRAMPercentage to 75 changed nothing, because the small-machine branch ignores it. Raising MinRAMPercentage to 80 changes nothing at 200 MiB and above: at 200 MiB 80 percent is 160 MiB, the machine is no longer small, and the floor applies again. Many configurations set MinRAMPercentage expecting a floor and get no effect. MaxRAMPercentage is the flag that moves the heap on anything but tiny machines.
The initial heap followed InitialRAMPercentage with an 8 MiB minimum: 8 MiB at 256 MiB and 512 MiB, 16 MiB at 1 GiB, 64 MiB at 4 GiB and 256 MiB at 16 GiB.
Worked example: the 512 MiB container
On the old virtual machine, the catalogue service got a 1 GiB heap by default, a quarter of 4 GiB. In a container with a 512 MiB limit and no flags, it gets 128 MiB. In this example its warm working set, as the old machine's GC logs would show it, is about 180 MiB. The service starts, then spends its time in garbage collection and eventually fails with OutOfMemoryError: Java heap space, while the container has 384 MiB the JVM never uses for the heap.
The tempting fix is -XX:MaxRAMPercentage=90. That requests about 461 MiB before heap alignment and leaves roughly 50 MiB for everything else, which is insufficient in this hypothetical workload. Non-heap memory includes metaspace for loaded classes, the code cache, thread stacks, GC data structures, direct buffers used by network libraries, and the C heap. When heap plus non-heap exceeds the container limit, the kernel kills the process, and the JVM never logs an OutOfMemoryError.
A better method sizes from measurements:
- Inspect HotSpot native allocations with Native Memory Tracking: start with
-XX:NativeMemoryTracking=summaryand runjcmd <pid> VM.native_memory summaryunder load. NMT is not total process memory and excludes some third-party native allocations. Cross-check process RSS and container memory accounting before setting the limit. - Choose the heap from the working set plus headroom for the collector, for example 180 MiB live data and a 256 MiB maximum heap.
- Check that heap plus measured non-heap plus a safety margin fits the limit. If non-heap is 150 MiB, 256 + 150 leaves about 100 MiB of margin in 512 MiB.
- Express the heap as a percentage of the limit so it scales if the limit changes: 256 of 512 is
-XX:MaxRAMPercentage=50. Or set-Xmx256mexplicitly if the limit never changes.
JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=50 -XX:+ExitOnOutOfMemoryError"ExitOnOutOfMemoryError makes a heap exhaustion end the process, so the orchestrator restarts it instead of leaving a half-working JVM.
The garbage collector also changes
Heap size is not the only ergonomic decision. HotSpot picks G1 on what it calls a server-class machine: at least two active processors and roughly 2 GiB of memory, according to os::is_server_class_machine. Otherwise it picks Serial. The probe shows the processor half: with -XX:ActiveProcessorCount=1, the same JVM selected UseSerialGC. The memory half uses the JVM's view of physical memory, not MaxRAM, which is why -XX:MaxRAM=1g alone still selected G1 on this machine. In a container with a single CPU or a small memory limit, the collector can change silently along with the heap. Set it explicitly if your tuning depends on it.
Failure modes
A smaller container with the same flags. A percentage scales down with the limit, and so does the working set's headroom. Re-check step 3 whenever limits change.
Setting MinRAMPercentage instead of MaxRAMPercentage. As measured above, it only matters below about 252 MiB of memory with default settings.
Explicit -Xmx copied from a large machine. An -Xmx2g left in a start script will be accepted in a 512 MiB container and the process will be killed by the kernel under load.
Native memory growth. Thread counts, direct buffers and class loading grow with load. Measure non-heap under realistic traffic, not at idle.
When to leave the defaults
On machines with several gigabytes and services with small working sets, the 25 percent default is conservative and harmless. Tuning pays off on small containers and on services whose working set is a large share of memory.
How to verify it
Print the final flags in the target environment with java -XX:+PrintFlagsFinal -version | grep -E "MaxHeapSize|UseG1GC|UseSerialGC", from inside the container image, not on a workstation. Log Runtime.getRuntime().maxMemory() at startup. Watch the container's memory usage against its limit together with GC logs. The companion example reproduces the sweep above and asserts the rule for each row; on Linux, add a run inside a container with a memory limit and compare it with the MaxRAM rows.