Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

profiling

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 relative terms (i.e. how long each takes to execute)

I know broadly where the bottle neck is in my application but I need a much more fine grained view in order to narrow it down.

Thanks

Edit jvisualvm looks like the tool - it identified the problem in about 30 seconds. I just need to know what 'selftime' means in the context of a method profile. Thanks

like image 977
MalcomTucker Avatar asked Feb 15 '10 17:02

MalcomTucker


People also ask

Which of the below Cannot be used profiling Java applications?

Which of the below is not a Java Profiler? Explanation: Memory leak is like holding a strong reference to an object although it would never be needed anymore. Objects that are reachable but not live are considered memory leaks. Various tools help us to identify memory leaks.

What is JProfiler used for?

JProfiler is a Java profiler tool and is useful for developers/testers as it can be used to analyze performance bottlenecks, memory leaks, CPU loads, and to resolve threading issues. JProfiler works both as a stand-alone application and as a plug-in for the Eclipse software development environment.


2 Answers

Simplest approach for a program running in java 6 from Sun is to use the jvisualvm program in the jdk. Allows you to attach and profile without any special setup.

like image 183
Thorbjørn Ravn Andersen Avatar answered Sep 23 '22 15:09

Thorbjørn Ravn Andersen


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.<init> 

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).

like image 30
gub Avatar answered Sep 19 '22 15:09

gub