`
m635674608
  • 浏览: 5027053 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

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

    博客分类:
  • jvm
 
阅读更多
  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):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    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. 结果分析:

  

 

http://www.cnblogs.com/aaronL/p/5902386.html

分享到:
评论

相关推荐

    性能分析系列-小命令保证大性能

    最近在工作中经常和性能压测工作打交道,积累了一些性能分析经验,我... 一、开发者的自测利器-Hprof命令 1、示例演示 例子程序:/***PROJECT_NAME:test*DATE:16/7/22*CREATEBY:chao.cheng**/publicclassHProfTest

    解决sdk tools运行缺少hprof-conv文件

    hprof-conv.exe 关于sdk tools 里面缺少这个运行文件

    hprof-conv文件下载和使用说明

    hprof-conv.exe 在sdk tools 里面缺少这个运行文件导致DDMS files not found: tools\hprof-conv.exe 错误 , 详细看博客:https://blog.csdn.net/DickyQie/article/details/79657573

    hprof-conv.exe

    在Android开发过程中,可能会遇到各种问题,其中之一是“DDMS files not found: tools\hprof-conv.exe”的错误。这个错误通常发生在尝试分析或转换Hprof文件时,Hprof是Android系统生成的一种内存快照文件格式,用于...

    xalanjava源码-Efficient-Hprof:Hprof更改以允许更好的内存跟踪

    java源码高效的HProf 完成这个项目是为了检查是否可以对 hprof 进行一些改进。 所有依赖项都添加到存储库中,并使用 Java 版本 8。 该版本只在linux、solaris和bsd上编译,其他版本修改lib文件夹为系统需要的文件...

    hprof-conv.rar

    执行上述命令后,hprof-conv会读取输入文件并将其转换为更易于处理的格式,然后保存到指定的输出文件中。 在Eclipse中,当出现找不到hprof-conv.exe的错误时,应检查以下几点: 1. **SDK路径**:确保你的Android ...

    PyPI 官网下载 | hprof2flamegraph-0.0.5.tar.gz

    总结来说,"hprof2flamegraph"库是Python开发者解决Java性能问题的利器,它将Java剖析工具与现代性能分析技术相结合,提高了分析效率。通过PyPI下载并使用这个库,开发者可以更轻松地理解和优化Java应用的性能,从而...

    hprof-conv 内存转mat工具

    hprof-conv ,内存转mat工具

    hprof-conv

    sdk\tools文件夹中缺少hprof-conv.exe文件,导致DDMS报错,Eclipse无法正常运行

    解决sdk tools运行缺少hprof-conv.exe(最低积分)

    只能设个最低1积分。同事电脑有的话,到adt-bundle-windows-x86_64\sdk\tools\路径下查找。

    Android在eclipse 缺少的工具包(hprof-conv.exe,,traceview.bat)

    通过运行hprof-conv.exe,开发者可以将原始的二进制.hprof文件转换成文本或XML格式,以便于分析内存泄漏、对象分配等内存管理问题。这对于优化应用性能和防止内存消耗过大至关重要。 2. **traceview.bat**:这是一...

    解决sdk tools运行缺少adb和hprof-conv文件

    本文将深入探讨标题和描述中提及的问题,即如何解决SDK Tools运行时缺失ADB(Android Debug Bridge)和hprof-conv文件的问题。 首先,ADB是一个强大的命令行工具,用于连接开发者计算机与Android设备进行通信。它...

    ubuntu 14.04 sdk_tools_hprof-conv

    下载android-sdk_r23.0.2-linux.tgz tools里有可能缺少hprof-conv

    HPROF Agent 中文版本

    HPROF的主要功能包括显示CPU使用情况、堆分配统计、监视器和线程状态的完整堆转储。 **启动与使用** 要启动HPROF,可以在Java命令行中添加`-agentlib:hprof`参数来指定需要的分析类型。例如,要获取堆分配配置文件...

    HPROF Agent 英文 版

    HPROF的源码可以从OpenJDK项目的相关仓库中获取,对于开发者来说,这是一份有价值的参考资料,可以帮助理解和实现自定义的JVM TI代理。 总之,HPROF Agent是Java性能分析的强大工具,通过其各种选项,开发人员和...

Global site tag (gtag.js) - Google Analytics