JVM introduction,

Source: Internet
Author: User

JVM introduction,
[Switch] JVM Introduction

1. What is JVM?

JVM is short for Java Virtual Machine (Java Virtual Machine). JVM is a specification used for computing devices. It is a fictitious computer, it is achieved by simulating various computer functions on the actual computer. The Java Virtual Machine includes a set of bytecode instruction sets, a set of registers, a stack, a garbage collection heap, and a storage method domain. JVM shields information related to the specific operating system platform, so that Java programs only need to generate the target code (bytecode) that runs on the Java Virtual Machine ), it can be run on multiple platforms without modification. When JVM executes bytecode, it actually translates bytecode into machine instruction execution on a specific platform.

A very important feature of Java is its independence from the platform. The use of Java virtual machines is the key to achieving this feature. General advanced languages must at least compile different target codes to run on different platforms. After the Java Virtual Machine is introduced, the Java language does not need to be re-compiled 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 ), it can be run on multiple platforms without modification. When executing the bytecode, the Java Virtual Machine interprets the bytecode as a machine instruction execution on a specific platform. This is why Java can "compile once and run everywhere.

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

JRE (JavaRuntimeEnvironment, Java Runtime Environment), that is, the Java platform. All Java programs can run only under JRE. Normal users only need to run the developed java program and install JRE. JDK (Java Development Kit) is a Development Kit used by program developers to compile and debug java programs. JDK tools are also Java programs, and JRE is also required to run. To ensure JDK independence and integrity, JRE is also a part of JDK installation. Therefore, there is a directory named jre under the JDK installation directory to store JRE files. JVM (JavaVirtualMachine, Java Virtual Machine) is part of JRE. It is a fictitious computer that simulates various computer functions on an actual computer. JVM has its own complete hardware architecture, such as processor, stack, and register, as well as corresponding command systems. The most important feature of Java is cross-platform running. JVM is used to support cross-platform operations that are irrelevant to the operating system.

3. JVM principles

JVM is the core and foundation of java. It is a virtual processor between the java compiler and the OS platform. It is an abstract computer implemented using software methods. Based on the underlying operating system and hardware platform, it can execute java bytecode programs on it. As long as the java compiler targets JVM, it generates code or bytecode files that can be understood by JVM. Java source file warp knitting is translated into a bytecode program, and each instruction is translated into a machine code of different platforms through JVM and run on a specific platform. 4. JVM Program Execution Process
1) load. class file 2) manage and allocate memory 3) execute the garbage collection JRE (java Runtime Environment) the runtime environment of java programs constructed by JVM, which is also the environment where Java programs run, but he is also an operating system application and a process, so he also has his own running lifecycle, as well as his own code and data space. JVM is at the bottom layer of the entire jdk and is responsible for operating system interaction. It is used to shield the operating system environment and provide a complete Java running environment. Therefore, it is also a virtual computer. The operating system is installed with jvmthrough java.exe in JDK. The following four steps are used to complete the JVM environment: 1) Create the JVM loading environment and configure 2) load the JVM. dll 3) initialize JVM. dll and bound to the JNIENV (JNI call interface) instance 4) Call the JNIEnv instance to load and process the class. 5. JVM Lifecycle1) The JVM instance corresponds to an independent java program, which is a process-level
A) Start. When a Java program is started, a JVM instance is generated.
The class of the main (String [] args) function can be used as the starting point for running the JVM instance.
B) run. Main () is the starting point of the program's initial thread, and any other threads are started by this thread. There are two types of threads in the JVM: the daemon thread and the non-daemon thread. The main () is a non-daemon thread, which is usually used by the JVM itself, java programs can also indicate that the thread they created is a daemon thread.
C) extinction. JVM exits only when all non-daemon threads in the program are terminated. If the security manager permits, the program can also use the Runtime class or System. exit () to exit 2) the JVM execution engine instance corresponds to the thread of the user running program. It is a line level. 6. JVM Architecture 
  • ClassLoader (used to load. class files)
  • Execution engine (Execution bytecode or execution of local methods)
  • Runtime data zone (method zone, heap, java stack, PC register, local method stack)

7. JVM runtime data Zone

First: PC register

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

Part 2: JVM Stack

The JVM stack is private to the thread. a jvm stack is created for each thread at the same time, variables stored in the JVM Stack are local basic types in the current thread (eight basic types defined in java: boolean, char, byte, short, int, long, float, double) partial returned results and Stack Frame. Non-basic types of objects only store one address pointing to the Stack on the JVM Stack.

Part 3: Heap)

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

(1) The heap is shared by all threads in the JVM. Therefore, the object memory allocation on the heap needs to be locked. This also causes the overhead of the new object to be relatively large.

(2) In order to improve the efficiency of Object Memory Allocation, Sun Hotspot JVM allocates an independent space for the created Thread TLAB (Thread Local Allocation Buffer ), the size is calculated by the JVM Based on the running situation. No lock is required when objects are allocated on the TLAB. Therefore, the JVM tries its best to allocate the memory to the objects in the thread on the TLAB, in this case, the performance of allocating Object Memory in JVM is basically the same as that of C, but if the object is too large, the heap space allocation is still used directly.

(3) TLAB only applies to the new generation of Eden Space. Therefore, when writing Java programs, it is generally more efficient to allocate multiple small objects than large objects.

(4) All newly created objects will be stored in the new Generation Yong Generation. If Young Generation's data survive one or more GC operations, it will be transferred to OldGeneration. New objects are always created in Eden Space.

Area 4: Method Area)

(1) In Sun JDK, this region corresponds to PermanetGeneration, also known as permanent 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 in the class, when developers obtain information through the getName, isInterface, and other methods in the Class object in the program, the data comes from the method region, and the method region is also shared globally, under certain conditions, it will also be GC. When the memory used in the method area exceeds the permitted size, an OutOfMemory error message will be thrown.

Fifth: Runtime Constant Pool)

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

Block 6: Native Method Stacks)

JVM uses the local method stack to support native METHOD execution. This region is used to store the status of each native method call.

8. JVM garbage collection

GC (Garbage CollectionThe basic principle is to recycle objects that are no longer used in the memory. The garbage collection method in GC is called a collector. Because GC consumes some resources and time, after analyzing the lifecycle features of an object, Java collects the object based on the new generation and old generation methods to minimize the pause caused by GC on the application.

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

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

(3) The gc that actively calls System. GC () is executed is Full GC.

GC recycles different object reference types. JVM object references are classified into four types:

(1) strong reference: by default, all objects use strong references (the instances of this object are not referenced by other objects and will be recycled only when GC is used)

(2) soft reference: soft reference is an application provided by Java that is suitable for cache scenarios (GC is performed only when the memory is insufficient)

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

(4) Virtual Reference: Because the Virtual Reference is only used to know whether the object is GC

 

Main content from: http://baike.baidu.com/link? Url = r1DppgYdvfVHc2I0uVBfCgYd0MiNXrSMKU-E3AL_O5yvrQ3fL1FNvpNgS9MUk9H-#4 and http://wenku.baidu.com/link? Url = UXf-aoHl8YCX535q4G2qC48OExWk9ttLaIPW4Qn-GvdeSrM0WSjuAbq_78MJUrHq46ZS-8OsHDCMKkwmJTmXkPrkBZmbNqOA49iDyxsLIkm

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.