[Deep understanding JVM One]---Java program execution Flow __java

Source: Internet
Author: User
Tags garbage collection

Reprint: https://blog.csdn.net/sinat_33087001/article/details/76977437

This is the first of a "deep Understanding JVM" series blog that aims to control the whole process first, and then step through it. Most of this blog content comes from http://www.cnblogs.com/dqrcsc/p/4671879. Htmljava some places to rearrange, based on their own understanding of the content-tml Overview

Program execution process I divide it into the following steps: Editing the source code, compiling the class file, (Loading the class file, running the class bytecode file), where the last two steps are performed on the JVM virtual machine.
edit process description

Edit the source code, that is, we write the source code on any tool, can be Notepad, and finally named Student.java.

this is equivalent to creating a new. Java class and writing content in an IDE like MyEclipse. source Files

Class Person {private String name;



       private int age;

              public person (int age, String name) {this.age = age;

       THIS.name = name;

public void Run () {}} interface istudyable {public int study (int a, int b);

       //public class, with the same name as the Java file public class Student The extends person implements istudyable {private static int cnt=5;

       static{cnt++;

       Private String SID;

              Public Student (int age, string name, String sid) {super (age,name);

       This.sid = SID;

       public void Run () {System.out.println () "Run () ...");

              public int study (int a, int b) {int c = 10;

              int d = 20;

       return a+b*c-d;

       public static int getcnt () {return CNT; public static void Main (string[] args) {Student s = new StudeNT ("DQRCSC", "20150723");

              S.study (5,6);

              Student.getcnt ();

       S.run (); }

}
1-2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40-41 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 the very same compiling Process Description

Generate the. class bytecode file, and enter the command Javac Student.java compile the source file to generate the. class bytecode file. Because two classes are defined in the source file, an interface, 3. clsss files are generated.

This part of the operation is equivalent to the way we write code on an IDE like MyEclipse ctrl+s Save byte code file

Bytecode files, seemingly trivial things, but really implement the Java language across the platform. The same program storage format is used uniformly by virtual machines of various platforms. Further, the JVM is running a class bytecode file, as long as it is a file in this format, so the JVM is not as tightly bundled with the Java language as I had imagined before. If you are very familiar with the format requirements of bytecode, you can use the binary editor to write a byte-code file that matches your requirements, and then give it to the JVM to run, or compile the source code in other languages into a bytecode file and run it to the JVM, and the JVM will run correctly as long as it's a legitimate bytecode file. So, it also implements Cross-language ... The following is a bytecode file Student.class.txt:


Part of the class file content, from the image above, You can see that this information comes from Student.class, compiled from Student.java, the compiler's major version number is 52, which is jdk1.8, this class is public, and then the constant pool of constants in the class, the byte code of each method, etc.

it holds a variety of information about this class: Fields, methods, parent classes, implemented interfaces, and so on. run process description

When you enter the Java Student command at the command line, you start a Java virtual machine, then load the Student.class bytecode file into memory, and then run the byte code instruction in memory.

This part of the operation is the equivalent of clicking the Run button on an IDE like MyEclipse. Introduction to the basic structure of the JVM

Runtime memory partitioning and overflow processing for JVMS see my second post in this series (Java Bottom analysis-JVM memory analysis) http://blog.csdn.net/sinat_33087001/article/details/76976027, there is a specific description, here simply.

The JVM divides memory into a method area, a Java stack, a Java heap, a local method stack, and a PC register 5 part of the data region.
Method area: Used to store metadata information for classes, interfaces, and byte code data loaded in are stored in the method area
Java stack (virtual machine stack): Runtime memory area when the execution engine runs bytecode, save the call run data for each method in the form of a stack frame
Local method stack: Runtime memory area when the execution engine calls a local method
Java Heap (heap): Runtime data area, where various objects are generally stored on the heap
PC Register (program counter): functions as a PC register in the CPU, indicating the byte code instructions to execute.

the JVM's functional modules mainly include class loaders, execution engines, and garbage collection systems. class load

Load phase
1 The class loader finds the file in the specified classpath (through the class's fully qualified name), and then reads the data in the byte stream and stores it in the method area.
2) According to Student.class information to create a class object, this object is more special, generally also stored in the method area, used as a run-time access to the student class of the various data interface.
Verification phase:
3 The necessary verification work, format, semantics, etc.
Preparation phase:
4 allocates the memory space for the static field in the student, is also in the method area, and carries out 0 initialization, that is, the numeric type is initialized to 0,boolean to False, the reference type is initialized to null, and so on.

              
1

At this point, the assignment of 5 is not performed, but it is initialized to 0.
Parsing phase
5 because it has been loaded into memory, the original bytecode file in some of the methods, fields, and other symbolic references can be resolved to its direct reference in memory, and not necessarily wait until the actual runtime to parse.
Initialization phase
6 because it has been loaded into memory, the original bytecode file in some of the methods, fields, and other symbolic references can be resolved to its direct reference in memory, and not necessarily wait until the actual runtime to parse.
There is only one static field in the Student.java:

The parent class and its implementation interface to which a class loads before loading

It is not until line No. 390 that the part of your definition is loaded, first student the implemented interface Istudyable, then the parent person, then the student itself, then the load of a startup class, and then the main () method, executed. run byte code directives

The execution engine finds the main () entry method and executes the byte code instruction in it:
Only the stack frame of the currently running method is on top of the stack. The current method returns the stack frame for the current method, and the current method's caller's stack frame becomes the top of the stack; if other methods are invoked in the method body of the current method, the stack frame is created for the method being called and pressed onto the stack.

Simply view the running process of Student.main ():

   public static void Main (string[] args) {

              Student s = new Student ("DQRCSC", "20150723");

              S.study (5,6);

              Student.getcnt ();

              S.run ();

}
1 2 3 4 5 6 7 8 9 10 11


Mximum stack depth Specifies that the current method, the main () method, corresponds to the maximum depth of the operand stack in the stack frame with the current value of 5

Maximum Local variables Specifies the size of the locals table in the main () method, currently 2, and has two slot for the method's parameters and local variables.

Code length specifies the lengths of the codes in the main () method.
The implementation process is as follows:

1, create a stack frame for the Main method:

The local variable table length is 2,slot0 storage parameter args,slot1 storing local variable student s, the maximum depth of operand stack is 5.

2,new#7 instruction, creates a student object in the Java heap and puts its reference value on the top of the stack.

3, initializes an object (by the way the instance is constructed)

UP directive: Copies the value of the top of the stack, and then the copied results into the stack.

Bipush 23: A single byte constant value of 23 into the stack.

LDC #8: #8 The constant in this constant pool, "DQRCSC", is taken out and merged into the stack.

LDC #9: #9 The constant in this constant pool, which is "20150723", is taken out and merged into the stack.

4,invokespecial #10: Invoke the method represented by #10 this constant, that is, student. () This method, this step is to initialize the object s value

The <init> () method is a method that the compiler will use to invoke the <init> () statement of the parent class, construct code blocks, instance field assignment statements, and the statements in the construction method that you write. Ensure that the <init> () method that invokes the parent class is at the very beginning, and the constructed method statement that you write is at the end, while the building code block and the Instance field assignment statements are consolidated in sequence in the <init> () method in the order in which they appear.

Note that the maximum operand stack depth of the student.<init> () method is 3, and the local variable table size is 4.

Note At this point: from DUP to LDC #9这四条指令向栈中添加了4个数据, and student. () method just needs 4 parameters:

Public Student (int age, string name, String sid) {

              super (age,name);

              This.sid = SID;

}
1 2 3 4 5 6 7

Although only 3 parameters are explicitly defined in the definition, and a reference to the current object is implicitly passed as the first argument, the four arguments are this,age,name,sid in turn.

The 4 instructions above just put the values of the four parameters into the stack, pass the parameters, and then call the Student.<init> () method, which creates the stack frame of the method and merges it into the stack. The No. 0 to 4th slot of the local variable table in the stack frame holds the four parameter values in the stack respectively.

Create a stack frame for the studet.<init> () method:

Byte code directive in the Student.<init> () method:


ALOAD_0: Place the reference value of the local variable table slot0 into the stack

Aload_1: Place the int value of the local variable table slot1 into the stack

Aload_2: Place the reference value of the local variable table Slot2 into the stack

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.