1. 测试代码:

     public class HProfTest {
    public void slowMethod() {
    try {
    Thread.sleep(1000);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public void slowerMethod() {
    try {
    Thread.sleep(10000);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    HProfTest test = new HProfTest();
    test.slowerMethod();
    test.slowMethod();
    }
    }
  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. 结果分析:

  

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

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

    有时候会遇到这样的需求,比如log日志文件,这个文件很大,甚至上百M,需要把所有的日志拿来做统计,这时候我们如果用单进程来处理,效率会很慢.如果我们想要快速完成这项需求,我们可以利用Linux的一个利 ...

  2. 寻找CPU使用率高的进程方法

    寻找CPU使用率高的进程方法 发布时间:  2017-07-13 浏览次数:  1362 下载次数:  0 问题描述 节点报CPU使用率高,甚至出现"ALM-12016 CPU使用率超过阈值 ...

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

    今天跑了一个非常耗时的批量插入操作..通过top命令查看cpu以及内存的使用的时候,cpu的时候查过了120%..以前没注意..通过在top的情况下按大键盘的1,查看的cpu的核数为4核. 通过网上查 ...

  4. psacct监视用户执行的命令,如cpu时间和内存战胜,实时进程记账

    psacct监视用户执行的命令,如cpu时间和内存战胜,实时进程记账

  5. 【记录一次windows技术学习】使用笔记本DOS命令搭建WLAN热点

    [记录一次windows技术学习]使用笔记本DOS命令搭建WLAN热点 时间:2017-10-14 22:36:13 撰写者:AK末影人 [转发请注明出处] --------------------- ...

  6. Android使用adb命令查看CPU信息

    Android中使用JNI编程的时候会需要编译出不同的SO文件,以供适配不同的机型. 例如: 由此需要查看不同机型的CPU信息. 使用ADB命令查看CPU信息命令如下: 1. adb shell 2. ...

  7. python调用top命令获得CPU利用率

    1.python调用top命令获得CPU利用率 思路:通过python调用top命令获取cpu使用率 #python2代码 [root@zdops-server script]# cat cpu_lo ...

  8. Linux下用命令查看CPU ID以及厂家等信息

    Linux下用命令查看CPU ID // 获得CPU IDdmidecode -t 4 | grep ID |sort -u |awk -F': ' '{print $2}' // 获得磁盘IDfdi ...

  9. top命令查看CPU状态信息:%us、%sy、%ni、%id、%wa、%hi、%si、%st 表示的是什么意思

    Linux CPU负载状态:%us/%sy/%ni/%id/%wa/%hi/%si/%st含义 2018-08-26 分类:Linux 评论(0)   缙哥哥发现用了雅黑的探针,在 Linux 的 C ...

随机推荐

  1. Linux 7.x 防火墙&端口

    Linux 7.x 防火墙&端口 查看当前防火墙的状态: # firewall-cmd --state 也可以使用指令:systemctl status firewall.service 启动 ...

  2. C#中DataTable与XML格式的相互转换

    1.DataTable转换成XML public string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; ...

  3. 简单的Json数据

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  4. sql 生成随机字符串

    生成三位随机字母+12位数字 ),), @c int; select @CardCode=abs(CHECKSUM(NEWID())) -LEN(@CardCode); ,@c)) set @Card ...

  5. IT题库2-LinkList和ArrayList(插入数据、末尾插入数据、不同数据量插入数据)的效率?

    ArrayList和LinkedList是两个集合类,用于存储一系列的对象引用(references).ArrayList是List接口的一个实现类,它的特点是查询效率高,增删效率低,线程不安全,原因 ...

  6. bzoj2809 [Apio2012]dispatching(左偏树)

    [Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 M ...

  7. 编译jmeter5.0源码

    jmeter5.0使用过程中,遇到request或者response乱码的情况,想要一次性解决这个问题,需要编译ApacheJMeter_http.jar这个包(lib\ext文件下)里的Reques ...

  8. Nginx 出现 _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/

    Nginx 出现  _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/ 这种情况是因为 application 没有足的权限 .需要给予777的权限就能解决了

  9. linux使用vim打开乱码问题

    在windows中编辑好的汉字文本文档,上传到Linux下打开乱码. [root@localhost ~]# rpm -ivh /mnt/Packages/lrzsz-0.12.20-36.el7.x ...

  10. oracle中新建用户和赋予权限

    1.新建个用户 create user xxxxx(用户名) identified by "密码" alert user 用户名 identified by “新密码” --修改用 ...