All Products
Search
Document Center

Simple Log Service:Collect GC logs

Last Updated:Jan 25, 2024

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.

    • JDK 8 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 the Xloggc: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.

    -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.

    • JDK 11 options

      In JDK 11, the preceding options are unavailable, except for -Xloggc. JDK 11 introduces a new GC logging system referred to as Unified Logging. You can use -Xlog:gc*:file instead of -Xloggc:. This option allows you to manage the output formats and levels of logs in a fine-grained manner.

      -Xlog:gc*:file=gc-%p-%t.log:time,pid:filecount=5,filesize=10M
      Note

      %t specifies a point in time, %p specifies a process ID, filecount specifies the number of times that a file can be rotated, and filesize specifies the size of a file.

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 duration, and the number of objects that are collected. You can identify and handle the following common issues by analyzing GC logs:

  1. 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.

  2. 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.

  3. 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 information about the performance of 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.

  4. GC policy selection: JVMs provide multiple GC algorithms, such as Mark-Sweep, Copying, and 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.

image.png

Procedure

  1. Log on to the Simple Log Service console.

  2. In the Log Application section, click the Intelligent O&M tab. Then, click Full-stack Observability.

  3. On the Simple Log Service Full Stack Observability page, click the instance that you want to manage.

  4. In the left-side navigation pane, click Full-stack Monitoring.

    The first time you use Performance Monitoring in the instance, click Enable.

  5. In the left-side navigation tree, click Data Import. On the Data Access 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.

  6. 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.

      1. 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.

        Important

        If you want to collect logs from an ECS instance that belongs to a different Alibaba Cloud account, a server in an on-premises data center, or a server of a third-party cloud service provider, you must install Linux Logtail V0.16.40 or later. For more information, see Install Logtail on a Linux server. After you manually install Logtail, you must configure a user identifier for the server. For more information, see Configure a user identifier.

      2. After Logtail is installed, click Complete Installation.

      3. 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.

  7. 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.

    To generate topics, we recommend that you select File Path RegEx. 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 the pid 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 complete the configuration, Simple Log Service automatically creates assets such as Metricstores. For more information, see Assets.

GC metrics

Important

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.

  • USER: the time in the user state.

  • SYS: the time in the kernel state, including the I/O blocking time.

  • REAL: the actual execution time.

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.

  • Serial GC

  • Parallel GC

  • ZGC

  • Generational ZGC

  • Shenandoah GC

  • Generational Shenandoah GC

  • G1 GC

  • CMS GC

type

The GC type.

  • Young GC

  • Mixed GC

  • Full GC

  • Concurrent Mark Cycle

  • Concurrent Undo Cycle

  • CMS

  • Garbage Collection

GC subphase time (GC_SUBPHASE_TIME)

Label

Description

instance_id

The ID of the request.

gc_type

The GC algorithm.

  • Serial GC

  • Parallel GC

  • ZGC

  • Generational ZGC

  • Shenandoah GC

  • Generational Shenandoah GC

  • G1 GC

  • CMS GC

type

The GC type.

  • Young GC

  • Mixed GC

  • Full GC

  • Concurrent Mark Cycle

  • Concurrent Undo Cycle

  • CMS

  • Garbage Collection

subphase

The name of the GC subphase.

  • Important phases of CMS GC

    • Concurrent Mark

    • Final Remark

  • All phases of CMS GC

    • Initial Mark

    • Concurrent Preclean

    • Concurrent Abortable preclean

    • Concurrent Mark

    • Final Remark

    • Concurrent Sweep

    • Concurrent Reset

    • Concurrent Mode Interrupted

    • Concurrent Mode Failure

    • Rescan

    • Class unloading

    • Scrub Symbol Table

    • Scrub String Table

  • Important phases of G1

    • Concurrent Mark

    • Concurrent Mark From Roots

    • Pause Remark

    • Object Copy

  • All phases of G1 GC

    • Concurrent Clear Claimed Marks

    • Root Region Scanning

    • Concurrent Mark From Roots

    • Concurrent Preclean

    • Concurrent Mark

    • Concurrent Mark Reset For Overflow

    • Pause Remark

    • Concurrent Rebuild Remembered Sets

    • Pause Cleanup

    • Concurrent Cleanup

    • Finalize Marking

    • Class Unloading

    • Reference Processing

    • Concurrent Mark Abort

    • Mark Live Objects

    • Prepare for Compaction

    • Adjust Pointers

    • Compact Heap

    • Ext Root Scanning

    • Update Remember Set

    • Scan Remember Set

    • Code Root Scanning

    • Object Copy

    • Termination

    • Code Root Fixup

    • Code Root Purge

    • Clear Card Table

    • Choose Collection Set

    • Evacuation Failure

    • Ref Enq

    • Redirty Cards

    • Humongous Register

    • Humongous Reclaim

    • Free Collection Set

GC time (GC_COST_TIME)

Label

Description

instance_id

The instance ID.

gc_type

The GC type.

  • Serial GC

  • Parallel GC

  • ZGC

  • Generational ZGC

  • Shenandoah GC

  • Generational Shenandoah GC

  • G1 GC

  • CMS GC

Space

Memory before GC (BEFORE_GC_REGION_SIZE)

Label

Description

instance_id

The instance ID.

gc_type

The GC type.

  • Serial GC

  • Parallel GC

  • ZGC

  • Generational ZGC

  • Shenandoah GC

  • Generational Shenandoah GC

  • G1 GC

  • CMS GC

type

The area name.

  • Young

  • Old

  • Humongous

  • Heap

  • Metaspace

Memory after GC (AFTER_GC_REGION_SIZE)

Label

Description

instance_id

The instance ID.

gc_type

The GC type.

  • Serial GC

  • Parallel GC

  • ZGC

  • Generational ZGC

  • Shenandoah GC

  • Generational Shenandoah GC

  • G1 GC

  • CMS GC

type

The area name.

  • Young

  • Old

  • Humongous

  • Heap

  • Metaspace

Promotion size (GC_PROMOTION)

Label

Description

instance_id

The instance ID.

gc_type

The GC type.

  • Serial GC

  • Parallel GC

  • ZGC

  • Generational ZGC

  • Shenandoah GC

  • Generational Shenandoah GC

  • G1 GC

  • CMS GC

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.