Java Profiling – VisualVM Sampling & Accuracy

javaprofiling

It's been said in other questions that jvisualvm sampling works as a "lightweight" profiling tool by calculating metrics directly from Java stack frames.

Its almost unanimously agreed that such a technique is faster but not as accurate in terms of its "timings". My question: why is this method not as accurate? And, what is it not as accurate as (as opposed to what? Are there more precise profiling techniques?)?

Best Answer

There are a few factors I am aware of:

  1. Look at the javadoc of System.currentTimeMillis() and note what it says about the Operating System granularity. For even more fun, look at the source code of that method and see how it rounds to a millisecond. A similar rule applies for Java's nanosecond support, look at the source code to see how it is actually used and you might be surprised.

  2. There are still gaps where there are no Java APIs to measure certain things, e.g. Clock counters on a CPU (which we're working on with jpcm) and APIs into the running GC itself.

HTH a little.