Skip to content

Commit 7708c98

Browse files
authored
Merge pull request #1432 from madeline-underwood/JavaGC
Java GC_approved by Andy Pickard
2 parents ef0c85d + 872cb4c commit 7708c98

File tree

8 files changed

+165
-117
lines changed

8 files changed

+165
-117
lines changed

content/learning-paths/servers-and-cloud-computing/java-gc-tuning/Example_application.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
---
22
title: Example Application
3-
weight: 4
3+
weight: 5
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

9-
## Example Application.
9+
## Example Application
1010

11-
Using a file editor of your choice, copy the Java snippet below into a file named `HeapUsageExample.java`. This code example allocates 1 million string objects to fill up the heap. You can use this example to easily observe the effects of different GC tuning parameters.
11+
Using a file editor of your choice, copy the Java snippet below into a file named `HeapUsageExample.java`.
12+
13+
This code example allocates 1 million string objects to fill up the heap. You can use this example to easily observe the effects of different GC tuning parameters.
1214

1315
```java
1416
public class HeapUsageExample {
@@ -32,9 +34,13 @@ public class HeapUsageExample {
3234
}
3335
```
3436

35-
### Enable GC logging
37+
### Enable Garbage Collector logging
38+
39+
To observe what the Garbage Collector is doing, one option is to enabling logging while the JVM is running.
40+
41+
To enable this, you need to pass in some command-line arguments. The `gc` option logs the GC information. The `filecount` option creates a rolling log to prevent uncontrolled growth of logs with the drawback that historical logs might be rewritten and lost.
3642

37-
To observe what the GC is doing, one option is to enabling logging while the JVM is running. To enable this, you need to pass in some command-line arguments. The `gc` option logs the GC information. The `filecount` option creates a rolling log to prevent uncontrolled growth of logs with the drawback that historical logs may be rewritten and lost. Run the following command to enable logging with JDK 11 and higher:
43+
Run the following command to enable logging with JDK 11 and higher:
3844

3945
```bash
4046
java -Xms512m -Xmx1024m -XX:+UseSerialGC -Xlog:gc:file=gc.log:tags,uptime,time,level:filecount=10,filesize=16m HeapUsageExample.java
@@ -46,7 +52,7 @@ If you are using JDK8, use the following command instead:
4652
java -Xms512m -Xmx1024m -XX:+UseSerialGC -Xloggc:gc.log -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation HeapUsageExample.java
4753
```
4854

49-
The `-Xms512m` and `-Xmx1024` options create a minimum and maximum heap size of 512 MiB and 1GiB respectively. This is simply to avoid waiting too long to see activity within the GC. Additionally, you will force the JVM to use the serial garbage collector with the `-XX:+UseSerialGC` flag.
55+
The `-Xms512m` and `-Xmx1024` options create a minimum and maximum heap size of 512 MiB and 1GiB respectively. This is to avoid waiting too long to see activity within the GC. Additionally, you can force the JVM to use the serial garbage collector with the `-XX:+UseSerialGC` flag.
5056

5157
You will now see a log file, named `gc.log` created within the same directory.
5258

@@ -58,17 +64,19 @@ Open `gc.log` and the contents should look similar to:
5864
[2024-11-08T15:04:54.350+0000][0.759s][info][gc] GC(3) Pause Young (Allocation Failure) 139M->3M(494M) 3.699ms
5965
```
6066

61-
These logs provide insights into the frequency, duration, and impact of Young garbage collection events. The results may vary depending on your system.
67+
These logs provide insights into the frequency, duration, and impact of Young garbage collection events. The results can vary depending on your system.
6268

6369
- Frequency: ~ every 46 ms
6470
- Pause duration: ~ 3.6 ms
6571
- Reduction size: ~ 139 MB (or 3M objects)
6672

67-
This logging method can be quite verbose. Also, this method isn't suitable for a running process which makes debugging a live running application slightly more challenging.
73+
This logging method can be quite verbose, and makes it challenging to debug a live running application.
6874

6975
### Use jstat to observe real-time GC statistics
7076

71-
Using a file editor of your choice, copy the java code below into a file named `WhileLoopExample.java`. This java code snippet is a long-running example that prints out a random integer and double precision floating point number 4 times a second.
77+
Using a file editor of your choice, copy the java code below into a file named `WhileLoopExample.java`.
78+
79+
This java code snippet is a long-running example that prints out a random integer and double precision floating point number four times a second:
7280

7381
```java
7482
import java.util.Random;
@@ -91,7 +99,7 @@ public class GenerateRandom {
9199
// Print random double
92100
System.out.println("Random Doubles: " + rand_dub1);
93101

94-
// Sleep for 1 second (1000 milliseconds)
102+
// Sleep for 1/4 second (250 milliseconds)
95103
try {
96104
Thread.sleep(250);
97105
} catch (InterruptedException e) {
@@ -107,13 +115,15 @@ Start the Java program with the command below. This will use the default paramet
107115
```bash
108116
java WhileLoopExample.java
109117
```
110-
While the program running, open another terminal session. In the new terminal use the `jstat` command to print out the JVM statistics specifically related to the GC using the `-gcutil` flag:
118+
While the program is running, open another terminal session.
119+
120+
In the new terminal use the `jstat` command to print out the JVM statistics specifically related to the GC using the `-gcutil` flag:
111121

112122
```bash
113123
jstat -gcutil $(pgrep java) 1000
114124
```
115125

116-
You will observe output like the following until `ctl+c` is pressed.
126+
You will observe output like the following until `ctl+c` is pressed:
117127

118128
```output
119129
S0 S1 E O M CCS YGC YGCT FGC FGCT CGC CGCT GCT
@@ -125,10 +135,10 @@ You will observe output like the following until `ctl+c` is pressed.
125135
```
126136

127137
The columns of interest are:
128-
- **E (Eden Space Utilization)**: The percentage of the Eden space that is currently used. High utilization indicates frequent allocations and can trigger minor GCs.
129-
- **O (Old Generation Utilization)**: The percentage of the Old (Tenured) generation that is currently used. High utilization can lead to Full GCs, which are more expensive.
130-
- **YGCT (Young Generation GC Time)**: The total time (in seconds) spent in Young Generation (minor) GC events. High values indicate frequent minor GCs, which can impact performance.
131-
- **FGCT (Full GC Time)**: The total time (in seconds) spent in Full GC events. High values indicate frequent Full GCs, which can significantly impact performance.
132-
- **GCT (Total GC Time)**: The total time (in seconds) spent in all GC events (Young, Full, and Concurrent). This provides an overall view of the time spent in GC, helping to assess the impact on application performance.
138+
- **E (Eden Space Utilization)**: The percentage of the Eden space that is being used. High utilization indicates frequent allocations and can trigger minor GCs.
139+
- **O (Old Generation Utilization)**: The percentage of the Old (Tenured) generation that is being used. High utilization can lead to Full GCs, which are more expensive.
140+
- **YGCT (Young Generation GC Time)**: The total time in seconds spent in Young Generation (minor) GC events. High values indicate frequent minor GCs, which can impact performance.
141+
- **FGCT (Full GC Time)**: The total time in seconds spent in Full GC events. High values indicate frequent Full GCs, which can significantly impact performance.
142+
- **GCT (Total GC Time)**: The total time in seconds spent in all GC events (Young, Full, and Concurrent). This provides an overall view of the time spent in GC, helping to assess the impact on application performance.
133143

134144

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
---
22
title: Basic GC Tuning Options
3-
weight: 5
3+
weight: 6
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

99
### Update the JDK version
1010

11-
If you are on an older version of JDK, a sensible first step is to use one of the latest long-term-support (LTS) releases of JDK. This is because the GC versions included with recent JDKs offer improvements. For example, the G1GC included with JDK 11 offers improvements in the pause time compared to JDK 8. As shown earlier, you can use the `java --version` command to check the version currently in use.
11+
If you are on an older version of JDK, a sensible first step is to use one of the latest long-term-support (LTS) releases of JDK. This is because the GC versions included with recent JDKs offer improvements on previous releases. For example, the G1GC included with JDK 11 offers improvements in the pause time compared to JDK 8.
12+
13+
As shown earlier, you can use the `java --version` command to check the version currently in use:
1214

1315
```output
1416
$ java --version
@@ -22,25 +24,27 @@ OpenJDK 64-Bit Server VM Corretto-21.0.4.7.1 (build 21.0.4+7-LTS, mixed mode, sh
2224

2325
In this section, you will use the `HeapUsageExample.java` file you created earlier.
2426

25-
The G1 GC (Garbage-First Garbage Collector) is designed to handle large heaps and aims to provide low pause times by dividing the heap into regions and performing incremental garbage collection. This makes it suitable for applications with high allocation rates and large memory footprints.
27+
The Garbage-First Garbage Collector (G1GC) is designed to handle large heaps and aims to provide low pause times by dividing the heap into regions and performing incremental garbage collection. This makes it suitable for applications with high allocation rates and large memory footprints.
28+
29+
You can run the following command to generate the GC logs using a different GC and compare the two.
2630

27-
You can run the following command to generate the GC logs using a different GC and compare. You just need to change the GC from `Serial` to `G1GC` using the `-XX:+UseG1GC` option as shown:
31+
To make this comparison, change the Garbage Collector from `Serial` to `G1GC` using the `-XX:+UseG1GC` option:
2832

2933
```bash
3034
java -Xms512m -Xmx1024m -XX:+UseG1GC -Xlog:gc:file=gc.log:tags,uptime,time,level:filecount=10,filesize=16m HeapUsageExample.java
3135
```
32-
From the created log file `gc.log`, you can observe that at a very similar time after start up (~0.75s), the Pause Young time reduced from ~3.6ms to ~1.9ms. Further, the time between GC pauses has improved from ~46ms to every ~98ms.
36+
From the created log file `gc.log`, you can see that at a similar time after startup (~0.75s), the Pause Young time reduced from ~3.6ms to ~1.9ms. Further, the time between GC pauses has improved from ~46ms to every ~98ms.
3337

3438
```output
3539
[2024-11-08T16:13:53.088+0000][0.790s][info][gc ] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 307M->3M(514M) 1.976ms
3640
...
3741
[2024-11-08T16:13:53.186+0000][0.888s][info][gc ] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 307M->3M(514M) 1.703ms
3842
```
39-
As discussed in the previous section, the performance improvement from moving to a G1GC will depend on the CPU overhead of your system. The performance may vary depending on the cloud instance size and available CPU resources.
43+
As described in the previous section, the performance improvement from moving to a G1GC depends on the CPU overhead of your system. The performance can vary depending on the cloud instance size and available CPU resources.
4044

41-
### Add GC Targets
45+
### Add Garbage Collector Targets
4246

43-
You can manually provide targets for specific metrics and the GC will attempt to meet those requirements. For example, if you have a time-sensitive application such as a REST server, you may want to ensure that all customers receive a response within a specific time. You may find that if a client request is sent during GC you need to ensure that the GC pause time is minimised.
47+
You can manually provide targets for specific metrics and the GC will attempt to meet those requirements. For example, if you have a time-sensitive application such as a REST server, you might want to ensure that all customers receive a response within a specific time. You might find that if a client request is sent during Garbage Collection that you need to ensure that the GC pause time is minimised.
4448

4549
Running the command with the `-XX:MaxGCPauseMillis=<N>` sets a target max GC pause time:
4650

@@ -55,19 +59,19 @@ Looking at the output below, you can see that at the same initial state after ~0
5559
[2024-11-08T16:27:37.149+0000][0.853s][info][gc] GC(19) Pause Young (Normal) (G1 Evacuation Pause) 193M->3M(514M) 0.482ms
5660
```
5761

58-
Here are some additional target options you can consider to tune performance:
62+
Here are some additional target options that you can consider to tune performance:
5963

6064
- -XX:InitiatingHeapOccupancyPercent:
6165

62-
Defines the old generation occupancy threshold to trigger a concurrent GC cycle. Adjusting this can be beneficial if your application experiences long GC pauses due to high old generation occupancy. For example, lowering this threshold can help start GC cycles earlier, reducing the likelihood of long pauses during peak memory usage.
66+
This defines the old generation occupancy threshold to trigger a concurrent GC cycle. Adjusting this is beneficial if your application experiences long GC pauses due to high old generation occupancy. For example, lowering this threshold can help start GC cycles earlier, reducing the likelihood of long pauses during peak memory usage.
6367

6468
- -XX:ParallelGCThreads
6569

66-
Specifies the number of threads for parallel GC operations. Increasing this value can be beneficial for applications running on multi-core processors, as it allows GC tasks to be processed faster. For instance, a high-throughput server application might benefit from more parallel GC threads to minimize pause times and improve overall performance.
70+
This specifies the number of threads for parallel GC operations. Increasing this value is beneficial for applications running on multi-core processors, as it allows GC tasks to be processed faster. For instance, a high-throughput server application might benefit from more parallel GC threads to minimize pause times and improve overall performance.
6771

6872
- -XX:G1HeapRegionSize
6973

70-
Determines the size of G1 regions, which must be a power of 2 between 1 MB and 32 MB. Adjusting this can be useful for applications with specific memory usage patterns. For example, setting a larger region size can reduce the number of regions and associated overhead for applications with large heaps, while smaller regions might be better for applications with more granular memory allocation patterns.
74+
This determines the size of G1 regions, which must be a power of 2 between 1 MB and 32 MB. Adjusting this can be useful for applications with specific memory usage patterns. For example, setting a larger region size can reduce the number of regions and associated overhead for applications with large heaps, while smaller regions might be better for applications with more granular memory allocation patterns.
7175

72-
You can refer to [this technical article](https://www.oracle.com/technical-resources/articles/java/g1gc.html) for more information of G1GC tuning.
76+
See [Garbage First Garbage Collector Tuning](https://www.oracle.com/technical-resources/articles/java/g1gc.html) for more information of G1GC tuning.
7377

content/learning-paths/servers-and-cloud-computing/java-gc-tuning/_index.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ title: Tune the Performance of the Java Garbage Collector
33

44
minutes_to_complete: 45
55

6-
who_is_this_for: This learning path is designed for Java developers aiming to optimize application performance on Arm-based servers. It is especially valuable for those migrating applications from x86-based to Arm-based instances.
6+
who_is_this_for: This Learning Path is for Java developers aiming to optimize application performance on Arm-based servers, especially those migrating applications from x86-based to Arm-based instances.
77

88
learning_objectives:
9-
- Understand the key differences among Java garbage collectors (GCs).
10-
- Monitor and interpret GC performance metrics.
9+
- Describe the key differences between individual Java Garbage Collectors (GCs).
10+
- Monitor and interpret Garbage Collector performance metrics.
1111
- Adjust core parameters to optimize performance for your specific workload.
1212

1313
prerequisites:
14-
- An Arm based instance from a cloud service provider, or an on-premise Arm server.
15-
- Basic understanding of Java and [Java installed](/install-guides/java/) on your machine.
14+
- An Arm-based instance from a cloud service provider, or an on-premise Arm server.
15+
- Basic understanding of Java.
16+
- An [installation of Java](/install-guides/java/) on your machine.
1617

1718
author_primary: Kieran Hejmadi
1819

content/learning-paths/servers-and-cloud-computing/java-gc-tuning/_review.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22
review:
33
- questions:
44
question: >
5-
What is the purpose of garbage collection?
5+
What is the purpose of Garbage Collection?
66
answers:
7-
- To manage memory by automatically reclaiming unused objects
8-
- To manually manage memory allocation
7+
- To manage memory by automatically reclaiming unused objects.
8+
- To manually manage memory allocation.
99
correct_answer: 1
1010
explanation: >
11-
Garbage collection is used to manage memory by automatically reclaiming memory occupied by objects that are no longer in use, thus preventing memory leaks and optimizing memory usage.
11+
Garbage Collection is used to manage memory by automatically reclaiming memory occupied by objects that are no longer in use, to prevent memory leaks and optimize memory usage.
1212
1313
- questions:
1414
question: >
15-
Which JVM flag can be used to enable detailed garbage collection logging?
15+
Which JVM flag can you use to enable detailed garbage collection logging?
1616
answers:
17-
- -XX:+UseG1GC
18-
- -XX:+PrintGCDetails
17+
- -XX:+UseG1GC.
18+
- -XX:+PrintGCDetails.
1919
correct_answer: 2
2020
explanation: >
2121
The flag -XX:+PrintGCDetails enables detailed logging of garbage collection events, which helps in monitoring and tuning the GC performance.
2222
2323
- questions:
2424
question: >
25-
Which garbage collector is best suited for applications requiring very low latency in a heavily multi-threaded application?
25+
Which Garbage Collector is best suited for applications requiring very low latency in a heavily multi-threaded application?
2626
answers:
27-
- Serial GC
28-
- ZGC
27+
- Serial GC.
28+
- ZGC.
2929
correct_answer: 2
3030
explanation: >
3131
ZGC (Z Garbage Collector) is designed for applications requiring very low latency, as it aims to keep pause times below 10 milliseconds even for large heaps.

0 commit comments

Comments
 (0)