Quick revision
The short answer
-Xms sets the initial heap size and -Xmx the maximum heap size. Many teams set them equal for servers because the heap is then fixed from the start: no resizing work, no shrinking and regrowing under changing load, and predictable memory use. In containers, the alternative is -XX:InitialRAMPercentage and -XX:MaxRAMPercentage, which size the heap as a share of the container's memory limit so one image fits several pod sizes, always leaving headroom for the non-heap memory the JVM also uses.
What to remember
In this answer, we saw that -Xms sets the initial and -Xmx the maximum heap size. Equal values avoid resizing, keep memory use predictable and surface memory shortages at deploy time, at the cost of never returning memory. With different values the JVM grows and may shrink the heap with GC behaviour. In containers, InitialRAMPercentage and MaxRAMPercentage size the heap relative to the memory limit so one image fits several pod sizes, and the remaining memory must cover metaspace, code cache, thread stacks, GC structures and native memory.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
-Xms sets the initial heap size and -Xmx the maximum heap size. Many teams set them equal for servers because the heap is then fixed from the start: no resizing work, no shrinking and regrowing under changing load, and predictable memory use. In containers, the alternative is -XX:InitialRAMPercentage and -XX:MaxRAMPercentage, which size the heap as a share of the container's memory limit so one image fits several pod sizes, always leaving headroom for the non-heap memory the JVM also uses.
Why this question is asked
Heap size is the first JVM setting most of us ever touch, and it is still a common cause of production trouble: containers killed for exceeding their memory limit, heaps that grow and shrink with load, and images hard-coded for one pod size. Interviewers ask about it to see whether we know what the flags really control and how they interact with containers. In this answer, we will recap what the heap is, see what each flag does, explain why equal values are popular, look at the percentage-based flags for containers, and work out how much headroom the rest of the process needs.
1A Quick Recap of the Heap
The heap is the area of memory where the JVM stores Java objects. The garbage collector manages it: it allocates new objects there and reclaims objects that are no longer reachable. The JVM reserves address space for the maximum heap at startup but only commits, that is actually backs with memory, the part it is currently using. Committed heap can grow up to the maximum, and with some collectors it can shrink again when the JVM decides it needs less.
2What -Xms and -Xmx Do
-Xmx sets the maximum heap size. The heap never grows beyond it; if live objects need more, the JVM throws OutOfMemoryError: Java heap space. -Xms sets the initial heap size, the amount committed at startup. If we set neither, the JVM chooses them from the machine's or container's memory: by default the maximum is a quarter of available memory and the initial size is a small fraction of it.
With different values, the JVM starts with the smaller heap and grows it when GC finds that the heap is too full or that collections happen too often. Some collectors can also shrink it again when less is needed: G1, for example, can return unused memory to the operating system after a full or concurrent cycle, and periodically when the application is idle, if configured. So the committed heap moves up and down with the load.
3Why Servers Often Use Equal Values
Growing the heap costs a little work each time: the collector decides to expand, commits more memory and adjusts its internal sizing. Under a traffic spike the JVM may be collecting more often while it grows, exactly when we least want it. If the heap later shrinks during a quiet period and the load comes back, the cycle repeats. Setting -Xms equal to -Xmx removes all of that: the heap is fully committed from the start and stays that size.
There is a second benefit: predictability. A server that will use its full heap under peak load anyway might as well claim it at startup. If the machine or container does not have that memory, we find out at deploy time, not at 3 a.m. under peak traffic. Some teams add -XX:+AlwaysPreTouch, which touches every page of the heap at startup so the operating system really allocates it; startup gets slower, but there are no page-fault delays later.
The trade-off is that the JVM never gives memory back. On a shared machine where several services peak at different times, a growing heap can be more economical. For a dedicated server or a container with a fixed memory limit, equal values are usually the simpler choice.
| Aspect | -Xms = -Xmx | -Xms < -Xmx |
|---|---|---|
| Memory at startup | full heap committed | small, grows on demand |
| Resizing work | none | grow and possibly shrink |
| Behaviour under spikes | stable | may collect more while growing |
| Returns memory when idle | no | possibly, depending on the GC |
| Best for | dedicated servers, fixed containers | shared hosts, desktop apps |
4The Container-Friendly Alternative
Hard-coding -Xmx4g ties the image to one pod size. If the same image runs in a 2 GB pod, it will be killed by the container's memory limit; in an 8 GB pod, half the memory is wasted. Since JDK 10 the JVM is container aware: it reads the cgroup memory limit. Two flags let us express the heap as a share of that limit. -XX:MaxRAMPercentage sets the maximum heap as a percentage of the limit, and -XX:InitialRAMPercentage sets the initial heap the same way. Setting both to the same value gives the equal-values behaviour in a size-independent form.
Notice the default in the last line. Many teams discover that their 4 GiB pods use a 1 GiB heap because the JVM's default of 25 percent was designed for machines shared with other processes. In a container that runs only the JVM, values between 50 and 75 percent are common, depending on how much non-heap memory the service needs. The percentages accept fractional values, and -Xmx, if also given, takes precedence, so we should use one approach, not both.
5Heap Is Only Part of the Process
The container's memory limit applies to the whole process, not just the heap. Exceeding it gets the container killed by the kernel, with no OutOfMemoryError and no heap dump. Besides the heap, the JVM uses metaspace for class metadata (often 100 to 300 MB for a Spring Boot service), the code cache for compiled code (up to 240 MB by default with tiered compilation), a stack per thread (often 1 MB of reserved space each on 64-bit Linux, though only the used part is committed), the garbage collector's own structures, direct byte buffers used by networking libraries such as Netty, and memory used by native libraries and the C allocator.
- Java heapset by -Xmx or MaxRAMPercentage
- Metaspaceclass metadata, grows with loaded classes
- Code cacheJIT-compiled code
- Thread stacksone per thread, platform threads
- GC structurese.g. G1 remembered sets
- Direct buffers and native memoryNetty, compression, native libraries
6 layers, from the top down. Step through them one at a time.
So in a 4 GiB pod, a 3.8 GiB heap is a mistake even though it fits: the rest of the process has nowhere to go. We measure the non-heap part with Native Memory Tracking (-XX:NativeMemoryTracking=summary and jcmd <pid> VM.native_memory summary), and set the heap percentage so heap plus measured non-heap plus a safety margin stays below the limit.
6Follow-up questions
Should I still set -Xmx if I use MaxRAMPercentage?
No, choose one. -Xmx overrides the percentage, so setting both makes the percentage useless and brings back the problem of an image that only fits one pod size. Percentages are the better default for container images.
Why does my container get OOM-killed when the heap is far from full?
Because the limit covers the whole process. Growing metaspace, many threads, large direct buffers from a networking library or native allocations can push total memory over the limit while the heap looks fine. Native Memory Tracking and the container's memory metrics show where the memory went, and the usual fix is a lower heap percentage or a higher limit.
7Conclusion
In this answer, we saw that -Xms sets the initial and -Xmx the maximum heap size. Equal values avoid resizing, keep memory use predictable and surface memory shortages at deploy time, at the cost of never returning memory. With different values the JVM grows and may shrink the heap with GC behaviour. In containers, InitialRAMPercentage and MaxRAMPercentage size the heap relative to the memory limit so one image fits several pod sizes, and the remaining memory must cover metaspace, code cache, thread stacks, GC structures and native memory.
Open with
-Xms is the initial heap size, what the JVM commits at startup, and -Xmx is the maximum heap size; if live data needs more than that, we get OutOfMemoryError.
Then, if they want more
With different values, the JVM starts small and grows the heap when GC sees it is too full, and some collectors, like G1, can shrink it and give memory back when load drops.
For servers, many teams set them equal: there is no resizing work during traffic spikes, no grow and shrink cycles, memory use is predictable from the first minute, and if the memory is not there we find out at deploy time rather than under peak load.
The cost is that the JVM never returns memory, which matters more on shared hosts than in dedicated containers. In containers I prefer percentages instead of fixed sizes: InitialRAMPercentage and MaxRAMPercentage size the heap relative to the container's memory limit, so one image works in a 2 GB or an 8 GB pod.
The default maximum is only 25 percent, so we usually raise it to somewhere around 60 to 75.
And I always remember that heap is only part of the process: metaspace, code cache, thread stacks, GC structures and direct buffers also count against the container limit, so I measure them with Native Memory Tracking and leave headroom, otherwise the container gets OOM-killed even though the heap is fine.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 10
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What do -Xms and -Xmx do, and why do many teams set them to the same value for server...": https://scrivio-interview-library.netlify.app/java/jvm-tuning/fcfffb3885/