MalcomTucker
MalcomTucker

Reputation: 7477

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

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

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

Upvotes: 32

Views: 32763

Answers (7)

manikanta
manikanta

Reputation: 8500

Java 1.7* comes bundled with Java Mission Control (jmc), which has 'Flight Recorder' feature which can be used to profile the method execution. Profiling results are displayed almost similar to AppDynamics - easy to spot the perf issues (esp. which methods are eating up all the CPU).

Though not detailed, but good blog post explaining the about Flight Recorder: http://hirt.se/blog/?p=364

* Not sure about the minor version

Upvotes: 4

gub
gub

Reputation: 5229

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

Upvotes: 16

Hussain Ashruf
Hussain Ashruf

Reputation: 441

Exactly this is where AOP helps. Aspects can be added/removed without changing code. If you are using Spring; create Aspect including JoinPoint, Advice listing the Classes and public methods that are to evaluate execution time. Add these beans to the Spring config. Else use AspectJ Container for non-spring application

Upvotes: 1

Bill K
Bill K

Reputation: 62759

There have been a couple profilers listed (The eclipse one and JProfiler). I just want to HIGHLY RECOMMEND that a profiler is one of the tools in your programming toolchest.

This is something most programmers pass over, but a profiler can solve entire classes of problems that are very difficult to solve otherwise.

I'm just saying (to everyone, not just the questioner) that if you haven't used a profiler go find one, download it and run it.

By the way, they are much more powerful than the static output of the java tools--although the java tools might be enough in this specific case. A profiler can tell you what each thread is doing and can make some pretty cool call graphics (flow diagram style) that will help you analyze code you didn't write.

Just find one, and use it for a week or two so that you know what it offers.

Upvotes: 4

karoberts
karoberts

Reputation: 9938

If you are willing to spend a little money,

JProfiler: http://www.ej-technologies.com/products/jprofiler/overview.html

is very good, it shows you % of time used, absolute time used, and # of invocations down to the method level. It understands EJB calls, web service calls, and will even show the SQL of jdbc calls. I use it frequently to find performance issues.

It has memory profiling too, but I find the cpu profiling much more useful.

Upvotes: 2

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.

Upvotes: 22

Thomas L&#246;tzer
Thomas L&#246;tzer

Reputation: 25381

Take a look at Eclipse TPTP. They can provide exactly that and much more for any application started from Eclipse.

Upvotes: 2

Related Questions