Commit d27c1aa
feat(container): auto-tune JVM GC algorithm in setenv.sh based on available vCPUs (#36259)
## Summary
- Replaces the static `JAVA_OPTS_MEMORY` GC config with CPU-aware
auto-selection at container startup
- 1–3 vCPUs → G1GC (default) + `G1PeriodicGCInterval=10000` to return
memory to OS on idle
- 4+ vCPUs → `-XX:+UseZGC` (Java 25 GenerationalZGC by default;
sub-millisecond pauses, scales with cores)
- SerialGC explicitly excluded — not appropriate for a long-running
multi-threaded web server
## Details
CPU detection uses `nproc` with `/proc/cpuinfo` fallback; defaults to 2
if neither is available. Base memory flags (`MaxRAMPercentage=72.0`,
heap free ratios, NIO buffer cap) are unchanged. Set `JAVA_OPTS_MEMORY`
before startup to bypass auto-selection entirely.
Follows the same cgroup-aware pattern already used by
`JAVA_OPTS_DIRECT`.
## Test plan
- [ ] Verify startup log prints `setenv: auto-GC: ...` with detected
vCPU count
- [ ] On a 1-vCPU container (or `nproc` returning 1–3), confirm
`G1PeriodicGCInterval` appears in JVM flags (`-XX:+PrintFlagsFinal` or
`jcmd <pid> VM.flags`)
- [ ] On a 4+-vCPU container, confirm `-XX:+UseZGC` / GenerationalZGC is
active
- [ ] Set `JAVA_OPTS_MEMORY="-XX:MaxRAMPercentage=60.0"` and confirm
auto-tune block is skipped
- [ ] Confirm no regression in startup time or heap usage on standard
containers
Closes #36258
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent 4ac6ea6 commit d27c1aa
1 file changed
Lines changed: 17 additions & 2 deletions
Lines changed: 17 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
10 | 25 | | |
11 | 26 | | |
12 | 27 | | |
| |||
0 commit comments