You can collect garbage collection (GC) logs to the Full-stack Observability application. This way, you can monitor the logs in a visualized manner.
Prerequisites
A Full-stack Observability instance is created. For more information, see Create an instance.
The GC logging options are configured for Java processes and GC logs are collected to specific files. The following table describes the options.
-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Xloggc:gc-%p.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=100M
Option
Required
Description
-XX:+PrintGCDetails
Yes
Prints detailed GC logs.
-XX:+PrintGCDateStamps
Yes
Adds date timestamps to GC logs.
If you enable this option, the Java virtual machine (JVM) adds the date and time information to each GC log. The information can be used to analyze the time sequence of GC logs and help you understand the behavior and effects of GCs.
In most cases, this option is used together with other GC logging options, such as
-XX:+PrintGCDetails
and-XX:+PrintGCTimeStamps
, to provide more detailed GC logs.-XX:+PrintGCTimeStamps
Yes
Adds timestamps to GC logs.
If you enable this option, the JVM adds the timestamp information to each GC log, including the start time and end time. The information can be used to analyze the time sequence and performance of GCs and help you understand the GC behavior of applications and optimize performance.
In most cases, this option is used together with other GC logging options, such as
-XX:+PrintGCDetails
and-XX:+PrintGCDateStamps
, to provide more detailed GC logs.-Xloggc
Yes
Collects GC logs to a specified file. Format:
-Xloggc:gc-%p.log
.gc-%p.log
specfies a file path.In JDK 8, the
-Xloggc
option is valid and must be configured in theXloggc:gc-%p.log
format. In most cases, this option is used together with other GC logging options, such as-XX:+PrintGCDetails
and-XX:+PrintGCTimeStamps
, to provide more detailed GC logs.In JDK 11, the
-Xloggc
option is valid.JDK 11 introduces a new GC logging system referred to as Unified Logging. You can use-Xlog:gc=
instead of-Xloggc:
. This option allows you to manage the output formats and levels of logs in a fine-grained manner.
-XX:+UseGCLogFileRotation
No (recommended)
Enables rotation for GC log files.
If you enable this option, the JVM automatically rotates GC log files. If the size of a file reaches a specific value or a specific period of time elapses, the JVM automatically creates a log file and retains the previous log file. This prevents GC log files from becoming excessively large and occupying a large volume of disk space. The log file rotation feature helps you efficiently manage and analyze log files.
In most cases, this option is used together with other GC logging options, such as
-XX:GCLogFileSize
and-XX:GCLogFileSize
, to manage the rotation conditions and policies for log files.-XX:NumberOfGCLogFiles
No (recommended)
Specifies the maximum number of files that can be retained when GC log files are rotated.
If you configure this option, the JVM retains up to the specified number of GC log files. When the number of log files exceeds the value of this option, the earliest log file is deleted. This prevents a large number of log files from occupying a large volume of disk space.
In most cases, this option is used together with the
-XX:+UseGCLogFileRotation
option to manage the rotation of GC log files.-XX:GCLogFileSize
No (recommended)
Specifies the maximum size of a GC log file.
If you configure this option, the JVM monitors the size of each GC log file based on the value of this option. When the size of a log file exceeds the value of this option, the JVM automatically creates a new log file and retains the previous log file. This prevents a large number of log files from occupying a large volume of disk space.
In most cases, this option is used together with the
-XX:+UseGCLogFileRotation
option to manage the rotation of GC log files.
Background information
If an issue related to Java application performance occurs, you can analyze JVM GC logs to troubleshoot the issue. GC logs provide information about garbage collections, including the cause of GC triggers, GC type, GC , and the number of objects that are collected. You can identify and handle the following common issues by analyzing GC logs:
Memory leaks: You can obtain the creation and destruction process of objects and the allocation of objects in the heap memory by analyzing GC logs. If specific objects are not released after you use the objects, memory leaks may occur. You can check whether memory leaks occur, identify the cause of the memory leaks, and fix the issue at the earliest opportunity by analyzing GC logs.
High memory usage: GC logs show the memory usage of your application, including the heap memory usage and object distribution. You can optimize memory usage based on the objects that occupy a large volume of memory space by analyzing GC logs.
GC performance-related issues: GC logs provide information about garbage collections, including the cause of GC triggers, GC frequency, and GC duration. You can obtain the performance information about garbage collections, such as the GC frequency and GC duration, by analyzing GC logs. If the GC frequency is high or the GC duration is long, the performance of your application may be affected. You can also identify the causes of issues that are related to GC performance and use GC logs to fix the issues.
GC policy selection: JVMs provide multiple GC algorithms, such as Mark-Sweep, Copying, Mark-Compact. Analysis of GC logs allows you to evaluate the performance of each GC algorithm and select the appropriate GC policy. This helps improve the stability of your application.
Data flow
The following figure shows the data flow of GC logs.
Procedure
Log on to the Simple Log Service console.
In the Log Application section, click the Intelligent O&M tab. Then, click Full-stack Observability.
On the Simple Log Service Full-stack Observability page, click the instance that you want to manage.
In the left-side navigation pane, click Full-stack Monitoring.
The first time you use Full-stack Monitoring in the instance, click Enable.
In the left-side navigation pane, click Data Import. On the Data Import Configurations page, find GC Log Analysis in the JVM Monitoring section.
The first time you create a Logtail configuration for host monitoring data, turn on the switch to go to the configuration page. If you created a Logtail configuration, click the
icon to go to the configuration page.
Create and select a machine group.
If a machine group is available, click Use Existing Machine Groups.
If no machine groups are available, perform the following steps to create a machine group. In this example, an Elastic Compute Service (ECS) instance is used.
On the ECS Instances tab, select Manually Select Instances. Then, select the ECS instance that you want to use and click Create.
For more information, see Install Logtail on ECS instances.
ImportantIf your server is an ECS instance that belongs to another Alibaba Cloud account, a server that is provided by a third-party cloud service provider, or a server that is deployed in a self-managed data center, you must install Linux Logtail V0.16.40 or later on the server. For more information, see Install Logtail on a Linux server. After you install Logtail, you must configure a user identifier for the server. For more information, see Configure a user identifier.
After Logtail is installed, click Complete Installation.
In the Create Machine Group step, configure the Name parameter and click Next.
Simple Log Service allows you to create IP address-based machine groups and custom identifier-based machine groups. For more information, see Create an IP address-based machine group and Create a custom identifier-based machine group.
In the Specify Data Source step, configure the parameters and click Complete.
The following table describes the parameters. Retain the default settings for the other parameters.
Parameter
Description
Example
Config Name
The name of the Logtail configuration.
host-szytbsxv
Log Path
The path of GC log files.
/app/sls-mall/*.gc*
Docker File
If the Java process runs in a Docker container or a Kubernetes cluster, you must select Docker File.
Yes
Deployed in K8s
If the Java process is deployed in a Kubernetes cluster, select Deployed in K8s.
Yes
Mode
The log file collection mode. Select Simple Mode.
Simple Mode
Topic Generation Mode
If you deploy multiple Java processes in the same host or container, you can use topics to identify the GC logs of each Java process.
We recommend that you select File Path RegEx to generate topics. If you select this method, you must add the
%p
parameter to the name of the JVM GC file to record Java process IDs. Example:-Xloggc:gc-%p.log
. After you configure the setting, the system adds thepid
suffix to each GC file name. Example:gc-pid1.log.0.current
.File Path RegEx
Custom Regular Expression
The regular expression that is used to generate topics. For more information, see Log topics.
By default, Full-stack Observability performs association analysis based on the
pid
field. We recommend that you set the named capturing group in the regular expression to (?P<pid>)./sls-mall/gc-pid(?P<pid>\d*).log.*
After you configure the settings, Simple Log Service automatically creates assets such as Metricstores. For more information, see Assets.
GC metrics
The following GC metrics are generated by using the Scheduled SQL feature of Simple Log Service.
GC metrics are categorized based on the following dimensions: time and space.
Time-based GC metrics: specify the duration of each GC subphase and the total duration of a GC phase.
Space-based GC metrics: specify the size of each area before and after each GC phase and the change of space promotion in each area.
Time
CPU time of a GC thread (GC_CPU_USED)
Label | Description |
instance_id | The instance ID. |
gc_type | The GC type. Example: G1, CMS, and ZGC. |
type | The type of the GC duration.
|
GC pause time (GC_PAUSE_TIME)
During a GC process, specific threads concurrently run and do not affect your application. The pause time is calculated by subtracting the concurrency time from the duration. The GC pause time affects your application.
Label | Description |
instance_id | The instance ID. |
gc_type | The GC algorithm.
|
type | The GC type.
|
GC subphase time (GC_SUBPHASE_TIME)
Label | Description |
instance_id | The instance ID. |
gc_type | The GC algorithm.
|
type | The GC type.
|
subphase | The name of the GC subphase.
|
GC time (GC_COST_TIME)
Label | Description |
instance_id | The instance ID. |
gc_type | The GC type.
|
Space
Memory before GC (BEFORE_GC_REGION_SIZE)
Label | Description |
instance_id | The instance ID |
gc_type | The GC type.
|
type | The area name.
|
Memory after GC (AFTER_GC_REGION_SIZE)
Label | Description |
instance_id | The instance ID. |
gc_type | The GC type.
|
type | The area name.
|
Promotion size (GC_PROMOTION)
Label | Description |
instance_id | The instance ID. |
gc_type | The GC type.
|
What to do next
After JVM monitoring data is collected to Simple Log Service, the Full-stack Monitoring application automatically creates dedicated dashboards for the monitoring data. You can use the dashboards to analyze the monitoring data. For more information, see View dashboards.