开发者

Profiling Java Code

I'm attempting to profile a Java web search program called Nutch from source. As far as I understand, to profile, I need to enable profiling in the compiler in order to generate a profile file to be opened in a program such as GProf. How do I do th开发者_JAVA百科is if all I do to compile the software is run ANT withing the source root directory?


If you're running a newer JDK (the latest 1.6 update 7 or greater), you don't need to do anything as far as preparing your Java process to profile. Simply use JVisualVM (which comes with the JDK) to attach to your process, and click the profile button.


You say in response to @Charlie's answer that ideally you would like information about how the program spends it's time.

There's another viewpoint - you need to know why the program spends its time.

The reason each cycle is spent is a chain of reasons, where each link is a line of code on the call stack. The chain is no stronger than its weakest link.

Unless the program is as fast as possible, you've got "bottlenecks".

For example, if a "bottleneck" is wasting 20% of the time, then it consists of an optimizable line of code (i.e. poorly justified) that is on the stack 20% of the time. All you have to do is find it.

If 10,000 samples of the stack are taken, it will be on about 2,000 of them. If 10 samples are taken, it will be on 2 of them, on average.

In fact, if you randomly pause the program several times and study the call stack, if you see an optimizable line of code on as few as 2 samples, you've found a "bottleneck". You can fix it, get a nice speedup, and repeat the whole process.

That is the basis of this technique.

Regardless, thinking in terms of gprof concepts will not serve you well.


You're really asking an Ant question here. You can add command line flags for the compiler as attributes in the ant file for the compile target. See the <compilerarg> tag here.

There are a lot of good profiling tools, by the way. Have a look at this google search.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜