Introduction to JDK Tools

Source: Internet
Author: User
Tags visualvm jconsole

The JDK provides us with some very useful gadgets to help us locate some simple JVM problems, which are under Jdk/bin. As a separate look, the tools in this article are the versions of Windows that are used.

1.jps

From the name you can probably see what it is to do, to understand that Liunx is aware of a very important command ps--lists the processes in the current system. The same JPS is the Java version of PS, which lists the Java processes in the current system, and the following is the result of executing JPS on my native machine:

c:\users\administrator>jps42809532 Jps
The preceding number represents the Java process number, the first is the process of my native eclipse, and the second is the process number of the JPS command itself, because the JPS command is also implemented in Java, JPS represents the entry class for the process. The JPS command is simple and there is not much to say.

2.jstat

JSTAT-GC PID 1000 10

Indicates that 10 GC messages are printed at 1-second intervals, and the following is the GC log that I performed for the JSTAT-GC 4280 output:

s0c    s1c    s0u    s1u      EC       EU        OC         OU       PC     PU    ygc     ygct FGC FGCT GCT4544.0 4544.0  0.0    0.0   36736.0 3905.0 91488.0    45833.2     ?      ?    0.660    199    38.241   38.902
S0C represents the current survivor space 0 capacity. Current survivor Space 0 capacity (KB), s1c refers to the capacity of Survivor Space 1 (this is 4.5M).

S0U represents the utilization of survivor Space 0. Survivor Space 0 Utilization (KB), similarly s1u.

The current Cenozoic Eden space capacity of the EC. Current Eden Space Capacity (KB). This is 36M.

The new generation of Eden space utilization in the EU. Eden Space Utilization (KB). Less than 4M is used here.

OC current age generation space capacity. Current old space Capacity (KB). Dead here, 91M.
OU old generation utilization situation. Old space Utilization (KB). 45M is used here.

The current living space capacity of the PC. Current permanent space Capacity (KB). Here is the question mark, suspected with my machine to install the JDK version, my native JDK is 1.8, 1.8 should not have the concept of permanent generation.
The space utilization of PU immortal generation. Permanent Space Utilization (KB).

YGC the number of new generation GC events. Number of young generation GC Events. You can see that the YGC happened 39 times.

YGCT generation GC time consuming. Young generation garbage collection time. 39 times YGC takes 0.66 seconds altogether.
FGC full GC count. Number of full GC events. The FULLGC occurred 199 times, and the full GC took place too frequently.
FGCT full GC time consuming. Full garbage collection time. 199 Full GC bows time 38 seconds
GCT Total GC time consuming. Total garbage collection time. The GC takes nearly 39 seconds to complete.

Jstat-gccapacity PID can see the use of three generations of VM heap memory

NGCMN    ngcmx     NGC     s0c   s1c       EC      ogcmn      ogcmx       OGC            OC      pgcmn    pgcmx     PGC       PC     ygc    FGC 13632.0 174720.0  45824.0 4544.0 4544.0  36736.0   27328.0   349568.0    91488.0    91488.0        ?       ???    228
NGCMN represents the minimum Cenozoic capacity, NGCMX represents the maximum Cenozoic capacity, NGC represents the current Cenozoic capacity, the same s0c,s1c,ec,ogcmn,ogcmx,ogc,oc and so on.

Jstat-gcutil PID can see the current generation of utilization and GC time

  S0     S1     E      O      P     ygc     ygct    FGC    fgct  GCT 0.00 0.00 5.98 50.81    ?      0.660   237   45.433   46.093
For example, the total time for S0 utilization is 0,GC is 46 seconds.

Similar to the Jstat-gccause PID more than the-gcutil two columns are as follows:

   S0     S1     E      O      P     ygc     ygct    FGC    fgct     GCT    lgcc         GCC  0.00   0.00   6.54  50.81     ?    0.660   245   46.892   47.552 System.GC ()   No GC
The extra two columns represent the reason for the last GC and the reason for this GC, and you can see that the LGCC reason is System.GC (), which is caused by the code's active call to the GC, which can be-XX:+DISABLEEXPLICITGCSuppresses the display of GC actions.

There are some parameters such as-gcnew-gcold and so on.

3.jinfo

The jinfo is used to view and configure the JVM's startup parameters in real time. jdk1.8 version print out a lot of information, it is not posted out. In addition the win version of the Jinfo command some features are not provided

4.jmap

The Jmap command allows you to dump a heap into a snapshot (called heapdump or dump), setting the virtual machine startup parameters-xx:+heapdumponoutofmemoryerror and-xx:heapdumppath=${directories} outside of the command Specifies that a heap snapshot is generated in the specified directory when oom occurs. The same effect can be achieved with the kill-3 command under Liunx.

Jmap-histo PID Prints the number of instances per class, memory consumption, class full name information. The internal class name of the VM is prefixed with "*". If the live child parameter is added, only the number of live objects is counted.

The following is a one-step classification instance of the eclipse process.

Num     #instances         #bytes  class name----------------------------------------------   1:        183057       19031952  [C   2:         81140        5199880  [B   3:        153279        2452464  java.lang.String
Jmap-heap PID-heap Prints the summary of the heap, the algorithm used by the GC, the configuration of the heap, and the use of the wise heap.

Here is the details of my eclipse:

Attaching to process ID 4280, please wait ... Debugger attached successfully. Client compiler detected. JVM version is 25.11-b03using thread-local object allocation. Mark Sweep Compact gcheap configuration:minheapfreeratio = 40\\gc idle heap less than 40% expands heap Maxheapfreeratio = 70 \\GC after idle heap is greater than 70% reduced heap maxheapsize = 536870912 (512.0MB) NewSize = 13959168 (13.3125MB) Ma                 Xnewsize = 178913280 (170.625MB) oldsize = 27983872 (26.6875MB) newratio = 2 Survivorratio = 8 Metaspacesize = 12582912 (12.0MB) \ \ Meta-space, replacement for permanent generation Compressedclassspacesiz  E = 1073741824 (1024.0MB) maxmetaspacesize = 4294901760 (4095.9375MB) g1heapregionsize = 0 (0.0MB) Heap Usage:new Generation (Eden + 1 Survivor Space): capacity = 42270720 (40.3125MB) used = 1896728 (1.808860778808593 8MB) free = 40373992 (38.503639221191406MB) 4.487096505571706% Usededen space:capacity = 37617664 (35.875MB) used = 1896728 (1.8088607788085938MB) free = 35720936 (34.066139221191406MB) 5.04212063779399% Usedfrom space:capacity = 4653056 (4.4375MB) used = 0 (0.0MB) free = 4653056 (4.4375MB) 0.0% usedto Space   : Capacity = 4653056 (4.4375MB) used = 0 (0.0MB) free = 4653056 (4.4375MB) 0.0% usedtenured generation:   Capacity = 93683712 (89.34375MB) used = 47601728 (45.39654541015625MB) free = 46081984 (43.94720458984375MB) 50.811103642007694% used22825 interned Strings occupying 1880328 bytes.
Jmap-dump:format=b,file=c pid dumps heap memory to file C

The dump file that analyzes the Jmpa dump can use the Jhat tool that comes with the JDK, and executes the command to jhat-j-xmx1024m c to specify that the memory size of the dump file is 1024M file C, and the output of the analysis is finished

Started HTTP server on port 7000
Server is ready.

We can see the results of the analysis by entering http://localhost:7000 in the browser.

In addition to the Jhat tool you can use some other tools to analyze such as memory Analyzer. However, it is generally not recommended to use their own jhat to analyze.

5.jstack

The Java stack Trace tool, which is used to generate a thread snapshot of the current JVM process. The following is part of my native Eclipse thread stack snapshot:

2015-03-07 23:17:03full thread Dump Java HotSpot (TM) Client VM (25.11-b03 mixed mode): "Worker-61" #478 prio=5 os_prio=0 ti d=0x288b2400 nid=0x18a4 in Object.wait () [0x29f1f000] Java.lang.Thread.State:TIMED_WAITING (on Object monitor) at JAVA.L Ang. Object.wait (Native Method)-Waiting on <0x0ed4f5a8> (a org.eclipse.core.internal.jobs.WorkerPool) at Org.eclipse.core.internal.jobs.WorkerPool.sleep (workerpool.java:188)-Locked <0x0ed4f5a8> (a Org.eclipse.core.internal.jobs.WorkerPool) at Org.eclipse.core.internal.jobs.WorkerPool.startJob (Workerpool.java : Up to Org.eclipse.core.internal.jobs.Worker.run (worker.java:50) "Worker-60" #468 prio=5 os_prio=0 tid=0x288b0400 Nid=0x20c4 in Object.wait () [0x2d8df000] Java.lang.Thread.State:TIMED_WAITING (on Object monitor) at JAVA.LANG.OBJECT.W AIT (Native Method)-Waiting on <0x0ed4f5a8> (a org.eclipse.core.internal.jobs.WorkerPool) at Org.eclipse.core.internal.jobs.WorkerPool.sleep (workerpool.java:188)-locked <0x0ed4f5a8> (a org.Eclipse.core.internal.jobs.WorkerPool) at Org.eclipse.core.internal.jobs.WorkerPool.startJob (workerpool.java:220 ) at Org.eclipse.core.internal.jobs.Worker.run (worker.java:50)
You can see information such as the thread name, thread number, thread status, and so on.

6.JConsole

The Jconsole is a graphical interface monitoring tool provided by the JDK that can display information about various aspects of the JVM, such as current memory, CPU usage, individual heap conditions, and so on.


The main content includes the six aspects of generalization, memory, threading, class, VM Summary, Mbean. Detailed content you can open a monitoring tool for yourself.

7.Java VisualVM

The Java VISUALVM tool is the most powerful monitoring and troubleshooting tool currently available with the JDK, compared to professional tools such as Jprofiler, and it has a great advantage of not needing to be monitored by a program that is running on a special agent, Therefore, it has little impact on the actual performance of the application and can be applied directly to the production environment. And the tool is based on plug-in mode, you can install a lot of powerful plug-ins to add functionality.

Here is one of the VISUALVM:


The graph shows the results of a performance analysis, and gives the hot-spot method and the number of call times for each method and so on.

VISUALVM can not only connect the native JVM process, but also connect to the remote JVM process. "Local" in the show is the native of all JVM processes, select a process right-click can complete thread dump, process dump, generate application snapshot.

Detailed features and introduction direct access to the VISUALVM Getting Started Guide

Below we explain the use of a plug-in Btrace Workbench, Btrace function is to not stop the target program to run the premise, through the hotspot virtual machine HotSwap technology to dynamically insert the original non-existent debugging code. For example, when we encounter a problem with our program, and there are not enough print statements, our general approach is to have to stop the service, then modify the code, add the print statement, recompile re-run to solve, the efficiency is very low. Btrace is actually Java code, just a few more annotations. Here is an example where the main program is as follows:

Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;public class BTraceTest { public static void Main (string[] args) throws IOException {btracetest t = new btracetest (); BufferedReader br = new BufferedReader (new InputStreamReader (system.in)); for (int i = 0;i<10;i++) {br.readline (); int a = (int) math.round (math.random () *), int b = (int) math.round (math.random () * 1000); System.out.println (T.add (A, b));}} public int Add (int a,int b) {return a+b;}}
For example, the runtime we want to know the current value of a, B and a+b, using the Btrace script can get the desired results when the program runs, first install the Btrace Workbench plug-in, and then VISUALVM on the left side of the program right click on the trace application, Click here, our Btrace script is as follows:

/* BTrace Script Template */import com.sun.btrace.annotations.*;import static com.sun.btrace.btraceutils.*;@ Btracepublic class Tracingscript {/* Put your code here *//* indicates the method to be viewed, class */@OnMethod (clazz= "Btracetest", method= "add", [ Email protected] (Kind.return)) public static void func (int a,int b, @Return int result) {    println (a);    println (b);    println (result);//Print call result    jstack ();//Print Call Stack}}
When the program runs, Btrace is able to capture the execution of the Add method and print the results, Btrace can do more than that, and other functions can access the relevant information on the Internet.

This is a brief introduction to some of the gadgets that the JDK comes with.

Introduction to JDK Tools

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.