A beginner's first experience of JVM entry

     By the way, there are many versions of jdk now. A glance at the official website: https://blogs.oracle.com/java-platform-group/the-arrival-of-java-13 is  already 13. I rarely paid attention to professional official websites before. , I learned second-hand information, and I don’t know if it’s right, so I should go to the official website, the official website is the authority (I am a scumbag in English, looking at the official website is a sad reminder). Okay, let’s get back to the subject, today we will talk about JVM together. It’s a little bit of my own opinion, and I would like to ask you all to advise me..

1. The first thing you need to understand is the relationship between JDK, JRE and JVM in Java?

 Here is a detailed description of a big guy: https://www.cnblogs.com/iskandar/p/8933340.html

I believe you all know this content.

2. How is it transformed and recognized by JVM? How does the java source code enter the JVM?

  a. Compile: javac Test.java ---> Test.class (source code --> class file)

cmd -> javac Test.java This process is compiled into a class file (Test.class). What kind of process is this?

   Test.java -> Lexical Analyzer -> tokens Stream -> Syntax Analyzer -> Syntax Tree/Abstract Syntax Tree -> Semantic Analyzer -> Annotation Abstract Syntax Tree -> Bytecode Generator -> Test.class File 

So how does our JVM understand these contents? There should be a corresponding relationship for conversion. Address (we also know how this corresponds through this address) https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html

 magic(魔数): cafe babe (固定值)
The  magic  item supplies the magic number identifying the  class  file format; it has the value  0xCAFEBABE . 

0000 0034 corresponds to 52 in decimal, which represents a version in JDK 8.

b. Class file to virtual machine (class loading mechanism

    1. Load to find and import class files

   (1) Obtain the binary byte stream that defines this class through the fully qualified name of a class (2) Convert the static storage structure represented by this byte stream into the runtime data structure of the method area (3) Generate in the Java heap A java.lang.Class object representing this class serves as the access entry to the data in the method area

   2. Link

   3. Verify (Verify) to ensure the correctness of the loaded class, such as: file format verification, bytecode verification, symbol reference verification, etc.

   4. Prepare (Prepare) allocate memory for the static variables of the class and initialize it to the default value. For example: private static int a=10 (in this step, a=0 will be assigned first) 

   5. Resolve (Resolve) Convert the symbol references in the class into direct references, in layman's terms: Convert these identification symbols into actual direct references (actual physical addresses)

   6. Initialize (Initialize) initialize the previously prepared a=0 to: a=10

See picture:


 

This place needs to be said: class loading

In the Load phase, the step (1): Obtain the defined binary byte stream through the fully qualified name of the class, which needs to be completed with the help of a class loader. As the name implies, it is used to load the Class file. (1) Obtain the binary byte stream that defines this class through the fully qualified name of a class 

When loading a class, you need to pay attention to this place: each time you load it, you need to load the parent class first, and only find yourself if the parent class does not (personal understanding). The official statement is called: Parental Delegation Mechanism

3. After loading, you have entered the JVM, here is the key point, there is a concept called: Run-Time Data Areas ( Run-Time Data Areas)

To put it bluntly, after the class file is loaded by the class loader, the contents of the class (such as variables, constants, methods, objects, etc.) must have a place, that is, to store , the storage location must be in the JVM Corresponding space

Heap (heap)   - (JVM memory heap memory) There is only one area for JVM, and the real thread shared area (threads are unsafe), mainly storing class objects and arrays. Heap memory is divided into: Yang (Eden, s0(from), s1(to), ratio (8:1:1) in order to better deal with memory fragmentation and ensure the continuity of memory space), old
Method Area (method area) )  - (Non-heap memory of JVM memory is permanent generation) There is only one area for JVM and real thread sharing (threads are unsafe), mainly storing static variables, class information, constants, and code compiled by the just-in-time compiler Wait.
R & lt UN-Time by Constant Pool (runtime constant pool) , belonging to the thread level, and store a variety of literal symbols compile generated reference, this section will always enter the operating method of the amount of the pool area is stored in the class loader
PC Register (program counter) facilitates the switching between threads when there are multiple threads, mainly records the execution step of the thread, etc., saves the address of the currently executing JVM instruction, in order to restore the correct execution position after the thread is switched , Each thread needs to have an independent program counter (thread private). 
Java virtual machine stack  (thread level). The area of ​​the Java stack is very small, only 1M. It is characterized by fast access speed, so all tasks stored in the stack are fast-executing tasks, basic data types, and object references (Reference), ① Push or pop the stack in units of frames; ② Set by -Xss, if not enough, StackOverflowError will be thrown. Each method executed by a thread is a stack frame in the stack, that is, each method corresponds to a stack frame. When a method is called, a stack frame is pushed onto the stack; when a method is called, the stack frame is popped from the stack of the
local method stack ( thread level), and the address of the native method's entry area is saved.

To be continued.........

If there is something wrong with the above description, please point it out, thank you!!

Guess you like

Origin blog.csdn.net/u010200793/article/details/103540680