Issue
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?
Solution
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.
Answered By - assylias
Answer Checked By - Katrina (JavaFixing Volunteer)