7

This might seem like a silly question, but after asking some questions on stackoverflow and looking at other people's question, one thing that comes often is to use profilers to see which part of the code runs slow, etc.

Being a programming beginner, I'm new to all of this, but since I'm creating bigger and bigger project, I feel like such a tool would prove to be very useful at times. The only thing is, I don't know how to use them.

Usually, I use JDeveloper to code in Java, and I read on the internet that there's one built-in. I tried using it but I guess I didn't really know how because I couldn't find which parts where making my program slower. I don't know if it's because I don't know how to use it or if it's known for not being good, so I figured I'd ask here to see with which one I should start.

I have Eclipse installed as well and know how to use it (I use Eclipse when I'm coding some Python or when I'm trying tutorials for Android-development), so if there's a free profiler I could use for either of those two IDEs, I'd really like knowing about it. Also, a tutorial or basic things I need to know about profilers would help me a lot.

Thanks in advance and sorry that my question isn't really about programming, but this seems like the best place to get valuable information about profilers.

Adam Smith
  • 4,432
  • 8
  • 41
  • 64

3 Answers3

4

If you're confused, so are most of the profiler vendors.

The first thing to understand is - programs aren't slow because they have slow parts (bottlenecks). They are slow because they do more than they have to. They have time drains.

It's like a horse race. A bottleneck would be a narrow place in the track, where the horses have to pile up and slow down. A time drain would be like another track fused with the first, into which horses wander and have to travel an extra distance. Then there could be another track fused with that one, and another, and so on.

A function call that could be avoided is an example of a time drain.

Here's how I find time drains. It's simple and language-independent. You can even do it with a simple tool like jStack.

Profiler makers mean well, but they are hampered by a bunch of confusing concepts.

Such as "where the time is spent". If what this means is "where the program counter is found most often", that's like the horse being in the wrong racetrack. You could try to shorten that racetrack, but the real problem is the horse shouldn't even be there. i.e. there's a function call that should have been avoided.

Such as "statistical accuracy of measurement". Do you need to measure how long the horse takes to get around the wrong racetrack to know it's on the wrong racetrack? No, you just need to take a snapshot (stack sample). The longer it's on the wrong racetrack, the more likely you'll see it. If you see it there twice, you know what the problem is.

Such as calling it a "CPU Profiler", that being an excuse to ignore I/O time. Sometimes the time drain is needless I/O you were not aware of. That would be like the horse stopping to munch on a bag of oats. If you can only take your snapshot while the horse is running, you would never notice. You would only notice that the time was suspiciously long.

There's more where those came from ...

Community
  • 1
  • 1
Mike Dunlavey
  • 38,662
  • 12
  • 86
  • 126
  • The links you posted are very interesting reads. I'll try to apply what you suggested and I'm sure it's going to end up helping a ton in the future. Thanks a lot! – Adam Smith Jul 16 '11 at 20:08
3

If you are looking for a free tool, jvisulavm is a great one. It comes with a standard JDK install, its executable is usually under the bin directory of the JAVA_HOME, next to java, javac, javadoc, and their siblings. Here is a tutorial:

http://download.oracle.com/javase/6/docs/technotes/guides/visualvm/index.html

If you are looking for a more advanced commercial product YourKit or jprofiler are both interesting.

sbridges
  • 24,059
  • 3
  • 60
  • 70
Moe Matar
  • 1,956
  • 12
  • 7
  • 1
    VisualVM releases more regularly than the JDK, so you may want to download the [latest version](http://visualvm.java.net/download.html). – Ryan Stewart Jul 16 '11 at 03:47
1

Using profiler is not a trivial task and requires a lot of background knowledge about VM internals. So it's best to start with reading about memory management, hot spot etc. Some decent help may be found in profiler help pages, see for example, help for JProfiler.

In my experience, I only had to use profiler few times when some serious performance issues arisen which could not be tracked by other means. In many cases simple things, like timestamps in the log, help to resolve performance problems. So use profiler as a last resort. Said that, I heard of cases when people would use it on a regular basis. I though about it but never could get this process to work for me.

I was not able to find any free profiler that would satisfy me. Commercial ones that I used at different times are JProbe and JProfiler. I liked JProfiler more but both allow you to get results.

Generally, the process starts with configuring your application inside profiler and telling it what classes you want to specifically look into. When you start your application in profiler, classes gets instrumented in such way that information about execution and VM gets recorded. So after running your application you can analyze it to see which code consumed most of the time and what objects used most of memory. You can often also monitor threads, monitors and number of other things. To take advantage of all the data you will need to understand great deal about VM internals and the code that you are profiling.

Good Luck!

animuson
  • 50,765
  • 27
  • 132
  • 142
Alex Gitelman
  • 23,471
  • 7
  • 49
  • 48