0

Watching in the Sampler Tab / CPU, under the CPU Samples sub-tab, i see that the most consuming method is java.lang.Thread.sleepnative.

I have about 30 Threads with relative sleep() calls. But what's the meaning of this?

My average reported CPU consumption is 7%, is the Thread.sleep() really using the 70% of this time?

How is it possible, is it a "real" consumption?

Fede Rossi
  • 69
  • 2
  • 7
  • can you provide your thread code? – ug_ Sep 09 '14 at 16:20
  • 1
    Thread.sleep is not free but if it is showing up a high percentage of the time (and you are not sleeping for 0 ms) it is almost certainly wrong. The simplest thing to do is to ignore calls to Thread.sleep along with most OS blocking operations if you are concerned with CPU consumption. – Peter Lawrey Sep 09 '14 at 16:43
  • Profilers try to estimate where time is spent but for a number of reasons it is never 100% accurate. You should treat them as a guide but apply some common sense to the results as well. – Peter Lawrey Sep 09 '14 at 16:46

1 Answers1

8

Usually a sampler will simply check every given time what your program is doing, and report you how many times (and by a simple multiplication give an approximation of how much time) your program was inside a given method.

This often has nothing to do with real CPU load.

For example, a sleep() call pauses the thread, and as such the CPU is almost not loaded at all by your code, but still, if I have a program that only starts, sleeps for 10 minutes and ends, sampling it will tell me that Thread.sleep consumed 100% of my cpu time.

The same applies to IO operations, in which your thread is sitting waiting for data to arrive from somewhere (could be a remote internet host, or simply your disk), and is not significantly loading the CPU, but still the sampler will (correctly) tell you that most of the time has been spent inside IO methods, even if CPU itself was not doing that much.

Simone Gianni
  • 11,426
  • 40
  • 49
  • So, is the CPU consumption reported by the sampler "fake" too? – mark Sep 09 '14 at 16:29
  • 3
    It's not "fake" .. it is where your program spent time, which does not mean that during that time the CPU was 100%, it could be time spent by your application inside a given method that does nothing at the CPU level. – Simone Gianni Sep 09 '14 at 16:34