开发者的自测利器-Hprof命令(寻找cpu热点)

  1. 测试代码:

     1 public class HProfTest {
     2     public void slowMethod() {
     3         try {
     4             Thread.sleep(1000);
     5         } catch (Exception e) {
     6             e.printStackTrace();
     7         }
     8     }
     9
    10     public void slowerMethod() {
    11         try {
    12             Thread.sleep(10000);
    13         } catch (Exception e) {
    14             e.printStackTrace();
    15         }
    16     }
    17
    18     public static void main(String[] args) {
    19         HProfTest test = new HProfTest();
    20         test.slowerMethod();
    21         test.slowMethod();
    22     }
    23 }
  2. 影响性能点分析:
    1. 类中有两个方法,slowMethod(睡眠1秒,可以理解成该方法执行需要1秒)和slowerMethod(睡眠10秒,可以理解成该方法执行需要10秒)。
    2. 在程序中的方法执行的时间越长越影响性能,那么slowerMethod就是我们预期影响性能的方法,HProf命令究竟以什么形式展示这个影响性能的方法,请往下阅读。
  3. hprof命令以及其参数介绍:
    /*
        times:java函数的执行时间
        hprof=cpu是针对cpu统计时间
        interval=10 采样10次
    */
    -agentlib:hprof=cpu=times,interval=10
  4. 使用方式:
    1. 点击run-->run configurations-->Java Application-->HProfTest-->选择Arguments选项卡-->在VM arguments框中填入参数
    2. 运行HProfTest
  5. 运行结果(会产生新文件java.hprof.txt):

    JAVA PROFILE 1.0.1, created Sat Sep 24 08:01:10 2016
    
    Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:
    
      - Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
    
      - Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
    
      - Neither the name of Oracle nor the names of its
        contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.
    
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
    Header for -agentlib:hprof (or -Xrunhprof) ASCII Output (JDK 5.0 JVMTI based)
    
    WARNING!  This file format is under development, and is subject to
    change without notice.
    
    This file contains the following types of records:
    
    THREAD START
    THREAD END      mark the lifetime of Java threads
    
    TRACE           represents a Java stack trace.  Each trace consists
                    of a series of stack frames.  Other records refer to
                    TRACEs to identify (1) where object allocations have
                    taken place, (2) the frames in which GC roots were
                    found, and (3) frequently executed methods.
    
    HEAP DUMP       is a complete snapshot of all live objects in the Java
                    heap.  Following distinctions are made:
    
                    ROOT    root set as determined by GC
                    CLS     classes
                    OBJ     instances
                    ARR     arrays
    
    SITES           is a sorted list of allocation sites.  This identifies
                    the most heavily allocated object types, and the TRACE
                    at which those allocations occurred.
    
    CPU SAMPLES     is a statistical profile of program execution.  The VM
                    periodically samples all running threads, and assigns
                    a quantum to active TRACEs in those threads.  Entries
                    in this record are TRACEs ranked by the percentage of
                    total quanta they consumed; top-ranked TRACEs are
                    typically hot spots in the program.
    
    CPU TIME        is a profile of program execution obtained by measuring
                    the time spent in individual methods (excluding the time
                    spent in callees), as well as by counting the number of
                    times each method is called. Entries in this record are
                    TRACEs ranked by the percentage of total CPU time. The
                    "count" field indicates the number of times each TRACE
                    is invoked.
    
    MONITOR TIME    is a profile of monitor contention obtained by measuring
                    the time spent by a thread waiting to enter a monitor.
                    Entries in this record are TRACEs ranked by the percentage
                    of total monitor contention time and a brief description
                    of the monitor.  The "count" field indicates the number of
                    times the monitor was contended at that TRACE.
    
    MONITOR DUMP    is a complete snapshot of all the monitors and threads in
                    the System.
    
    HEAP DUMP, SITES, CPU SAMPLES|TIME and MONITOR DUMP|TIME records are generated
    at program exit.  They can also be obtained during program execution by typing
    Ctrl-\ (on Solaris) or by typing Ctrl-Break (on Win32).
    
    --------
    
    THREAD START (obj=50000191, id = 200002, name="HPROF gc_finish watcher", group="system")
    THREAD START (obj=50000191, id = 200001, name="main", group="main")
    THREAD END (id = 200001)
    THREAD START (obj=50000191, id = 200003, name="DestroyJavaVM", group="main")
    THREAD END (id = 200003)
    TRACE 301758:
    	HProfTest.slowerMethod(HProfTest.java:Unknown line)
    	HProfTest.main(HProfTest.java:Unknown line)
    TRACE 301759:
    	HProfTest.slowMethod(HProfTest.java:Unknown line)
    	HProfTest.main(HProfTest.java:Unknown line)
    TRACE 301012:
    	java.lang.AbstractStringBuilder.append(<Unknown Source>:Unknown line)
    	java.lang.StringBuffer.append(<Unknown Source>:Unknown line)
    	java.io.WinNTFileSystem.normalize(<Unknown Source>:Unknown line)
    	java.io.WinNTFileSystem.normalize(<Unknown Source>:Unknown line)
    TRACE 300936:
    	java.lang.AbstractStringBuilder.append(<Unknown Source>:Unknown line)
    	java.lang.StringBuilder.append(<Unknown Source>:Unknown line)
    	sun.net.www.ParseUtil.decode(<Unknown Source>:Unknown line)
    	sun.misc.URLClassPath$JarLoader.<init>(<Unknown Source>:Unknown line)
    TRACE 301032:
    	java.lang.CharacterDataLatin1.toLowerCase(<Unknown Source>:Unknown line)
    	java.lang.Character.toLowerCase(<Unknown Source>:Unknown line)
    	java.lang.String.toLowerCase(<Unknown Source>:Unknown line)
    	java.io.WinNTFileSystem.hashCode(<Unknown Source>:Unknown line)
    TRACE 300937:
    	java.lang.StringBuilder.append(<Unknown Source>:Unknown line)
    	sun.net.www.ParseUtil.decode(<Unknown Source>:Unknown line)
    	sun.misc.URLClassPath$JarLoader.<init>(<Unknown Source>:Unknown line)
    	sun.misc.URLClassPath$3.run(<Unknown Source>:Unknown line)
    TRACE 300997:
    	sun.net.www.ParseUtil.decode(<Unknown Source>:Unknown line)
    	sun.misc.URLClassPath$JarLoader.<init>(<Unknown Source>:Unknown line)
    	sun.misc.URLClassPath$3.run(<Unknown Source>:Unknown line)
    	sun.misc.URLClassPath$3.run(<Unknown Source>:Unknown line)
    TRACE 300356:
    	java.lang.Character.toUpperCase(<Unknown Source>:Unknown line)
    	java.lang.Character.toUpperCase(<Unknown Source>:Unknown line)
    	java.lang.ProcessEnvironment$NameComparator.compare(<Unknown Source>:Unknown line)
    	java.lang.ProcessEnvironment$NameComparator.compare(<Unknown Source>:Unknown line)
    TRACE 300370:
    	java.lang.ProcessEnvironment.<clinit>(<Unknown Source>:Unknown line)
    	java.lang.System.getenv(<Unknown Source>:Unknown line)
    	sun.usagetracker.UsageTrackerClient$2.run(<Unknown Source>:Unknown line)
    	sun.usagetracker.UsageTrackerClient$2.run(<Unknown Source>:Unknown line)
    TRACE 300357:
    	java.lang.Character.toUpperCase(<Unknown Source>:Unknown line)
    	java.lang.ProcessEnvironment$NameComparator.compare(<Unknown Source>:Unknown line)
    	java.lang.ProcessEnvironment$NameComparator.compare(<Unknown Source>:Unknown line)
    	java.util.TreeMap.put(<Unknown Source>:Unknown line)
    TRACE 300338:
    	java.lang.ProcessEnvironment$CheckedEntry.getKey(<Unknown Source>:Unknown line)
    	java.util.AbstractMap.putAll(<Unknown Source>:Unknown line)
    	java.util.TreeMap.putAll(<Unknown Source>:Unknown line)
    	java.lang.ProcessEnvironment.<clinit>(<Unknown Source>:Unknown line)
    TRACE 301036:
    	java.lang.String.toLowerCase(<Unknown Source>:Unknown line)
    	java.io.WinNTFileSystem.hashCode(<Unknown Source>:Unknown line)
    	java.io.File.hashCode(<Unknown Source>:Unknown line)
    	java.util.HashMap.hash(<Unknown Source>:Unknown line)
    TRACE 301082:
    	sun.misc.URLClassPath.getLoader(<Unknown Source>:Unknown line)
    	sun.misc.URLClassPath.getNextLoader(<Unknown Source>:Unknown line)
    	sun.misc.URLClassPath.getResource(<Unknown Source>:Unknown line)
    	java.net.URLClassLoader$1.run(<Unknown Source>:Unknown line)
    CPU TIME (ms) BEGIN (total = 11076) Sat Sep 24 08:01:21 2016
    rank   self  accum   count trace method
       1 90.29% 90.29%       1 301758 HProfTest.slowerMethod
       2  9.03% 99.32%       1 301759 HProfTest.slowMethod
       3  0.04% 99.36%     783 301012 java.lang.AbstractStringBuilder.append
       4  0.03% 99.39%     665 300936 java.lang.AbstractStringBuilder.append
       5  0.03% 99.41%     665 301032 java.lang.CharacterDataLatin1.toLowerCase
       6  0.02% 99.43%     665 300937 java.lang.StringBuilder.append
       7  0.02% 99.45%      12 300997 sun.net.www.ParseUtil.decode
       8  0.02% 99.47%     398 300356 java.lang.Character.toUpperCase
       9  0.02% 99.49%       1 300370 java.lang.ProcessEnvironment.<clinit>
      10  0.02% 99.50%     388 300357 java.lang.Character.toUpperCase
      11  0.02% 99.52%      42 300338 java.lang.ProcessEnvironment$CheckedEntry.getKey
      12  0.02% 99.54%      12 301036 java.lang.String.toLowerCase
      13  0.02% 99.56%      14 301082 sun.misc.URLClassPath.getLoader
    CPU TIME (ms) END
    
  6. 结果分析:

  

时间: 2024-10-26 19:57:51

开发者的自测利器-Hprof命令(寻找cpu热点)的相关文章

php多进程结合Linux利器split命令实现把大文件分批高效处理

有时候会遇到这样的需求,比如log日志文件,这个文件很大,甚至上百M,需要把所有的日志拿来做统计,这时候我们如果用单进程来处理,效率会很慢.如果我们想要快速完成这项需求,我们可以利用Linux的一个利器split,先根据这个文件的总大小.总行数,来按照一个比例来分割,在根据分割的文件数量,fork出一定比例合适的子进程数量分批处理,那么效率可想而知. linux的split可以看这篇文章来学习测试下Centos文件切割利器_split命令及cat命令合并文件 //shell # split -l

Android使用adb命令查看CPU信息

Android中使用JNI编程的时候会需要编译出不同的SO文件,以供适配不同的机型. 例如: 由此需要查看不同机型的CPU信息. 使用ADB命令查看CPU信息命令如下: 1. adb shell 2. cat /proc/cpuinfo 查看到的信息如下(注意一次只能连接一台设备): 上面这款就是64位CPU的华为机型. 上面这款就是32位的 如果出现adb shell 不能打开的情况,需要将adb.exe的路径添加到环境变量path中. 如果出现"无法启动此程序,因为计算机中丢失AdbWinA

【MySQL笔记】SQL优化利器 - explain命令的输出格式详解

有MySQL使用经验的同学在实际项目中可能会遇到SQL慢查询的场景,有些场景很容易定位问题所在(如单表操作有慢查询SQL时,仔细check SQL语句通常很容易定位索引问题),而有些复杂业务场景下(如多表联合查询几十个字段并做group或sort等操作),人工check SQL语句通常很难发现SQL瓶颈根源.这个时候,MySQL提供的explain命令就派上用场了. 本笔记主要对explain的输出结果做说明,并给出根据explain输出对SQL做优化的思路. 1. EXPLAIN语法及用途 e

每日一测1(带命令行参数运行)

怎么让程序带参数运行以便适应不同的测试环境? 命令行参数在程序中的配置位置是在项目-属性-启动选项-命令行参数里 可以通过Main方法传入命令行参数(控制台程序本身就已带), 传入一个string类型的数组.也可以通过System.Environment这个类取出 static void Main(string[] args)  { for (int i = 0; i < args.Length; i++)      //取出来的直接是参数,没有路径 { Console.WriteLine(&quo

开发者必备丨Docker客户端常用命令分享

学习Docker,首先需要了解Docker基本概念,我们在以前的文章中介绍过docker的基本原理和部署操作系统. 今天讲下嵌入式开发中docker客户端常用的命令,开始进入“动手”阶段(也就是敲Docker命令).Docker命令较多,这里笔者总结下常用的Docker命令,方便小伙伴们查阅. 客户端常用命令列表 以下为Docker 客户端常用命令列表,可以通过docker COMMAND --help来查看这些命令的具体用法. attach:依附到一个正在运行的容器中: build :从一个D

top命令显示CPU使用率过了100%原因

1.使用top命令查看发现cpu使用率超过了100%,如下图: 4868 root      20   0  161m  768  568 S 100.1  0.0   6867:56 pidguard 2.我的机器是8cpu的,经过检查发现top命令显示的是占用的cpu总数. 即8cpu时top下cpu利用率最大可以到达800%. 如果你的进程利用了多个cpu,那么top命令显示的是多个cpu占用率的总和. 所以top命令下查看到的cpu利用率是可能超过100%的.

[Linux小技巧] 一行命令让CPU占用率达到100%

for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/dev/null & done 说明: cat /proc/cpuinfo |grep "physical id" | wc -l 能够获得CPU的个数, 我们将其表示为N. seq 1 N 用来生成1到N之间的数字 for i in `seq 1 N`; 就是循环运行命令,从1到N

Linux下的top命令、%cpu和cps(s)到底是什么意思呢!

在linux的top命令里的cpu信息是什么意思呢?Cpu(s): 62.1% us, 15.9% sy,   0.1% ni, 19.4% id,   2.0% wa,   0.1% hi,   0.4% siMem:    8247956k total,   8232004k used,     15952k free,    205240k buffersSwap:   8191992k total,        48k used,   8191944k free,   7156092k

linux下用top命令查看cpu利用率超过100%

这里显示的所有的cpu加起来的使用率,说明你的CPU是多核,你运行top后按大键盘1看看,可以显示每个cpu的使用率,top里显示的是把所有使用率加起来 按下1后可以看到我的机器的CPU是双核的.%Cpu0,%Cpu1 这里我们也可以查看一下CPU信息:在命令行里输入:cat /proc/cpuinfo 这里可以看到cpu cores       : 2