Sub topic 11
Heap Sizing and Basic Tuning
This chapter assumes you can compile and run Java programs from the command line. You should also understand that variables hold references to objects, while the objects themselves usually live on the heap.
What heap sizing controls
The Java Virtual Machine (JVM) grows the Java heap as your program needs memory. The heap stores most Java objects. Two options set its boundaries:
-Xmssets the initial heap size.-Xmxsets the maximum heap size.
For example:
java -Xms512m -Xmx2g MyApplicationThis starts with a heap of about 512 megabytes and allows the heap to grow to about 2 gigabytes. The operating system may reserve or commit memory differently during startup, so these values do not always equal the process's total memory use.
If the heap is too small, collections happen often and the application may spend too much time reclaiming memory. If it is too large, the JVM may consume memory needed by the operating system, native libraries, threads, or other applications.
Observe the running heap
The Runtime class reports the heap values visible to the application:
public class MemoryInfo {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
long mb = 1024 * 1024;
System.out.println("Max heap: " + rt.maxMemory() / mb + " MB");
System.out.println("Total heap: " + rt.totalMemory() / mb + " MB");
System.out.println("Free heap: " + rt.freeMemory() / mb + " MB");
}
}Compile and run it with explicit limits:
javac MemoryInfo.java
java -Xms256m -Xmx1g MemoryInfomaxMemory() is close to the -Xmx limit. totalMemory() is the heap currently committed by the JVM. freeMemory() is unused space inside that committed heap. These values describe the Java heap, not all memory used by the process.
A practical sizing process
Begin with a representative workload rather than a guess. Measure the application's live data after warm-up, then observe its peak allocation rate and pause behavior.
A useful process is:
- Set
-Xmxto a safe limit that leaves room for the operating system and non-heap memory. - Run a realistic load test.
- Record heap usage, garbage-collection pauses, throughput, and out-of-memory failures.
- Increase the limit only when measurements show that the application needs it.
- Repeat after changing the collector or application workload.
The initial size does not need to equal the maximum. Setting them equal can avoid heap resizing, but it commits more memory earlier:
java -Xms512m -Xmx512m MyApplicationA fixed heap can make measurements more predictable. A smaller -Xms can reduce startup memory use.
| Option | Meaning | Typical reason to change it |
|---|---|---|
| `-Xms` | Initial heap size | Control startup commitment |
| `-Xmx` | Maximum heap size | Set a memory safety limit |
| `-Xss` | Per-thread stack size | Adjust only when stack capacity is a measured issue |
Collector flags and tuning limits
Java 11 includes several collectors. G1 is a common general-purpose choice:
java -Xms512m -Xmx512m -XX:+UseG1GC -Xlog:gc*:file=gc.log MyApplicationThe collector affects pause times, throughput, and how the heap is organized. Change one major setting at a time and compare it with a baseline. Avoid copying a flag set from another application because allocation rates, object lifetimes, heap size, and pause requirements differ.
Container limits
In a container, the JVM may detect the container's memory limit rather than the host's total memory. Java 11 versions differ in their container-awareness behavior, so verify the exact JDK update and deployment environment. Leave room for native memory, thread stacks, direct buffers, and the Java launcher itself. The Java heap must not consume the entire container limit.
Exercise
An application has frequent garbage collections and reaches 95% of its 256 MB heap, but the container has 1 GB available. What should you investigate before simply setting -Xmx1g?
Show answer
Measure the live set, allocation rate, pause times, retained objects, and non-heap memory. Then choose a heap limit that leaves room for the rest of the process and confirm the result with a representative workload.