Java leak detection series (1) -- JVM

Source: Internet
Author: User

I haven't worked on Java for more than a year. I recently studied hadoop, so I want to repeat java. In the past, I mainly focused on Java syntax and APIs, and seldom focused on the underlying memory, so now I am looking for time to pick it up.

JVM, full name: Java virtual machine, that is, the Java Virtual Machine. Specifically, JVM can be regarded as an imaginary machine, which is realized through software simulation on an actual computer. It has its own hardware, such as the processor, stack, and register, you can also use your own command system. JVM has a clear task in its life cycle, that is, to run Java programs. Therefore, when a Java program is started, an instance of JVM is generated. When the program runs, the instance also disappears. The following describes the JVM in terms of its position in the system, the JVM architecture, and the JVM running process.

1. Location of JVM IN THE SYSTEM

First, from a macro perspective, JVM is a software running on a specific operating system, as shown in:

Throughout the Java platform, JVM is a component between a Java application and a specific operating system. The location of JVM on the Java platform is shown in:

In the structure of the Java platform, it can be seen that the Java Virtual Machine (JVM) is at the core position and is the key for programs to be irrelevant to the underlying operating system and hardware. Below it is A porting interface, which consists of an adapter and a Java operating system. The platform-dependent part is called an adapter; JVM is implemented on a specific platform and operating system by porting interfaces. Above the JVM is the basic Java class library, extended class library, and their APIs, applications and applets compiled using Java APIs can run on any Java platform without considering the underlying platform,
This is because the Java Virtual Machine (JVM) achieves the separation of the program and the operating system, thus realizing the platform independence of Java.

2. JVM Architecture

Each JVM has two mechanisms: one is to load a class (class or interface) with the appropriate name, called class loader; the other one is responsible for executing commands contained in the loaded classes or interfaces, called execution engine. Each JVM includes Method Area, heap, stack, PC register, and native.
The five parts of method Stack are composed of the following components:

This figure refers to the JVM composition diagram widely circulated on the Internet. It can be seen from this figure that the entire JVM is divided into the following four parts:

1) Class Loader)

The function of the Class Loader is to load the class file to the memory. For example, to compile a helloword. Java program and compile it into a class file through javac, how can it be loaded into the memory and executed? Class Loader assumes this responsibility and does not create one at will. the class file can be loaded. The class file loaded by the class loader has the format requirements. In addition, the Class Loader only loads the file as long as it conforms to the file structure. As for whether it can be run, it is not the responsibility of executionengine.

2) execution engine (execution engine)

The execution engine is also called an interpreter. It is responsible for interpreting commands and submitting them to the operating system for execution. The execution engine is at the core of JVM. in Java Virtual Machine specifications, its behavior is determined by the instruction set. The Java instruction set is equivalent to the Java program assembly language. The commands in the Java Instruction Set contain a single-byte operator, which is used to specify the operation to be executed. There are also 0 or multiple operands to provide the parameters or data required for the operation. Many Commands have no operands and are composed of only one single-byte operator.

The execution process of the internal loop of the virtual machine is as follows:

Do {Get An operator byte; execute an action based on the operator value;} while (the program is not finished );

Because of the simplicity of the command system, the virtual machine execution process is very simple, which is conducive to improving the execution efficiency.

3) Native Interface (Local interface)

The role of the local interface is to integrate different programming languages for Java. Its original intention is to integrate C/C ++ programs. Java was born when C/C ++ went viral, to establish a foothold, you must have a smart and wise call of the C/C ++ program. Therefore, the Code marked as native is opened up in the memory, the specific method is to register the native method in native methodstack and load nativelibraies during executionengine execution. Currently, this method is used less and less unless it is a hardware-related application, such as a printer driven by a Java program or a java system to manage production devices, because the communication between heterogeneous fields is very developed, such as socket communication or web
Service.

4) runtime data area)

The runtime data zone is the focus of the entire JVM. All the programs we write are loaded here and then run. The Java ecosystem is so prosperous that it is benefited from the excellent autonomy of the Region. The method area and heap are based on JVM instances, that is, each JVM instance has its own method domain and a heap, all threads running in JVM share these regions. When a virtual machine loads class files, it parses the class information contained in the binary data and places them in the method domain; when the program runs, JVM places all objects initialized by the Program on the stack. PC
Register and stack Are thread-based. Each thread has its own program counter and java stack when it is created. The value of the program counter points to the next command to be executed, the java stack of the thread stores the status of the Java method called by the thread. The local Method Invocation status is stored in the nativemethod stack, which depends on the specific implementation.

Since the runtime data zone is the focus of the entire JVM, we will introduce it in detail in the next section.

3. JVM running process

The Java source file is compiled and compiled by the Java compiler to generate a bytecode file (. Class). To execute the bytecode file in JVM, You need to perform the following three steps: Loading, linking, and initialization. The three steps are described as follows:

Loading: the class is loaded by the class loader. The function required by the class loader is to define a Java class, that is, to convert the Java bytecode into the Java in JVM. lang. class Object. All Java classes in the JVM are represented by Java. Lang. class objects.

Link: a link to a Java class refers to the process of merging the binary code of the Java class into the running state of the JVM. The class must be loaded successfully before the link is linked. Class links include verification, preparation, parsing, and other steps. Verification is used to ensure that the binary representation of the Java class is completely correct in structure. If an error occurs during the verification process, a java. Lang. verifyerror error will be thrown. The preparation process is to create static fields in the Java class and set these fields as the default values. The preparation process does not execute code. A Java class contains references to other classes or interfaces, including its parent class, Implemented interfaces, formal parameters of methods, and returned Java classes, the parsing process ensures that these referenced classes can be correctly found. The parsing process may cause other Java classes to be loaded.

Initialization: When a Java class is actually used for the first time, JVM will initialize the class. The main operation in the initialization process is to execute static code blocks and initialize static fields. Before a class is initialized, its direct parent class also needs to be initialized.

The following is an example to illustrate the JVM running process:

class HelloApp {public static int x = 10;public static void main(String[] args) {System.out.println(x);}static{x = 30;}}

The execution process is as follows:

He started to execute the main method of the helloapp class and found that the class was not loaded. That is to say, the virtual machine does not currently contain the binary representation of the class, so the virtual machine uses classloader to find such a binary representation. If the process fails, an exception is thrown. Before the main method is called, the class helloapp must be linked to other types and initialized. In the above program, the static field is X, and the static code block is also assigned to X. After initialization, the value of X is 30.

The next section focuses on the runtime data area in JVM.

(Sina Weibo: @ quanliang _ machine learning)

 

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.