Java profiling - how can I get a method by method analysis of my application?

后端 未结 7 1218
挽巷
挽巷 2020-12-29 03:01

I want to run my Java app and for a given workload be able to see:

  • how many times a given function was called
  • how expensive each function call is in
7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 04:05

    Easiest way is to use -prof, e.g: java -prof -jar yourjar.jar

    That will print a file called java.prof after the program has finished running.

    See the HPROF documentation page

    In my application I use: -Xrunhprof:cpu=samples,thread=y,doe=y

    This prints a report that contains, amongst other things, this:

    CPU SAMPLES BEGIN (total = 55110) Sun Feb  7 17:02:51 2010
    rank   self   accum   count  trace  method
    1      69.68% 69.68%   38399 300361 java.net.SocketInputStream.socketRead0
    2      24.40% 94.08%   13448 300386 java.net.SocketInputStream.socketRead0
    3      0.20%  94.28%     108 300425 java.io.FileOutputStream.writeBytes
    4      0.19%  94.47%     107 300976 java.net.PlainDatagramSocketImpl.receive0
    5      0.19%  94.65%     102 300414 package.BlockingSampleBuffer.addSample
    6      0.16%  94.82%      90 300365 java.net.SocketOutputStream.socketWrite0
    7      0.16%  94.98%      89 300412 package.BlockingSampleBuffer.addSample
    8      0.15%  95.13%      84 300430 java.lang.Object.wait
    9      0.14%  95.27%      77 300592 java.io.FileOutputStream.writeBytes
    10     0.14%  95.41%      76 300566 java.lang.AbstractStringBuilder.
    

    So you can see the total time (in seconds) spent in various methods. In mine the app spends most of its time waiting for data from a remote host (not unlikely over an internet connection).

提交回复
热议问题