2023-06-08 01:23:38 +08:00
|
|
|
---
|
|
|
|
title: "Debugging by Jattach"
|
|
|
|
hide_title: true
|
|
|
|
---
|
2025-04-16 16:55:51 -07:00
|
|
|
|
2023-06-08 01:23:38 +08:00
|
|
|
We have installed jattach in Docker image of datahub-gms, datahub-mae-consumer, datahub-mce-consumer
|
|
|
|
and datahub-frontend to facilitating troubleshooting in a production environment.
|
|
|
|
|
|
|
|
# Introduction to Jattach
|
2025-04-16 16:55:51 -07:00
|
|
|
|
2023-06-08 01:23:38 +08:00
|
|
|
Jattach is a utility to send commands to a JVM process via Dynamic Attach mechanism.
|
|
|
|
|
|
|
|
Supported commands:
|
2025-04-16 16:55:51 -07:00
|
|
|
|
|
|
|
- load : load agent library
|
|
|
|
- properties : print system properties
|
2023-06-08 01:23:38 +08:00
|
|
|
- agentProperties : print agent properties
|
2025-04-16 16:55:51 -07:00
|
|
|
- datadump : show heap and thread summary
|
|
|
|
- threaddump : dump all stack traces (like jstack)
|
|
|
|
- dumpheap : dump heap (like jmap)
|
|
|
|
- inspectheap : heap histogram (like jmap -histo)
|
|
|
|
- setflag : modify manageable VM flag
|
|
|
|
- printflag : print VM flag
|
|
|
|
- jcmd : execute jcmd command
|
2023-06-08 01:23:38 +08:00
|
|
|
|
|
|
|
# Use examples
|
|
|
|
|
|
|
|
Jattach is a All-in-one **jmap + jstack + jcmd + jinfo** functionality in a single tiny program.
|
|
|
|
The scenarios where these commands were previously used can now be replaced with jattach.
|
|
|
|
|
|
|
|
## Example 1: Dump heap
|
2025-04-16 16:55:51 -07:00
|
|
|
|
2023-06-08 01:23:38 +08:00
|
|
|
The jattach dumpheap command is typically used in the following scenarios:
|
|
|
|
|
|
|
|
- Memory leak analysis: When an application experiences memory leaks or abnormal memory growth, the jattach dumpheap
|
2025-04-16 16:55:51 -07:00
|
|
|
command is used to generate a heap dump file for further analysis and identification of the memory leak causes.
|
2023-06-08 01:23:38 +08:00
|
|
|
|
|
|
|
- Memory analysis: By generating a heap dump file, various memory analysis tools such as Eclipse Memory Analyzer and
|
2025-04-16 16:55:51 -07:00
|
|
|
VisualVM can be utilized to analyze objects, reference relationships, and memory usage within the heap. This helps
|
|
|
|
identify potential performance issues and optimization opportunities.
|
2023-06-08 01:23:38 +08:00
|
|
|
|
2025-04-16 16:55:51 -07:00
|
|
|
- Troubleshooting: When encountering frequent Full GCs, memory out-of-memory errors, or other memory-related issues,
|
|
|
|
generating a heap dump file provides additional information for troubleshooting and debugging purposes.
|
2023-06-08 01:23:38 +08:00
|
|
|
|
2025-04-16 16:55:51 -07:00
|
|
|
- Performance optimization: By analyzing the heap dump file, it becomes possible to identify which objects in the
|
|
|
|
application consume a significant amount of memory. This information can be used for targeted performance optimizations,
|
|
|
|
such as reducing object creation or optimizing cache usage.
|
2023-06-08 01:23:38 +08:00
|
|
|
|
2025-04-16 16:55:51 -07:00
|
|
|
It's important to note that generating a heap dump file can have an impact on the performance of the application.
|
2023-06-08 01:23:38 +08:00
|
|
|
Therefore, caution should be exercised when using it in a production environment, ensuring sufficient resources and
|
|
|
|
permissions are available for the operation.
|
|
|
|
|
|
|
|
The command is as follows:
|
2025-04-16 16:55:51 -07:00
|
|
|
|
2023-06-08 01:23:38 +08:00
|
|
|
```bash
|
|
|
|
jattach <pid> dumpheap /tmp/heap.bin
|
|
|
|
```
|
|
|
|
|
|
|
|
## Example 2: Dump thread
|
2025-04-16 16:55:51 -07:00
|
|
|
|
2023-06-08 01:23:38 +08:00
|
|
|
The jattach threaddump command is typically used in the following scenarios:
|
|
|
|
|
2025-04-16 16:55:51 -07:00
|
|
|
- Deadlock analysis: When an application experiences deadlocks or thread contention issues, the jattach threaddump
|
|
|
|
command is used to generate a thread dump file. It provides the current thread's stack trace information, helping to
|
|
|
|
identify the code paths and lock contention causing the deadlock.
|
2023-06-08 01:23:38 +08:00
|
|
|
|
|
|
|
- Thread troubleshooting: When threads in an application encounter exceptions, long blocking times, excessive CPU usage,
|
2025-04-16 16:55:51 -07:00
|
|
|
or other issues, generating a thread dump file provides detailed information about each thread's state, stack trace, and
|
|
|
|
resource usage. This helps with troubleshooting and analysis.
|
2023-06-08 01:23:38 +08:00
|
|
|
|
|
|
|
- Performance analysis: By generating a thread dump file, it becomes possible to understand the thread activity, call
|
2025-04-16 16:55:51 -07:00
|
|
|
paths, and resource contention within the application. This aids in identifying potential performance bottlenecks and
|
|
|
|
concurrency issues.
|
2023-06-08 01:23:38 +08:00
|
|
|
|
|
|
|
- Monitoring and diagnostics: Regularly generating thread dump files can be used to monitor the health of an application
|
2025-04-16 16:55:51 -07:00
|
|
|
and provide historical data for subsequent diagnostics. This helps gain a better understanding of the application's
|
|
|
|
behavior and performance.
|
2023-06-08 01:23:38 +08:00
|
|
|
|
|
|
|
It's important to note that generating a thread dump file may impact the performance of the application. Therefore,
|
|
|
|
caution should be exercised when using it in a production environment, ensuring sufficient resources and permissions
|
|
|
|
are available for the operation.
|
|
|
|
|
|
|
|
The command is as follows:
|
2025-04-16 16:55:51 -07:00
|
|
|
|
2023-06-08 01:23:38 +08:00
|
|
|
```bash
|
|
|
|
jattach <pid> threaddump -l > /tmp/jstack.out
|
2025-04-16 16:55:51 -07:00
|
|
|
```
|