Home > Java > javaTutorial > Introduction to JVM

Introduction to JVM

一个新手
Release: 2017-10-10 09:17:51
Original
1919 people have browsed it

JVM Introduction

1. What is JVM?

JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). JVM is a specification for computing devices. It is a fictitious computer that is simulated through simulation on an actual computer. various computer functions. The Java virtual machine includes a set of bytecode instructions, a set of registers, a stack, a garbage collection heap and a storage method field. The JVM shields the information related to the specific operating system platform, so that the Java program only needs to generate the target code (bytecode) that runs on the Java virtual machine, and it can run on a variety of platforms without modification. When the JVM executes bytecode, it actually ultimately interprets the bytecode into machine instructions for execution on the specific platform.

A very important feature of the Java language is its independence from the platform. The use of Java virtual machine is the key to achieving this feature. If a general high-level language wants to run on different platforms, it must at least be compiled into different target codes. After the introduction of the Java language virtual machine, the Java language does not need to be recompiled when running on different platforms. The Java language uses the Java virtual machine to shield information related to specific platforms, so that the Java language compiler only needs to generate the target code (bytecode) that runs on the Java virtual machine, and it can run on multiple platforms without modification. . When the Java virtual machine executes bytecode, it interprets the bytecode into machine instructions for execution on the specific platform. This is why Java can "compile once, run anywhere".

2. What is the relationship between JRE/JDK/JVM?

JRE (JavaRuntimeEnvironment, Java runtime environment), which is the Java platform. All Java programs must run under JRE. Ordinary users only need to run the developed java program and install JRE.

JDK (Java Development Kit) is a development tool kit used by program developers to compile and debug Java programs. JDK tools are also Java programs and require JRE to run. In order to maintain the independence and integrity of JDK, JRE is also part of the installation during the JDK installation process. Therefore, there is a directory named jre under the JDK installation directory, which is used to store JRE files.

JVM (JavaVirtualMachine, Java virtual machine) is part of the JRE. It is a fictional computer that is implemented by simulating various computer functions on an actual computer. JVM has its own complete hardware architecture, such as processor, stack, registers, etc., and also has a corresponding instruction system. The most important feature of the Java language is cross-platform operation. The purpose of using JVM is to support operating system-independent and cross-platform implementation.

3. JVM principle

JVM is the core and foundation of java, a virtual processor between the java compiler and the os platform. It is an abstract computer based on the underlying operating system and hardware platform implemented using software methods, on which Java bytecode programs can be executed.

# The java compiler only needs to be oriented to the JVM and generate code or bytecode files that the JVM can understand. Java source files are compiled into bytecode programs, and each instruction is translated into machine code for different platforms through the JVM and run on a specific platform.

4. The process of JVM executing the program

1) Load the .class file2) Manage and allocate memory3) Perform garbage collection

JRE (java runtime environment) The running environment of the java program constructed by the JVM is also the environment in which the Java program runs. However, it is an application and a process of an operating system at the same time, so it also has its own running life cycle, as well as Own code and data space. JVM is at the bottom of the entire jdk and is responsible for the interaction of the operating system. It is used to shield the operating system environment and provide a complete Java running environment, so it is also a virtual computer. The operating system is loaded into the JVM through Java.exe in the jdk. The JVM environment is completed through the following four steps: 1) Create the JVM loading environment and configuration 2) Load the JVM.dll 3) Initialize the JVM.dll and mount it to JNIENV( JNI calling interface) instance 4) Call the JNIEnv instance to load and process the class class.

5. JVM life cycle

1) The JVM instance corresponds to an independently running java program, which is started at the process level
a). When a Java program is started, a JVM instance is generated. Any class with a public static void
main(String[] args) function can be used as the starting point for running the JVM instance
b). main() serves as the starting point for the initial thread of the program, and any other threads are started by this thread. There are two kinds of threads inside the JVM: daemon threads and non-daemon threads. main() belongs to the non-daemon thread. The daemon thread is usually used by the JVM itself. The java program can also indicate that the thread it created is a daemon thread
c) and die. The JVM will exit when all non-daemon threads in the program terminate; if the security manager allows it, the program can also use the Runtime class or System.exit() to exit

2) The JVM execution engine instance corresponds to the thread belonging to the user running program, which is thread-level

6. JVM architecture

  • ClassLoader (used to load .class files)

  • Execution engine (execute bytecode, or execute local methods )

  • Runtime data area (method area, heap, java stack, PC register, local method stack)

##7. JVM runtime data area

First block: PC register

PC register is used to store each thread The JVM instruction to be executed next, if the method is native, no information is stored in the PC register.

Second block: JVM stack

The JVM stack is private to the thread. When each thread is created, a JVM stack will be created. What is stored in the JVM stack is the current thread. Variables of local basic types (eight basic types defined in Java: boolean, char, byte, short, int, long, float, double), partial return results and Stack Frame, non-basic type objects on the JVM stack Only stores an address pointing to the heap.

The third block: Heap

It is the area used by the JVM to store object instances and array values. It can be considered that all objects created by new in Java All memory is allocated here, and the memory of objects in Heap needs to wait for GC to be recycled.

(1) The heap is shared by all threads in the JVM, so allocating object memory on it requires locking, which also results in the overhead of new objects. It is relatively large

(2) In order to improve the efficiency of object memory allocation, the Sun Hotspot JVM will allocate an independent space TLAB (Thread Local Allocation Buffer) for the created threads, and its size is determined by the JVM according to the running According to calculations, locking is not required when allocating objects on TLAB. Therefore, when the JVM allocates memory to thread objects, it will try its best to allocate it on the TLAB. In this case, the performance of allocating object memory in the JVM is basically the same as that of C. Just as efficient, but if the object is too large, heap space allocation will still be used directly

(3) TLAB only acts on the Eden Space of the new generation, so when writing a Java program, usually multiple small objects It is more efficient to allocate than large objects.

(4) All newly created Objects will be stored in the new generation Yong Generation. If the data of Young Generation survives one or more GCs, it will be transferred to Old Generation. New Objects are always created in Eden Space.

Fourth block: Method Area

(1) In Sun JDK, this area corresponds to PermanetGeneration, also known as the persistent generation.

(2) The method area stores the information of the loaded class (name, modifier, etc.), static variables in the class, constants defined as final type in the class, Field information in the class, and method information. When developers obtain information in the program through getName, isInterface and other methods in the Class object, these data come from the method area. At the same time, the method area is also globally shared. Under certain conditions, it will also be GC, when the memory required by the method area exceeds its allowed size, an OutOfMemory error message will be thrown.

The fifth block: Runtime Constant Pool (Runtime Constant Pool)

stores fixed constant information, method and Field reference information in the class, etc. Its space is allocated from the method area.

Sixth Block: Native Method Stacks

JVM uses native method stacks to support the execution of native methods. This area is used to store each native The status of the method call.

8. JVM garbage collection

The basic principle of GC (

Garbage Collection): Recycle objects that are no longer used in memory. The method used for recycling in GC is called the collector. Since GC needs to consume some resources and time, Java collects objects according to the new generation and old generation after analyzing the life cycle characteristics of the objects, so as to maximize the collection of objects. Possible shortening of the pause caused by GC to the application

(1) The collection of objects in the new generation is called minor GC;

(2) The collection of objects in the old generation is called Full GC;

(3) The GC forced by actively calling System.gc() in the program is Full GC.

Different object reference types, GC will use different methods to recycle. JVM object references are divided into four types:

(1) Strong reference: By default, objects use strong references (instances of this object have no other object references and will be recycled only in GC)

(2) Soft reference: soft Reference is an application provided in Java that is more suitable for caching scenarios (it will only be GCed when the memory is not enough)

(3) Weak reference: it will definitely be recycled by GC during GC

(4) Virtual reference: Since virtual reference is only used to know whether the object has been GC

The main content comes from: http://baike.baidu.com/link? url=r1DppgYdvfVHc2I0uVBfCgYd0MiNXrSMKU-E3AL_O5yvrQ3fL1FNvpNgS9MUk9H-#4 and http://wenku.baidu.com/link?url=UXf-aoHl8YCX535q4G2qC48OExWk9ttLaIPW4Qn-GvdeSrM0WSjuAb q_78MJUrHq46ZS-8OsHDCMKkwmJTmXkPrkBZmbNqOA49iDyxsLIkm

1. What is JVM?

JVM is the abbreviation of Java Virtual Machine (Java Virtual Machine). JVM is a specification for computing devices. It is a fictitious computer that is simulated through simulation on an actual computer. various computer functions. The Java virtual machine includes a set of bytecode instructions, a set of registers, a stack, a garbage collection heap and a storage method field. The JVM shields the information related to the specific operating system platform, so that the Java program only needs to generate the target code (bytecode) that runs on the Java virtual machine, and it can run on a variety of platforms without modification. When the JVM executes bytecode, it actually ultimately interprets the bytecode into machine instructions for execution on the specific platform.

A very important feature of the Java language is its independence from the platform. The use of Java virtual machine is the key to achieving this feature. If a general high-level language wants to run on different platforms, it must at least be compiled into different target codes. After the introduction of the Java language virtual machine, the Java language does not need to be recompiled when running on different platforms. The Java language uses the Java virtual machine to shield information related to specific platforms, so that the Java language compiler only needs to generate the target code (bytecode) that runs on the Java virtual machine, and it can run on multiple platforms without modification. . When the Java virtual machine executes bytecode, it interprets the bytecode into machine instructions for execution on the specific platform. This is why Java can "compile once, run anywhere".

2. What is the relationship between JRE/JDK/JVM?

JRE (JavaRuntimeEnvironment, Java runtime environment), which is the Java platform. All Java programs must run under JRE. Ordinary users only need to run the developed java program and install JRE.

JDK (Java Development Kit) is a development tool kit used by program developers to compile and debug Java programs. JDK tools are also Java programs and require JRE to run. In order to maintain the independence and integrity of JDK, JRE is also part of the installation during the JDK installation process. Therefore, there is a directory named jre under the JDK installation directory, which is used to store JRE files.

JVM (JavaVirtualMachine, Java virtual machine) is part of the JRE. It is a fictional computer that is implemented by simulating various computer functions on an actual computer. JVM has its own complete hardware architecture, such as processor, stack, registers, etc., and also has a corresponding instruction system. The most important feature of the Java language is cross-platform operation. The purpose of using JVM is to support operating system-independent and cross-platform implementation.

3. JVM principle

JVM is the core and foundation of java, a virtual processor between the java compiler and the os platform. It is an abstract computer based on the underlying operating system and hardware platform implemented using software methods, on which Java bytecode programs can be executed.

The java compiler only needs to be oriented to the JVM and generate code or bytecode files that the JVM can understand. Java source files are compiled into bytecode programs, and each instruction is translated into machine code for different platforms through the JVM and run on a specific platform.

4. JVM execution process

1) Load .class file 2) Manage and allocate memory 3) Execute garbage Collection

JRE (java runtime environment) The running environment of the java program constructed by the JVM is also the environment in which the Java program runs. However, it is an application and a process of an operating system at the same time, so it also has its own The running life cycle also has its own code and data space. JVM is at the bottom of the entire jdk and is responsible for the interaction of the operating system. It is used to shield the operating system environment and provide a complete Java running environment, so it is also a virtual computer. The operating system is loaded into the JVM through Java.exe in the jdk. The JVM environment is completed through the following four steps: 1) Create the JVM loading environment and configuration 2) Load the JVM.dll 3) Initialize the JVM.dll and mount it to JNIENV( JNI calling interface) instance 4) Call the JNIEnv instance to load and process the class class.

5. JVM life cycle

1) The JVM instance corresponds to an independently running java program, which is started at the process level
a). When a Java program is started, a JVM instance is generated. Any class with a public static void
main(String[] args) function can be used as the starting point for running the JVM instance
b). main() serves as the starting point for the initial thread of the program, and any other threads are started by this thread. There are two kinds of threads inside the JVM: daemon threads and non-daemon threads. main() belongs to the non-daemon thread. The daemon thread is usually used by the JVM itself. The java program can also indicate that the thread it created is a daemon thread
c) and die. The JVM will exit when all non-daemon threads in the program terminate; if the security manager allows it, the program can also use the Runtime class or System.exit() to exit

2) The JVM execution engine instance corresponds to the thread belonging to the user running program, which is thread-level

6. JVM architecture


  • ClassLoader (used to load .class files)

  • Execution engine (execute bytecode, or execute local methods)

  • Runtime data area (method area, heap, java stack, PC register, local method stack)

7. JVM runtime data area

First block: PC register

The PC register is used to store the JVM instructions that each thread will execute next. If the method is native, no information is stored in the PC register.

Second block: JVM stack

The JVM stack is private to the thread. When each thread is created, a JVM stack will be created. What is stored in the JVM stack is the current thread. Variables of local basic types (eight basic types defined in Java: boolean, char, byte, short, int, long, float, double), partial return results and Stack Frame, non-basic type objects on the JVM stack Only stores an address pointing to the heap.

The third block: Heap

It is the area used by the JVM to store object instances and array values. It can be considered that all objects created by new in Java All memory is allocated here, and the memory of objects in Heap needs to wait for GC to be recycled.

(1) The heap is shared by all threads in the JVM, so allocating object memory on it requires locking, which also results in the overhead of new objects. It is relatively large

(2) In order to improve the efficiency of object memory allocation, the Sun Hotspot JVM will allocate an independent space TLAB (Thread Local Allocation Buffer) for the created threads, and its size is determined by the JVM according to the running According to calculations, locking is not required when allocating objects on TLAB. Therefore, when the JVM allocates memory to thread objects, it will try its best to allocate it on the TLAB. In this case, the performance of allocating object memory in the JVM is basically the same as that of C. Just as efficient, but if the object is too large, heap space allocation will still be used directly

(3) TLAB only acts on the Eden Space of the new generation, so when writing a Java program, usually multiple small objects It is more efficient to allocate than large objects.

(4) All newly created Objects will be stored in the new generation Yong Generation. If the data of Young Generation survives one or more GCs, it will be transferred to Old Generation. New Objects are always created in Eden Space.

Fourth block: Method Area

(1) In Sun JDK, this area corresponds to PermanetGeneration, also known as the persistent generation.

(2) The method area stores the information of the loaded class (name, modifier, etc.), static variables in the class, constants defined as final type in the class, Field information in the class, and method information. When developers obtain information in the program through getName, isInterface and other methods in the Class object, these data come from the method area. At the same time, the method area is also globally shared. Under certain conditions, it will also be GC, when the memory required by the method area exceeds its allowed size, an OutOfMemory error message will be thrown.

The fifth block: Runtime Constant Pool (Runtime Constant Pool)

stores fixed constant information, method and Field reference information in the class, etc. Its space is allocated from the method area.

Sixth Block: Native Method Stacks

JVM uses native method stacks to support the execution of native methods. This area is used to store each native The status of the method call.

8. JVM garbage collection

The basic principle of GC (Garbage Collection): Recycle objects that are no longer used in the memory. Used in GC The method of recycling is called a collector. Since GC needs to consume some resources and time, Java collects objects according to the new generation and old generation after analyzing the life cycle characteristics of the objects to shorten the time as much as possible. The pause caused by GC to the application

(1)The collection of objects in the new generation is called minor GC;

(2)The collection of objects in the old generation is called full GC;

(3) The GC forced by actively calling System.gc() in the program is Full GC.

Different object reference types, GC will use different methods to recycle. JVM object references are divided into four types:

(1) Strong reference: By default, the object uses All are strong references (the instance of this object has no other object references and will be recycled only in GC)

(2) Soft reference: Soft reference is an application provided in Java that is more suitable for caching scenarios ( It will only be GCed when the memory is not enough)

(3) Weak reference: It will be recycled by GC during GC

(4) Virtual reference: Since the virtual reference is only used To know whether the object has been GC

The above is the detailed content of Introduction to JVM. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template