序言
如果你经常遇到 java 线上性能问题束手无策,看着线上服务 cpu 飙升一筹莫展,发现内存不断泄露满脸茫然。
别慌,这里有一款低开销、自带火焰图、让你大呼好用的 java 性能分析工具 - async-profiler。
1、准备程序
[root@localhost ~]# git clone git://github.com/jvm-profiling-tools/async-profiler [root@localhost async-profiler]# yum -y install gcc+ gcc-c++ [root@localhost async-profiler]# make [root@localhost ~]# more test.java public class test { public static void main(string[] args) throws exception { test test = new test(); while (true) { test.func1(); test.func2(); test.func3(); } } public void func1() throws exception { //调用第一个方法,需要100ms thread.sleep(100l); } public void func2() throws exception { //调用第二个方法,需要500ms thread.sleep(500l); } public void func3() throws exception { //调用第三个方法,需要1500ms thread.sleep(1500l); } } [root@localhost ~]# [root@localhost ~]# javac test.java [root@localhost ~]# java test
生成火焰图数据
root 21765 7152 0 18:29 pts/1 00:00:00 java test root 21799 21777 0 18:29 pts/0 00:00:00 grep --color=auto java [root@localhost ~]# cd async-profiler/ [root@localhost async-profiler]# [root@localhost async-profiler]# ./profiler.sh -d 60 -o collapsed -f /tmp/test_01.txt 21765 profiling for 60 seconds done [root@localhost async-profiler]# cd .. [root@localhost ~]# ls anaconda-ks.cfg a.out async-profiler flamegraph jdk-8u121-linux-x64.tar.gz perf.data process.svg test.c test.class test.java [root@localhost ~]# cd flamegraph/ [root@localhost flamegraph]# perl flamegraph.pl --colors=java /tmp/test_01.txt > test_01.svg [root@localhost flamegraph]# ls aix-perf.pl example-perf-stacks.txt.gz readme.md stackcollapse-instruments.pl stackcollapse-pmc.pl test demos example-perf.svg record-test.sh stackcollapse-java-exceptions.pl stackcollapse-recursive.pl test_01.svg dev files.pl stackcollapse-aix.pl stackcollapse-jstack.pl stackcollapse-sample.awk test.sh difffolded.pl flamegraph.pl stackcollapse-bpftrace.pl stackcollapse-ljp.awk stackcollapse-stap.pl docs jmaps stackcollapse-elfutils.pl stackcollapse-perf.pl stackcollapse-vsprof.pl example-dtrace-stacks.txt pkgsplit-perf.pl stackcollapse-gdb.pl stackcollapse-perf-sched.awk stackcollapse-vtune.pl example-dtrace.svg range-perf.pl stackcollapse-go.pl stackcollapse.pl stackcollapse-xdebug.php [root@localhost flamegraph]# sz test_01.svg
3、展示
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论