Use Go memory diagnostics to analyze and profile memory usage in your Go processes.
Overview
Go memory diagnostics is a key feature of memory diagnostics. It provides a comprehensive analysis of Go processes, including heap memory, non-heap memory, goroutines, and threads, to help you pinpoint issues such as memory leaks, memory fragmentation, and high non-Go heap usage.
Use cases
Scenario | Description |
High Go heap memory usage | The heap memory ( |
Delayed Go memory release or fragmentation | Go memory is not returned to the OS promptly, resulting in an abnormally high proportion of |
High non-Go heap usage | The non-heap memory ( |
Prerequisites
A Go application is installed and running on the target instance.
You have the PID of the target Go process.
If you are a RAM user, make sure your root account has granted the
AliyunECSReadOnlyAccessandAliyunSysomFullAccesspolicies to the RAM user.You have permissions to use SysOM.
Procedure
Log on to SysOM. In the top-left corner of the page, select the region where the target instance is located.
In the left-side navigation pane, click system diagnostics.
Set Diagnosis Mode to node diagnostics. In the Diagnostic Type list, select memory diagnostics. In the list of diagnostic items, select Go memory diagnostics.
Configure the diagnostic parameters:
Parameter
Description
Required
Instance ID
The ID of the target ECS instance.
Yes
PID
The PID of the target Go process.
Yes
After configuring the parameters, click Run Diagnosis. The system automatically runs the diagnosis and generates a report.
In the Diagnostic Records section, click View Report in the Actions column to open the diagnostic report.
Diagnostic report
Basic information
The Basic information section displays basic information about the diagnosis, including:
Field | Description |
Diagnosed instance ID | The resource ID of the diagnosed instance. |
Diagnostic item | Go memory diagnostics (gomemdump) |
Report ID | The unique identifier for this diagnosis. |
Start time | The time when the diagnostic task was initiated. |
Go version | The Go version used by the target process. |
Executable file | The path of the executable file for the Go process. |
PID | The process ID of the target Go process. |
Completion time | The time when the diagnostic task was completed. |
Conclusion
This section summarizes the diagnostic findings and identifies the likely causes of memory usage issues. Possible conclusions include:
High Go heap memory usage: The
inUseSpanproportion of the Go process is abnormally high, which may indicate a memory leak or excessive object allocation.Delayed Go memory release or fragmentation: Go memory is not released promptly, resulting in an abnormal proportion of
deadSpanormanualSpan.High non-Go heap usage: The proportion of non-anonymous page memory (
nonAnonPageMem) is excessively high, indicating a large number of file mappings or CGO calls.System status is normal, no intervention required: The memory usage of the Go process is normal with no significant anomalies.
Suggestions
This section provides suggestions based on the diagnostic results. The following are suggestions for common scenarios:
High Go heap memory usage
Check for unreleased large objects or goroutine leaks.
Use pprof for in-depth analysis to locate memory allocation hotspots.
Use
sync.Poolto reuse objects.Check for unnecessary closure captures that prevent memory from being released.
Delayed Go memory release or fragmentation
Adjust the
GODEBUGparameter, for example, by settingmadvdontneed=1to return memory to the OS more quickly.Review code that frequently allocates and frees small objects.
Use
sync.Poolto reduce memory fragmentation.Periodically trigger
runtime.GC()to accelerate garbage collection.
High non-Go heap usage
Check for unreleased memory from large
mmapfile mappings.Analyze memory allocation from CGO calls.
Check for unclosed file descriptors.
Optimize file reading methods to avoid loading entire files into memory.
Details
The Details section provides a detailed breakdown of memory usage, covering the following areas:
1. Go process information
Displays basic information about the Go process:
PID: The ID of the Go process.
Go Version: The runtime Go version (for example, go1.20.5).
Executable file path: The full path of the executable file for the Go process.
Diagnostic time: The time when the diagnosis was performed.
2. Go memory usage details
Displays the details of memory usage for the Go process:
Operating system memory usage
Memory allocation within the Go process
Memory allocation in non-anonymous pages
FAQ
How do I get the PID of a Go process?
You can get the PID of a Go process using the following commands:
# Method 1: Find by process name
ps aux | grep <go_program_name>
# Method 2: Find by port (if the Go program listens on a specific port)
lsof -i :<port_number>
# Method 3: If the program prints its own PID
# Add the following code at program startup: fmt.Printf("PID: %d\n", os.Getpid())What is the difference between Go memory diagnostics and OOM diagnostics?
OOM diagnostics: Analyzes Out of Memory (OOM) events to determine their causes, such as insufficient system memory or exceeding cgroup memory limits.
Go memory diagnostics: Analyzes memory usage in a running Go process. You can run it on a specified Go process at any time without waiting for an OOM event.
Which Go versions does Go memory diagnostics support?
Go memory diagnostics supports Go 1.18 and later. For best results, we recommend using the latest stable version of Go.
Will the diagnosis affect the running Go process?
The diagnostic process reads information from the /proc file system and Go runtime debugging data, which has a minimal performance impact on the Go process.
What should I do if the diagnosis fails?
Verify the following:
Make sure that SysOM is correctly installed on the target instance.
Verify that the PID corresponds to an existing Go process.
Ensure that the current user has sufficient permissions to access the target instance.
Check the error messages in the diagnostic records and troubleshoot based on the information provided.