简体   繁体   中英

when compile-time ends and runtime starts for the JVM process

I am new to JVM, and was wondering the concepts of compile time and runtime with regards to the JVM class loading and execution phases. So, during the JVM process, from which point, the compilation ends and from which point the runtime starts. For JVM process, I mean the process of ClassLoader (loading ---> linking ---> initialization) <-----> Runtime Data Area <-----> Execution Engine (interpreter ---> JIT Compiler ---->GC) .

Lets be clear:

  1. We are talking about a Hotspot-based JVM. (Other JVM architectures may behave differently.)

  2. We are talking about JIT compilation, not Java source code compilation. (Java source code compilation occurs before the bytecodes are loaded, and typically 1 happens before the JVM even starts.)

When compile-time ends and runtime starts for the JVM process.

In reality, class loading, JIT compilation, garbage collection and so on all happen at runtime. There are no phases... as you imagine.

(I would be interested to know what your source of information was. As @Holger notes, it seems like it is very out of date.)

The life cycle of a class is roughly as follows:

  1. Classes are loaded and then linked as required.

  2. At some point, something will trigger the initialization of a class. The initialization is performed by the bytecode interpreter executing the classes <clinit> pseudo-method.

  3. The first time that any method is called, it is executed using the interpreter.

  4. As execution continues, the interpreter gathers and stores runtime stats about the methods that it is executing.

  5. Once an execution threshold has been reached for a given method 2 , the JVM triggers JIT compilation to native code.

  6. Once JIT compilation of a method has completed, the JVM switches to using the compiled native code version of the method. The switch can happen during a call to the method.

  7. Under some circumstances, the JVM may even tell the JIT compiler to recompile a method that it has already compiled.


While all this is going on, the JVM will be managing the heap, running the GC, finalizing objects, processing Reference queues, and so on.

Under normal circumstances 3 , Java classes remain reachable for the lifetime of the program, and are not garbage collected.


1 - It is possible compile and load Java source code or generate and load bytecodes at runtime. These things are beyond the scope of this Q&A.
2 - Indeed, a method may never reach this threshold. The point is that there is no benefit in JIT compiling a method that contributes virtually nothing to overall performance.
3 - A class may be unloaded and garbage collected under certain circumstances if it was loaded via a dynamically created classloader. This is beyond the scope of this Q&A.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM