• JDK工具


    在之前的教程中,我曾介绍过 这些工具。现在,我向大家介绍其中最重要的5个工具。 

    1.javap

    javap是一个Java类文件反汇编程序,可以查看Java编译器生成的字节码,是分析代码的一个好工具。让我们用javap来编译这段Hello World代码,再分解它。

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. public class HelloWorld {  
    2.     public static void main(String... args) {  
    3.         System.out.println("Hello World!");  
    4.     }  
    5. }  
    C:UsersCycleDesktop>javap HelloWorld

    我没有传递任何参数,只是运行了javap这个工具,就得到了上面这个结果。默认情况下,它会输出Java类的package,protected,public字段和方法。

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. Compiled from "HelloWorld.java"  
    2. public class HelloWorld {  
    3.   public HelloWorld();  
    4.   public static void main(java.lang.String...);  
    5. }  
    C:UsersCycleDesktop>javap -c HelloWorld

    如果传递参数-c到javap里面,便会得到上面这个结果。这是一条非常好的信息,这样输出的指令可以帮助我们更好地了解JVM。 

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. Compiled from "HelloWorld.java"  
    2. public class HelloWorld {  
    3.   public HelloWorld();  
    4.     Code:  
    5.        0: aload_0  
    6.        1: invokespecial #1                  // Method java/lang/Object."":()V  
    7.        4: return  
    8.   
    9.   public static void main(java.lang.String...);  
    10.     Code:  
    11.        0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;  
    12.        3: ldc           #3                  // String Hello World!  
    13.        5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V  
    14.        8: return  
    15. }  

    2.jvisualvm

    jvisualvm是一个Java虚拟机监控和分析工具,该工具提供了一个图形界面窗口,并且可以直观的了解Java应用程序的运行时信息。jvisualvm集成了许多工具,比如像jmp、jinfo、jstat、jstack、JConsole等。自从JDK 6 Update 7以后已经作为JDK的一部分。 

     

    在 Java垃圾回收监控和分析这篇文章中,我曾使用jvisualvm,大家不妨过去看看jvisualvm的使用方法。 

    3.jcmd

    jcmd主要用来把诊断命令请求发送到Java JVM中,当JVM进程中没有jcmd参数列表时,jcmd就会立即运行。这相当于jps工具,我开始启动jconsole,并且把它作为参数传递到jcmd,得到如下结果,这个也可以通过进程id(pid)实现。 

    C:UsersCycle>jconsole
    
    C:UsersCycle>jcmd JConsole help
    3344:
    The following commands are available:
    JFR.stop
    JFR.start
    JFR.dump
    JFR.check
    VM.native_memory
    VM.check_commercial_features
    VM.unlock_commercial_features
    ManagementAgent.stop
    ManagementAgent.start_local
    ManagementAgent.start
    Thread.print
    GC.class_stats
    GC.class_histogram
    GC.heap_dump
    GC.run_finalization
    GC.run
    VM.uptime
    VM.flags
    VM.system_properties
    VM.command_line
    VM.version
    help
    C:UsersCycle>jcmd JConsole VM.uptime 
    3344:289.977 s

    VM.uptime显示了Java应用程序具体运行时间。 

    在调试的时候,下面的参数可以用于并发锁的线程堆栈溢出。

    jcmd <pid> Thread.print -l

    4.jhat

    jhat的全称是Java heap analysis tool。它主要是用来解析和浏览堆文件,jhat有时更像是一个可视化工具。jhat解析堆存储( heap dump)并启动一个webserver,然后用户可以在浏览器下查看堆。jhat支持对象查询语言(oql)和一些预先设计查询。OQL帮助可能在 

    http://localhost:7000/oql/ 
    http://localhost:7000/oqlhelp/

    jmap工具来生成堆转储,我们应该使用-dump参数,下面jhat工具可以使用的参数列表: 

    C:UsersCycle>jhat -help
    Usage:  jhat [-stack ] [-refs ] [-port ] [-baseline ] [-debug ] [-version] [-h|-help] 
    
            -J          Pass  directly to the runtime system. For
                              example, -J-mx512m to use a maximum heap size of 512MB
            -stack false:     Turn off tracking object allocation call stack.
            -refs false:      Turn off tracking of references to objects
            -port :     Set the port for the HTTP server.  Defaults to 7000
            -exclude :  Specify a file that lists data members that should
                              be excluded from the reachableFrom query.
            -baseline : Specify a baseline object dump.  Objects in
                              both heap dumps with the same ID and same class will
                              be marked as not being "new".
            -debug :     Set debug level.
                                0:  No debug output
                                1:  Debug hprof file parsing
                                2:  Debug hprof file parsing, no server
            -version          Report version number
            -h|-help          Print this help and exit
                        The file to read
    
    For a dump file that contains multiple heap dumps,
    you may specify which dump in the file
    by appending "#" to the file name, i.e. "foo.hprof#3".
    
    All boolean options default to "true"

    我给jconsole应用程序创建了一个堆转储文件,并使用以下命令来运行进程id 3344:

    jmap -dump:format=b,file=heap.bin 3344

    现在,堆转储文件准备就绪,运行下面命令并且会启动一个服务: 

    jmap -dump:format=b,file=heap.bin 3344

    在控制台输出结果:

    C:UsersCycleDesktop>jhat heap.bin
    Reading from heap.bin...
    Dump file created Sun Nov 16 19:26:35 IST 2014
    Snapshot read, resolving...
    Resolving 641209 objects...
    Chasing references, expect 128 dots..................
    Eliminating duplicate references.....................
    Snapshot resolved.
    Started HTTP server on port 7000
    Server is ready.

    在浏览器中输入:http://localhost:7000/后便会出来堆转储的详细情况:

    例如,还可以在http://localhost:7000/histo/查看堆内存柱状图。

    5.Oracle Java Mission Control 

    作为JVM融合战略的一部分,主要用来统一HotSpot、JRockit VMs。目前,JRockit Mission Control在标准版Java SE中已经可以使用。Java Mission Control(JMC)与Java Flight Recorder一起工作,适用于HotSpot JVM,用来记录核心数据和事件。它是一个调优工具,并且适用于Oracle JDK。一旦出现问题,这些数据就可以用来分析。 

    开发者可以使用jmc命令来创建JMC工具。 

     

     

  • 相关阅读:
    FastAPI(60)- 针对 WebSocket 进行单元测试
    FastAPI(59)- 详解使用 OAuth2PasswordBearer + JWT 认证
    FastAPI(58)- 使用 OAuth2PasswordBearer 的简单栗子
    FastAPI(57)- 安全相关的概念
    FastAPI(56)- 使用 Websocket 打造一个迷你聊天室
    FastAPI(55)- Events: startup
    FastAPI(54)- 详解 Request 请求对象
    FastAPI(53)- Response Headers 响应设置 Headers
    FastAPI(52)- Response Cookies 响应设置 Cookies
    FastAPI(51)- 自定义响应之 StreamingResponse、FileResponse
  • 原文地址:https://www.cnblogs.com/haiyang1985/p/7654789.html
Copyright © 2020-2023  润新知