简体   繁体   English

当 JVM 进程的编译时间结束和运行时间开始时

[英]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.我是 JVM 的新手,想知道关于 JVM class 加载和执行阶段的编译时间和运行时概念。 So, during the JVM process, from which point, the compilation ends and from which point the runtime starts.因此,在 JVM 过程中,编译结束,运行时开始。 For JVM process, I mean the process of ClassLoader (loading ---> linking ---> initialization) <-----> Runtime Data Area <-----> Execution Engine (interpreter ---> JIT Compiler ---->GC) .对于JVM进程,我指的是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.我们谈论的是基于热点的 JVM。 (Other JVM architectures may behave differently.) (其他 JVM 架构可能表现不同。)

  2. We are talking about JIT compilation, not Java source code compilation.我们说的是JIT编译,不是Java源代码编译。 (Java source code compilation occurs before the bytecodes are loaded, and typically 1 happens before the JVM even starts.) (Java 源代码编译发生在字节码加载之前,通常1发生在 JVM 甚至启动之前。)

When compile-time ends and runtime starts for the JVM process.当 JVM 进程的编译时间结束并且运行时间开始时。

In reality, class loading, JIT compilation, garbage collection and so on all happen at runtime.实际上,class 加载、JIT 编译、垃圾回收等都发生在运行时。 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.) (我很想知道你的信息来源是什么。正如@Holger 所说,它似乎已经过时了。)

The life cycle of a class is roughly as follows: class的生命周期大致如下:

  1. Classes are loaded and then linked as required.加载类,然后根据需要链接。

  2. At some point, something will trigger the initialization of a class.在某些时候,某些东西会触发 class 的初始化。 The initialization is performed by the bytecode interpreter executing the classes <clinit> pseudo-method.初始化由执行类<clinit>伪方法的字节码解释器执行。

  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.一旦达到给定方法2的执行阈值,JVM 就会触发 JIT 编译为本机代码。

  6. Once JIT compilation of a method has completed, the JVM switches to using the compiled native code version of the method.方法的 JIT 编译完成后,JVM 将切换到使用该方法的已编译本机代码版本。 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.在某些情况下,JVM 甚至可以告诉 JIT 编译器重新编译它已经编译的方法。


While all this is going on, the JVM will be managing the heap, running the GC, finalizing objects, processing Reference queues, and so on.在这一切进行的同时,JVM 将管理堆、运行 GC、完成对象、处理Reference队列等。

Under normal circumstances 3 , Java classes remain reachable for the lifetime of the program, and are not garbage collected.在正常情况下3 ,Java 类在程序的生命周期内保持可达,并且不会被垃圾回收。


1 - It is possible compile and load Java source code or generate and load bytecodes at runtime. 1 - 可以编译和加载 Java 源代码或在运行时生成和加载字节码。 These things are beyond the scope of this Q&A.这些东西超出了本问答的scope。
2 - Indeed, a method may never reach this threshold. 2 - 事实上,一种方法可能永远不会达到这个阈值。 The point is that there is no benefit in JIT compiling a method that contributes virtually nothing to overall performance.关键是 JIT 编译一个对整体性能几乎没有贡献的方法没有任何好处。
3 - A class may be unloaded and garbage collected under certain circumstances if it was loaded via a dynamically created classloader. 3 - 如果通过动态创建的类加载器加载 class,则在某些情况下可能会卸载并收集垃圾。 This is beyond the scope of this Q&A.这超出了本问答的 scope。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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