Chapter 8 JVM performance monitoring and troubleshooting tools (2), jvm troubleshooting
Note: This blog is mainly recorded in "deep understanding of Java Virtual Machine (version 2)"
Note: For details about JVM performance monitoring and troubleshooting tools on the command line, see Chapter 7 JVM performance monitoring and troubleshooting tools (1).
1. Graphic troubleshooting tools
2. Jconsole
Go to "E: \ Java \ jdk1.6 \ bin" and double-click "jconsole.exe". The following dialog box is displayed:
Note: All JVM processes, one Jconsole process, and one eclipse (PID: 4684) are listed here, which is equivalent to the jps command.
Select a PID. If eclipse is selected, double-click the PID and the following result is displayed: (Note: each leaf sign is refreshed every 4 seconds)
"Memory": equivalent to jstat-gc. In the details section, the information corresponding to this section is the parameters written in the header Chart Section (this is the case of "entire Heap ), at the same time, it corresponds to the column ("Heap") selected in the bar chart in the lower right corner. For "non-heap", it refers to "Method Area" (or "permanent generation "). Of course, you can also select a time range to view the corresponding information.
"Class": equivalent to jstat-class, which lists information about the loading and unloading classes.
"Thread": equivalent to jstack. The line chart shows the number of threads, including the number of peak threads and the number of active threads. The lower left corner shows the names of all threads. Double-click the corresponding thread name
"VM summary": equivalent to jinfo
Finally, testThread deadlock. The Code is as follows:
1 package thread; 2 3/** 4 * test thread 5 */6 class XXthread implements Runnable {7 int a, B; 8 9 public XXthread (int a, int B) {10 this. a = a; 11 this. B = B; 12} 13 14 public void run () {15 synchronized (Integer. valueOf (a) {16 synchronized (Integer. valueOf (B) {17 System. out. println (a + B); 18} 19} 20} 21} 22 23 public class TestDeadLockThread {24 public static void main (String [] args) {25 for (int I = 0; I <100; I ++) {26 new Thread (new XXthread (1, 2 )). start (); 27 new Thread (new XXthread (2, 1 )). start (); 28} 29} 30}View Code
Run the main () method, view the "Thread" label, and click "detect deadlock", as shown below:
Thread-95 and Thread-106 deadlocks found (each has the lock the other wants)
Analysis:
1) Integer Cache Mechanism
Integer. valueOf (int xxx). To reduce object creation and reduce memory usage, this method caches the Integer object converted from xxx, this method will retrieve the object from the cache directly. Assume the Integer in the code. valueOf (1) is generated by java. lang. integer @ 987197, while Integer. valueOf (2) is generated by java. lang. integer @ 15e293a, then no matter how many times Integer is called. valueOf (1), no matter which thread calls this method, only the same object java is returned. lang. integer @ 987197. That is, the Integer in the above Code. valueOf (I) only generates two different objects, namely, java. lang. integer @ 987197 and java. lang. integer @ 15e293a, and these two objects are our lock objects.
2) deadlock occurrence time
Assume that the Thread "Thread-95" is executed to its first synchronized block (assuming that the Lock Object java has just been obtained. lang. integer @ 987197). At this time, the CPU time slice is switched to the Thread "Thread-106", and "Thread-106" executes the first synchronized block (obtaining the Lock Object java. lang. integer @ 15e293a), and then "Thread-106" will execute the second synchronized block to get the Lock Object java. lang. integer @ 987197, this time cannot be obtained, because this lock object is being held by "Thread-95", so "Thread-106" is blocked in java. lang. integer @ 987197 on the lock object, assuming that the CPU time slice is switched to "Thread-95", the Thread needs to execute the second synchronized block to obtain java. lang. integer @ 15e293a cannot be obtained because the lock object has been held by "Thread-106 ".
3) Results
"Thread-95" holds the Lock Object java. lang. Integer @ 987197, blocking the Lock Object java. lang. Integer @ 15e293a;
"Thread-106" holds the Lock Object java. lang. Integer @ 15e293a, blocking in the Lock Object java. lang. Integer @ 987197
3. visualVM
It is a more comprehensive GUI monitoring tool, including many plug-ins (you need to download them yourself). For details, see Java Virtual Machine (version 2).