Compile once , Run around

java The slogan that has been publicized is : Compile once , Run around . So how does it work ? Let's look at the picture below :
graph TD java Original procedure --javac compile -->java Bytecode java Bytecode -->jvm virtual machine
jvm virtual machine --java explain -->windows Machine code jvm virtual machine --java explain -->linux Machine code windows Machine code -->windows implement
linux Machine code -->linux implement
java After a compilation of the program , take java The code is compiled into bytecode, that is class file , And then rely on different java virtual machine
Explain , Finally, it is converted to machine code of different platforms , Finally implemented . So we can deduce , If you want to mac Running on the system , Do you just need to install mac java virtual machine
Just do it . So, after understanding the basic principle , Let's take a look , How a program is executed .
public class HelloWorld { public static void main(String[] args) {
System.out.print("Hello world"); } }
This program runs from compile to run , The process is as follows :
graph TD java source code --class file -->java Bytecode java Bytecode -- load jvm.cfg file --> load configuration
load configuration -- according to jvm.cfg Configuration of --> load jvm.dll file load jvm.dll file --> initialization jvm initialization jvm--> obtain JNI Interface
obtain JNI Interface --JNI For local methods, it can directly interact with the operating system --> operation Class file operation Class file --> find main Document execution
jvm Basic structure

Probably through the above description , Everyone is right JVM We have a rough understanding of the operation process , that JVM How to execute one inside class What about the documents ?
graph TD Class file --> Classloader Classloader --> Memory space Memory space -- Runtime Constant Pool --> Method area Method area --> garbage collection GC
Memory space -- Object store -->java heap java heap --> garbage collection GC Memory space -- Local variable table _ Stack frame _ Operands -->java Stack
java Stack -- Thread end auto release --> Thread private Memory space -- Local method library _C language --> Native Method Stack Native Method Stack --> Thread private Memory space --JNI Direct operation --> Out of heap memory
jvm Introduction to memory classification

JVM Memory space contains : Method area ,java heap ,java Stack , Native Method Stack .

*
Method area is the area shared by each thread , Storage information , constant , Static variable .

*
java Heap is also thread sharing
Area of , The instance of our class is in this area , You can imagine that your system will produce many instances , therefore java The heap also has the largest space . If java Insufficient heap space , The program will throw OutOfMemoryError abnormal .

*
java Stack is the private area of each thread , It has the same life cycle as a thread , One thread corresponds to one java Stack , Every time a method is executed, an element is pushed into the stack , This element is called “ Stack frame ”, Stack frame
Local variables in the method are included in , Operation stack for storing intermediate status values , If java Insufficient stack space , The program will throw StackOverflowError abnormal .

Each frame represents a method ,Java Method can be returned in two ways ,return And throw exceptions , Both methods will cause the corresponding frames of the method to stack out and free memory .

*
Local method stack roles and java Stack similar , It's just that it's meant to execute local methods , Method call local method interface stored in local method stack , Final call to local method library , Implementation and operating system , Purpose of hardware interaction .

*

PC register ( Program counter ), At this point, our class has been loaded , Instance object , method , Static variables go where they are , So here comes the question... , How to execute the program , Which method should be executed first , Which method is executed after , The order in which these instructions are executed is PC Register in tube , Its function is to control the execution sequence of program instructions .

The execution engine is based on PC Instruction order of register adjustment , Execute program instructions in sequence .

* Static variable + constant + Class information + Runtime constant pool exists in method area , Instance variable in heap memory .
* Basic types of variables and object reference variables are divided in the function stack memory

Technology
©2019-2020 Toolsou All rights reserved,
Huawei 2021 session Hardware Engineer Logical post (FPGA) Super detailed surface !!!Vue-element-admin upgrade ui edition virtual machine VMware Download and install the most detailed tutorial !C++ Move constructor and copy constructor sound of dripping water java Backstage interview pygame Realize full screen mode and adjustable window size mysql Database setting character set configuration modification my.ini file (windows)30 What's the experience of being a junior programmer at the age of 20 C++ Multithreading programming ( Summary of common functions and parameters )python_ cherry tree