9

I would like to know how to check if the JIT compiler is turned off. I have the following code which is meant to turn the JIT compiler off.The problem is, I am not sure if it is actually doing that. So I was wondering if there is a way of checking if the JIT is off. I looked at the Compiler class but there isn't any method like isDisabled/enabled().

Code:

Compiler.disable();  

Any help or direction will be highly appreciated.

DIF
  • 2,470
  • 6
  • 35
  • 49
isaiah
  • 311
  • 2
  • 5
  • 14
  • You want to check this at _runtime_? – Louis Wasserman Feb 26 '12 at 21:38
  • Yes, I would like to check it at runtime. – isaiah Feb 26 '12 at 21:48
  • 1
    Maybe I should also add why I am doing this. Basically, we have a program that's meant to time how long it takes to run an algorithm (e.g. quicksort) based on the size of the data. What we noticed was that the first two times are really skewed compared to the other results and we are of the opinion that this is due to the initialisation of the JIT compiler. As a result, we wanted to see the effect on the times when the JIT is off. – isaiah Feb 26 '12 at 21:50
  • It is not really a boolean thing, methods get optimized and deoptimized in different tiers. It is best to use a benchmark harness which supports warmup. I would use JMH. It even has compiler control annotations. – eckes Jan 29 '16 at 11:20
  • Another reason to check at runtime, I have an application that appears to switch to interpreted only mode after a certain point in time. I have tracked down a JIT bug (https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8023983) that appears to be related. I want to know if something has "whomped" memory and overwritten that flag. – pojo-guy Jan 16 '19 at 20:13

5 Answers5

22

(Not a direct answer to your question since it seems your were trying to turn off the JIT compiler programmatically, but based on your comment, this might be of interest.)

If you want to turn off the JIT compiler on a Sun/Oracle JVM, you should try the -Xint option:

-Xint

Operate in interpreted-only mode. Compilation to native code is disabled, and all bytecodes are executed by the interpreter. The performance benefits offered by the Java HotSpot Client VM's adaptive compiler will not be present in this mode.

Bruno
  • 119,590
  • 31
  • 270
  • 376
  • upto which version of Java is this supported? because I'm getting `javac: invalid flag: -Xint`. – Erik Kaplun Jan 21 '15 at 15:35
  • 1
    @ErikAllik It's available in the options when you type `java -X` in Oracle JRE 8. Whether or not to use the JIT compiler is a JRE decision, this has nothing to do with compiling the source code into bytecode (what `javac` does): you're probably getting this error because you're using it with `javac` not `java`. – Bruno Jan 21 '15 at 15:56
  • oh crap... you are totally right; I feel retarded now; I knew everything you said :) – Erik Kaplun Jan 21 '15 at 16:21
10

In the article Performance Features and Tools.

The JIT compiler was first made available as a performance update in the Java Development Kit (JDK) 1.1.6 software release and is now a standard tool invoked whenever you use the java interpreter command in the Java 2 platform release.

You can disable the JIT compiler using the -Djava.compiler=NONE option to the Java VM.

So, you can deduce that when the variable is not set, or set to something other than NONE, then the JIT is enabled.

Community
  • 1
  • 1
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
10

I don't believe you can turn the JIT off at runtime.

If you want to seriously benchmark a Java program, you should definitely be ignoring the first few runs. Getting reliable benchmarks in Java is an extremely tricky business, best left to people much smarter than you or I.

I recommend using Caliper, which is used internally at Google for microbenchmarking and is plenty smart about warming up the JIT and stuff. In particular, look at the example here, which shows how to measure the efficiency of an algorithm for different input sizes.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Thanks Louis. This is for my final year project. We actually considered ignoring the first few runs too. I guess it's the easiest option for now. I will also look at Caliper. – isaiah Feb 26 '12 at 22:38
  • Is the project implementing the algorithms you're measuring, or is it doing the benchmarking? – Louis Wasserman Feb 26 '12 at 22:54
  • It's basically running the user's algorithm using reflection and then analysing it in terms of O notation. Also, what confuses me is, on the IBM's site (http://publib.boulder.ibm.com/infocenter/realtime/v1r0/index.jsp?topic=%2Fcom.ibm.rt.doc.10%2Frealtime%2Frt_jit.html), it does say how to turn off the JIT compiler or am I misinterpreting the message? – isaiah Feb 27 '12 at 10:06
  • It can turn off if you run out of code cache. In that case it prints a "CodeCache is full. Compiler has been disabled" – juancn Nov 02 '16 at 15:35
7

IBM JVMs definitely support the Java interface java/lang/Compiler.disable() and .enable() which was introduced in Java 5, I believe. That includes WebSphere Real Time (which is a JVM designed to provide more predictable performance) as well as our "standard" JVMs. If you call disable(), it will prevent JIT compilations until you call enable().

I work for IBM on the JIT compiler team. We don't usually recommend people to use this interface, because interfering with JIT compilation heuristics is generally not a good idea, but there are reasonable real-time scenarios where you would use it.

Mark Stoodley
  • 71
  • 1
  • 1
5

You can printout methods when they get compiled, with `-XX:+PrintCompilation if your method isn't printed out or suddenly gets faster after it is print out, you can see the likely cause.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130