You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/learning-paths/servers-and-cloud-computing/java-gc-tuning/Example_application.md
+27-17Lines changed: 27 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,16 @@
1
1
---
2
2
title: Example Application
3
-
weight: 4
3
+
weight: 5
4
4
5
5
### FIXED, DO NOT MODIFY
6
6
layout: learningpathall
7
7
---
8
8
9
-
## Example Application.
9
+
## Example Application
10
10
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.
12
14
13
15
```java
14
16
publicclassHeapUsageExample {
@@ -32,9 +34,13 @@ public class HeapUsageExample {
32
34
}
33
35
```
34
36
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.
36
42
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:
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.
50
56
51
57
You will now see a log file, named `gc.log` created within the same directory.
52
58
@@ -58,17 +64,19 @@ Open `gc.log` and the contents should look similar to:
58
64
[2024-11-08T15:04:54.350+0000][0.759s][info][gc] GC(3) Pause Young (Allocation Failure) 139M->3M(494M) 3.699ms
59
65
```
60
66
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.
62
68
63
69
- Frequency: ~ every 46 ms
64
70
- Pause duration: ~ 3.6 ms
65
71
- Reduction size: ~ 139 MB (or 3M objects)
66
72
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.
68
74
69
75
### Use jstat to observe real-time GC statistics
70
76
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:
@@ -107,13 +115,15 @@ Start the Java program with the command below. This will use the default paramet
107
115
```bash
108
116
java WhileLoopExample.java
109
117
```
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:
111
121
112
122
```bash
113
123
jstat -gcutil $(pgrep java) 1000
114
124
```
115
125
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:
117
127
118
128
```output
119
129
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.
125
135
```
126
136
127
137
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.
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:
12
14
13
15
```output
14
16
$ 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
22
24
23
25
In this section, you will use the `HeapUsageExample.java` file you created earlier.
24
26
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.
26
30
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:
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.
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.
40
44
41
-
### Add GC Targets
45
+
### Add Garbage Collector Targets
42
46
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.
44
48
45
49
Running the command with the `-XX:MaxGCPauseMillis=<N>` sets a target max GC pause time:
46
50
@@ -55,19 +59,19 @@ Looking at the output below, you can see that at the same initial state after ~0
55
59
[2024-11-08T16:27:37.149+0000][0.853s][info][gc] GC(19) Pause Young (Normal) (G1 Evacuation Pause) 193M->3M(514M) 0.482ms
56
60
```
57
61
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:
59
63
60
64
- -XX:InitiatingHeapOccupancyPercent:
61
65
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.
63
67
64
68
- -XX:ParallelGCThreads
65
69
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.
67
71
68
72
- -XX:G1HeapRegionSize
69
73
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.
71
75
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.
Copy file name to clipboardExpand all lines: content/learning-paths/servers-and-cloud-computing/java-gc-tuning/_index.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,17 @@ title: Tune the Performance of the Java Garbage Collector
3
3
4
4
minutes_to_complete: 45
5
5
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.
7
7
8
8
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.
11
11
- Adjust core parameters to optimize performance for your specific workload.
12
12
13
13
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.
Copy file name to clipboardExpand all lines: content/learning-paths/servers-and-cloud-computing/java-gc-tuning/_review.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,30 +2,30 @@
2
2
review:
3
3
- questions:
4
4
question: >
5
-
What is the purpose of garbage collection?
5
+
What is the purpose of Garbage Collection?
6
6
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.
9
9
correct_answer: 1
10
10
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.
12
12
13
13
- questions:
14
14
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?
16
16
answers:
17
-
- -XX:+UseG1GC
18
-
- -XX:+PrintGCDetails
17
+
- -XX:+UseG1GC.
18
+
- -XX:+PrintGCDetails.
19
19
correct_answer: 2
20
20
explanation: >
21
21
The flag -XX:+PrintGCDetails enables detailed logging of garbage collection events, which helps in monitoring and tuning the GC performance.
22
22
23
23
- questions:
24
24
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?
26
26
answers:
27
-
- Serial GC
28
-
- ZGC
27
+
- Serial GC.
28
+
- ZGC.
29
29
correct_answer: 2
30
30
explanation: >
31
31
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