NDeveloper
NDeveloper

Reputation: 1847

How to know method is JITed or interpreted

As I understand there is no clear rule to determine whether Java method will be JITed or interpreted while call. So is there somehow to tell JVM that I need a certain method to be JITed. And is there a way to know for sure which method will be JITed and which not.

Upvotes: 6

Views: 1274

Answers (5)

user207421
user207421

Reputation: 310915

You can't tell and there is no way that it makes any difference barring bugs in HotSpot. The term 'JIT' is at least ten years out of date.

Upvotes: 0

josefx
josefx

Reputation: 15656

There is a way to ask the jvm to compile a class, it is not guaranteed to do anything but should work on any jvm having a jit.

Compiler.compileClass(MyClass.class);

Upvotes: 0

Denis Tulskiy
Denis Tulskiy

Reputation: 19177

And is there a way to know for sure which method will be JITed and which not.

The (Oracle) Sun JVM is called HotSpot, which means it looks at which methods are called most, thus becoming "hot", and those methods are the first to be compiled. So some methods may never be compiled. But if you know the method is called a lot, it most probably be compiled. You can set the threshold with -XX:CompileThreshold=10000 VM options, which specifies how many invocations it takes to consider the method "hot".

I don't know any way to check whether current code is running in interpreted or compiled mode. VM crash logs show which methods in the stack trace are interpreted or compiled, maybe there's some way to get it in runtime.

Upvotes: 1

Waldheinz
Waldheinz

Reputation: 10487

As far as I know you don't know (from inside the JVM) and can not enforce a method being JITed or not, but using -XX:+PrintCompilation JVM argument you can watch the JIT compiler doing it's work and check if a method gets JITed in that particular run of the program or not.

Upvotes: 6

aioobe
aioobe

Reputation: 421020

So is there somehow to tell JVM that I need a certain method to be JITed.

No, which methods are "JITed" and not is not up to you, and in fact, there is no guarantee that any method will ever be JITed. I suggest you leave these decisions to the JVM.

Upvotes: 1

Related Questions