Analyze JVM memory usage and configure garbage collection settings for optimal performance.
- Understand JVM memory regions (Heap, Stack, Metaspace)
- Enable and interpret GC logs
- Compare different garbage collectors
- Use JVM tuning flags
- Java 17+ installed
- Basic understanding of Java memory model
Create a Java application that allocates memory progressively.
Starter Code Provided: MemoryLabApp.java
Run the application with the following JVM flags:
java -Xms128m -Xmx256m -Xlog:gc*:file=gc.log:time,uptime MemoryLabAppQuestions to Answer:
- How many Minor GCs occurred? - 6
- How many Major/Full GCs occurred? - 7
- What was the longest GC pause time? - 9.343ms
Run the application with different GC algorithms:
# Serial GC
java -XX:+UseSerialGC -Xmx256m -Xlog:gc*:file=gc-serial.log MemoryLabApp
# G1GC
java -XX:+UseG1GC -Xmx256m -Xlog:gc*:file=gc-g1.log MemoryLabApp
# ZGC (Java 11+)
java -XX:+UseZGC -Xmx256m -Xlog:gc*:file=gc-zgc.log MemoryLabAppRecord your observations in a table:
| GC Type | Total GC Events | Longest Pause | Throughput |
|---|---|---|---|
| Serial | 6 | 25.018 | 99.9% |
| G1GC | 22 | 18.124 | 99.92% |
| ZGC | 28 | 5.557 | 99.9% |
Configure G1GC to target a maximum pause time of 100ms:
java -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -Xmx256m MemoryLabAppQuestions:
- Did G1GC achieve the target pause time? yes
- What trade-offs did you observe? It seemed to be more inconsistent
- Completed
MemoryLabApp.java - GC log files from each run
- Answers to all questions in a markdown file
See starter_code/MemoryLabApp.java