2

I want to profile my class, it is something like a buffer where I save my user's data and get it after. I've created main method and run the test method in endless cycle there.

    public void test(){
       buffer.add(n1);
       Message message = buffer.remove();
       msgSeqNum = message.getMsgSeqNum();
    }

msgSeqNum is global variable, I've made it in order to JVM not to remove my call(I think it can help me).

I was profiling it for 30 seconds, but as I can see my code was run about 40 times(I expected a few thousands). I also see the spaces in JMC(see screenshot) which show me that my JVM did nothing. There were not GC, Exceptions, looks, waiting time and my code works very fast.

I tried to run it several time but I was getting the same result. Why does it happen?

OS - windows

Kire Haglin
  • 6,569
  • 22
  • 27

2 Answers2

3

When sampling (as opposed to full blown profiling) an application, the profiler makes checks at certain intervals to see what methods are being run. This makes it lighter than keeping track of every single invocation.

It also means that you don't get accurate results (which you probably don't need) for things like invocation count. You may think that the code was run only 40 times, but in fact that's just how many times the profiler detected that method being run. While a second without any samples taken does seem like a long time, maybe the sampler just didn't get a chance to run (especially if you have a tight loop and are running on a low end machine).

While sampling doesn't give you exact invocation counts, it does give you enough information to determine which methods are using most time and CPU, so you can optimize code.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

Too few invocations, JFR samples allocations and method invocations to keep overhead low. It samples the stacks about once every 10 ms, and allocations at every TLAB (thread local allocation buffer), or when a large object is allocated.

The JIT compiler may also eliminate the allocation/invocation, but it usually requires more than 40 invocations before it happens.

Kire Haglin
  • 6,569
  • 22
  • 27
  • I don't think JIT compiler eliminated the invocation because I put there this line msgSeqNum = message.getMsgSeqNum(); msgSeqNum is global variable. I am not sure that it is correct way to avoid the elimination, what do you think? – Viacheslav Kolybelkin Apr 24 '17 at 08:27