Thread dump file capture and analysis (JCA tool)

Thread dump file capture and analysis

Next, analyze how to capture Thread dump files under CentOS and how JCA analyzes Thread dump files.

1. Capture Thread dump files under CentOS

The command under CentOS
will print out additional lock information. When a deadlock occurs, you can use jstack -l pid to observe the lock status.

jstack -l pid

Not only will Java stack information be output, but C/C++ stack information will also be output.

jstack -m pid

Output the abnormal process Thread Core file.

jstack -l pid >> /root/*.txt

Execute "jstack process number | grep thread ID" to find the status of threads under a certain process.

Insert image description here

2. JCA tool analyzes Thread dump file

2.1 Thread status

New: The state that exists when the thread object is created, and the thread cannot be executed at this time;
Runnable : When thread.start() is called, the thread becomes Runnable state. As long as the CPU is obtained, it will be executed;
Running : the thread is executing;
Waiting : executing thread.join() or calling obj.wait() on the lock object will enter this state, indicating that the thread is waiting for a resource or condition to occur. To wake yourself up;
Timed_Waiting : Executing Thread.sleep(long), thread.join(long) or obj.wait(long) will enter this state. The difference from Waiting is that Timed_Waiting’s waiting has a time limit; Blocked:
If you enter If the synchronization method or synchronization code block does not acquire the lock, it will enter this state;
Dead : After the thread completes execution, or an uncaught exception is thrown, it will enter the dead state, indicating that the thread has ended.
Secondly, for the jstack log, we Pay special attention to the following key information :
Deadlock : Indicates a deadlock.
Waiting on condition : Waiting for a resource or condition to occur to wake yourself up. The specific analysis needs to be combined with jstacktrace. For example, the thread is sleeping, the network is busy reading and writing, and waiting for
Blocked : Blocking
Waiting on monitor entry : Waiting to acquire the lock
in Object.wait():After acquiring the lock, execute obj.wait() to give up the lock

Generally focus on "Waiting on condition", "wait()", and "Blocked". These are caused by high CPU and may be caused by an infinite loop in thread execution.

2.2 Specific examples

Use the following large number of new objects to simulate the case of "heap memory overflow".

   List<Object> list = new ArrayList<>();
            for (int i = 0; i < 10000000; i++) {
                String str = "";
                for (int j = 0; j < 1000; j++) {
                    str += UUID.randomUUID().toString();
                }
                list.add(str);
            }

Download Jca.jar. Place the jar package in the jdk/bin directory.
Enter the following command to open the JCA analysis tool
.../bin>java –jar jca433.jar

The tool page pops up, File—>Open Thread dumps, find the file.
Insert image description here
-----Generally for JavaCore files, we need multiple files for analysis. By comparison, we can find out which line of code has the problem.
From the above thread status analysis, we can see that there is an exception in line 21 of the corresponding code, and the other 51% of the CPU is "waiting for resources Waiting on condition", which may be due to problems such as an infinite loop in the code. Can be investigated in detail.

Guess you like

Origin blog.csdn.net/xunmengyou1990/article/details/108013251