1

I am trying to test some queue insertions and removal of objects timestamp. After a few test runs I have a significant speedup improvement of upto 80 times better results on the same code. This looks clearly a caching result either by JVM or the hardware\cpu cache, but I wish to get fresh results each run.

Is there a way to clear both\either of these caches programmatically from within the Java code?

4
  • This sounds more likely to be the JIT compiler kicking it. As how to solve it is beyond me... Commented Feb 17, 2013 at 8:09
  • 1
    why not simply run it for a while to let jit run and then start measuring? Commented Feb 17, 2013 at 8:11
  • 2
    stackoverflow.com/questions/504103/… Commented Feb 17, 2013 at 8:11
  • WHy is this a problem? The JIT is speeding things up, and that is the actual performance. Commented Feb 17, 2013 at 8:12

2 Answers 2

6

This is probably due to the JIT kicking in. The JIT will compile your bytecode to machine code after a certain number of runs to make it more efficient.

You can change the number of calls before a method gets optimised by setting the -XX:CompileThreshold option (default value is 10,000) or by excluding your class from being optimised at all.

However I'm not sure why you would want to disable the compiler and force your program to run more slowly.

Sign up to request clarification or add additional context in comments.

1 Comment

Or he could turn the JIT compiler off completely ... though doing these means he will measure the code running in an unrealistic way. The numbers may not be indicative of real performance.
0

If you want to design a microbenchmark, I would suggest using JMH.

It is 'the' microbenchmarking framework and it will take care of JIT warmup, dead code elimination etc. It also has out of the box support for various profilers including Linux Perf.

https://www.baeldung.com/java-microbenchmark-harness

https://github.com/openjdk/jmh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.