1

I had this sort of concern: if a method contains a while-true loop, it is only called once and it is interpreted, then it will execute in the interpreter forever and kill performance. I first suspected this when I'm testing an AOT-Compiled Minecraft version called libminecraft 1.14.4 native next generation. I used OpenJDK 13 + JVMCI and saw better peak performance. I fully understand that Minecraft had a lot of while-true loops running on multiple threads, so when I ran another test with inline-then-optimize Whole-Program Optimization, it gave horribly bad performance unless AOT-Compiled (The non-optimized version did well in the non-AOT test with the exact same OpenJDK version). Is is actually true that if a Method with a while-true loop gets invoked only once, it will stay in the interpreter for the remaining lifetime of it's thread? I can't run something as big as Minecraft with -XX:+PrintCompilation to tell.

Jessie Lesbian
  • 1,273
  • 10
  • 14

2 Answers2

2

A method with a long running loop can be JIT-compiled, too.

HotSpot JVM has a technique called on-stack replacement:

Also known as 'OSR'. The process of converting an interpreted (or less optimized) stack frame into a compiled (or more optimized) stack frame. This happens when the interpreter discovers that a method is looping, requests the compiler to generate a special nmethod with an entry point somewhere in the loop (specifically, at a backward branch), and transfers control to that nmethod.

Most compiler features/optimizations are valid for OSR compilation just like for a regular compilation. However, there are cases (1, 2) when OSR stubs appear not as optimized as a fully compiled method. In a real application though, it's not a common case when a long running loop does not call other methods, so OSR is rarely a performance issue.

apangin
  • 92,924
  • 10
  • 193
  • 247
-3

As a general programming practice while-true loop is not a good thing to use or at best try an look for some alternatives.

Interpreter does not store any code it just executes the bytecode ,

The key point here is that so long as the threads entry function does not exit/return, the thread will stay in existence. However this does not necessarily mean that the thread has to be actively executing code.

More over there are various way to implement the while true loop if you do not want to be executed even once before the breaking condition is meant you can try using while(!<<some-condition>>) instead of while(true)

Sambit Nag
  • 38
  • 5
  • 2
    There is nothing wrong with a `while (true)` loop if that is the natural expression of the controlling condition. The HotSpot JVM does indeed store code: it converts hotspots in the code to native code and executes that. – user207421 Jun 10 '20 at 05:24
  • i am not saying ```while(true)``` is wrong but it requires proper break condition and in terms of reliability it is always easy to miss that ```while(true)``` will be executed at least once – Sambit Nag Jun 10 '20 at 05:30