Skip to content

Commit be4cdb9

Browse files
committed
added memory/gc flag optimization guide
1 parent 0991301 commit be4cdb9

File tree

2 files changed

+39
-1
lines changed
  • content
    • install-guides
    • learning-paths/servers-and-cloud-computing/migration

2 files changed

+39
-1
lines changed

content/install-guides/java.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ To print the final values of the flags after the JVM has been initialized, run:
234234
java -XX:+PrintFlagsFinal -version
235235
```
236236

237+
Generally the biggest performance improvements from JVM flags can be obtained from heap and garbage collection (GC) tuning, as long as you understand your workload well.
238+
239+
Default initial heap size is 1/64th of RAM and default maximum heap size is 1/4th of RAM. If you know your memory requirements, you should set both of these flags to the same value (e.g. `-Xms12g` and `-Xmx12g` for an application that uses at most 12 GB). Setting both flags to the same value will prevent the JVM from having to periodically allocate additional memory. Additionally, for cloud workloads max heap size is often set to 75%-85% of RAM, much higher than the default setting.
240+
241+
If you are deploying in a cloud scenario where you might be deploying the same stack to systems that have varying amounts of RAM, you might want to use `-XX:MaxRAMPercentage` instead of `-Xmx`, so you can specify a percentage of max RAM rather than a fixed max heap size. This setting can also be helpful in containerized workloads.
242+
243+
Garbage collector choice will depend on the workload pattern for which you're optimizing.
244+
245+
* If your workload is a straightforward serial single-core load with no multithreading, the `UseSerialGC` flag should be set to true.
246+
* For multi-core small heap batch jobs (<4GB), the `UseParallelGC` flag should be set to true.
247+
* The G1 garbage collector (`UseG1GC` flag) is better for medium to large heaps (>4GB). This is the most commonly used GC for large parallel workloads, and is the default for high-core environments. If you want to optimize throughput, use this one.
248+
* The ZGC (`UseZGC` flag) has low pause times, which can drastically improve tail latencies. If you want to prioritize response time at a small cost to throughput, use ZGC.
249+
* The Shenandoah GC (`UseShenandoahGC` flag) is still fairly niche. It has ultra low pause times and concurrent evacuation, making it ideal for low-latency applications, at the cost of increased CPU use.
250+
237251
## Are there other tools commonly used in Java projects?
238252

239253
There are a number of Java-related tools you might like to install.

content/learning-paths/servers-and-cloud-computing/migration/java.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,31 @@ Depending on your application, you may want to investigate the vector processing
9191

9292
You can try [Process Watch](https://learn.arm.com/learning-paths/servers-and-cloud-computing/processwatch/) to monitor the usage of SIMD and CRC instructions.
9393

94-
Refer to the [Java documentation](https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html) for more information about the flags.
94+
Refer to the [Java documentation](https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html) for more information about the flags.
95+
96+
## Memory and Garbage Collection
97+
98+
The default [JVM ergonomics](https://docs.oracle.com/en/java/javase/21/gctuning/ergonomics.html) can generally be improved upon if you understand your workload well.
99+
100+
Default initial heap size is 1/64th of RAM and default maximum heap size is 1/4th of RAM. If you know your memory requirements, you should set both of these flags to the same value (e.g. `-Xms12g` and `-Xmx12g` for an application that uses at most 12 GB). Setting both flags to the same value will prevent the JVM from having to periodically allocate additional memory. Additionally, for cloud workloads max heap size is often set to 75%-85% of RAM, much higher than the default setting.
101+
102+
If you are deploying in a cloud scenario where you might be deploying the same stack to systems that have varying amounts of RAM, you might want to use `-XX:MaxRAMPercentage` instead of `-Xmx`, so you can specify a percentage of max RAM rather than a fixed max heap size. This setting can also be helpful in containerized workloads.
103+
104+
Garbage collector choice will depend on the workload pattern for which you're optimizing.
105+
106+
* If your workload is a straightforward serial single-core load with no multithreading, the `UseSerialGC` flag should be set to true.
107+
* For multi-core small heap batch jobs (<4GB), the `UseParallelGC` flag should be set to true.
108+
* The G1 garbage collector (`UseG1GC` flag) is better for medium to large heaps (>4GB). This is the most commonly used GC for large parallel workloads, and is the default for high-core environments. If you want to optimize throughput, use this one.
109+
* The ZGC (`UseZGC` flag) has low pause times, which can drastically improve tail latencies. If you want to prioritize response time at a small cost to throughput, use ZGC.
110+
* The Shenandoah GC (`UseShenandoahGC` flag) is still fairly niche. It has ultra low pause times and concurrent evacuation, making it ideal for low-latency applications, at the cost of increased CPU use.
111+
112+
If you'd like to see what the default JVM values are for specific processor counts, you can run
113+
114+
```bash
115+
java -XX:ActiveProcessorCount=[selected processor count] -XX:+PrintFlagsFinal -version
116+
```
117+
118+
Where `[selected processor count]` is the number of processors you want to evaluate the defaults for. You can also use this `-XX:ActiveProcessorCount` if you don't want to set GC and RAM sizes manually, if you know which default configuration you want to force the JVM to use.
95119

96120
## Crypto
97121

0 commit comments

Comments
 (0)