JVM structure of Lesson2-java virtual machine

Source: Internet
Author: User



On the internet to find a large circle, for the Java Virtual machine, or not clear. This is a more reliable picture. Refer to Java Virtual machine Specification Java SE7 version, study the structure of the next Java virtual machine, shallow understand it. Below, according to this diagram, understand the next sections.


The above is a schematic diagram of a Java virtual machine, which corresponds to a number of JVM mechanisms, such as Java's class loading and execution mechanisms, such as Java's garbage collection mechanism. This article focuses on the structure of a Java virtual machine.
Before we learn about the structure of a Java virtual machine, we'll look at the types of data that can be manipulated in a Java virtual machine.

Similar to the Java language, the data types that the JVM can manipulate can be divided into two categories: primitive type (primitive type, also commonly referred to as native type, base type), and reference type (reference type). The JVM expects as many types of checks as possible to complete before the program runs, that is, the compiler should do its best during compilation to complete possible type checking so that the virtual machine does not need to do so during the run.
The original data types include numeric types, Boolean types, and ReturnAddress types. Where numeric types include integer types and floating-point types.

Integral type has Byte,short,int,long, are signed twos complement, the default is the 0,char type is a 16-bit unsigned integer representation, point to the basic Multilingual plane Unicode code point, to UTF-16 encoding, the default is the Unicode null code point (\ u0000).

The Boolean type is true and false, which defaults to false. A Boolean numeric value that, after compilation, is replaced with the int data type in the JVM. The JVM maps true to a 1,false map of 0.
The returnaddress type represents the opcode of a bytecode instruction, and only that type cannot correspond directly to the data type of the Java language in all primitive types supported by the virtual machine.
Floating-point types it is worth emphasizing that the type contains 5 special values, a positive number of 0, a negative zero, a positive infinity, a negative infinity, and a NaN. In floating-point numbers, integers 0 and 0 are equal, but 1.0/0.0 produces positive infinity results, and 1.0/-0.0 produces negative infinity. Nan is unordered, and any numeric comparison and equivalence test will return false. A non-equivalent comparison of any number and Nan will return true, except for itself.
Reference data types include, class type, array type, interface type, and default value is null. There is a special value in the reference type, NULL. When a reference does not point to any object, its value is null and does not have any actual type without context, but it can be converted to any reference type when there is a context.
The above is a brief introduction to the data types of the virtual machines. The following is an explanation of the run-time data area.
1.PC Registers
The JVM can support multiple threads executing simultaneously, and each JVM thread has its ownof PrivatePC registers. At any moment, a JVM thread executes only one method of code, and the method being executed is called the current method of the thread, and if the method is not native, then the PC register saves the address of the bytecode instruction that the JVM is executing, if the method is native, Then the value of the PC register is undefined. The capacity of the PC register should at least be able to hold a returnaddress type of data or a value of a platform-dependent local pointer.
2.Java Stack
Each JVM thread has its ownof PrivateJava Virtual machine stack, this stack and thread are created at the same time to store stack frames. Similar to traditional language stacks, store local variables and intermediate processes. In addition, the Java stack plays an important role in method invocation and return because the Java stack is no longer affected by other factors except the stack frame and the stack, so the stack frame can be allocated in the heap, and the memory used by the Java stack does not need to be guaranteed to be contiguous. The Java stack can be implemented as a fixed size or dynamically scaled and shrunk according to the calculation. If a fixed size is used, then this capacity should be determined when the JVM thread is created. There are two exceptions for Java Virtual machine stacks: 1. Thread requests allocate more stack capacity than the Java stack, and run out of Stackoverflowerror. 2. If you are dynamically expanding and have tried to get more stack space in the past, but you cannot request enough memory to complete the extension, or when creating a new thread, there is not enough memory to create the corresponding virtual machine stack, and this will run out of OutOfMemoryError.
3.java Heap
Java heap is all JVM threadssharedThe run-time memory area, which is also the area where all class instances and array objects allocate memory. The Java heap is created when the virtual machine is started, and it stores objects managed by the automatic memory management system (garbage collector), which are managed without the need to be destroyed by the display. The capacity of the Java heap can be a fixed size, or it can be dynamically scaled when the program executes, and shrink automatically when too much space is not required. The memory used by the Java heap does not need to be guaranteed to be contiguous. The exception to the Java heap is when the actual amount of memory required exceeds the maximum capacity that the automatic memory management system can provide, and the Java virtual opportunity throws a OutOfMemoryError exception.
4. Method Area
In a Java virtual machine, the method area is available for each threadsharedRun-time memory area. A method area is similar to a compiled code store in a traditional language, or the body segment of an operating system, which stores the structure information for each class, such as the run-time pool, the field and method data, the bytecode for constructors and normal methods, and the special methods that are used in classes, instances, and interfaces for initialization. (Additional explanations will be added later in the special method). The method area is created when the virtual machine is started, and its capacity can be a fixed size, or it can be dynamically scaled when the program executes, and shrinks automatically when there is no need for too much space. The method area can be discontinuous in the actual memory space. If the memory space of the method area does not meet the memory allocation request, the Java virtual machine throws a OutOfMemoryError exception. A run-time pool is a runtime representation of a constant pool table for each class or interface in the class file (the constant pool table is appended to the explanation). It includes a number of different constants, from the compile-time known numeric literal to the method or field reference that must be parsed after the run-time. Each running constant pool is allocated in the method area of the virtual machine. After the classes and interfaces are loaded into the virtual machine, the corresponding run-time-constant pool is created. Throws a OutOfMemoryError exception.
5. Local Method Stack
Java virtual machines may use traditional stacks (C stack) to support native methods, that is, methods written in languages other than the Java language. This stack is the local method stack. The native method stack is also used when the Java Virtual machine uses other languages to implement instruction set interpretation. If the JVM does not support local methods and does not rely on the traditional stack itself, there is no need to support the local method stack, and if so, the stack is typically thread-allocated when threads are created. The local method stack can be implemented as a fixed size or dynamically scaled and shrunk according to the calculation. If a fixed size is used, then this capacity should be determined when the JVM thread is created. There are two types of exceptions: 1. The thread request allocates a stack capacity that exceeds the maximum capacity of the stack and runs out of Stackoverflowerror. 2. If it is dynamically extended and has tried to get more stack space in the past, but cannot request enough memory to complete the extension, or when creating a new thread, there is not enough memory to create the corresponding local method stack, which will run out of OutOfMemoryError. The above is a categorical interpretation of the data area at run time. Some of the concepts just mentioned are explained below.
6. Stack frame
Stack frames are data structures used to store data and partial process results, and are also used to handle dynamic links, method return values, and exception assignments. Stack frames are created with the invocation of the method, and are destroyed as the method ends, regardless of whether the method is completed normally or abnormally, and is counted as the end of the method. The storage space of the stack frame is allocated in the Java Virtual machine stack, each stack frame has its own local variable table, the operand stack, and a reference to the run-time constant pool of the class to which the modification method belongs. The capacity of the local variable table and operand stack is determined at compile time and is saved by the code attribute of the method and provided to the stack frame for use, which is described later in the Code property. Current stack frame, current method, current class concept skipped. If the current method calls another method, or the current method execution ends, the stack frame is no longer the current stack frame. The stack frame is a thread-local private data, and it is not possible to refer to another thread's stack frame in one stack frame.
7. Special Methods
At the Java Virtual machine level, constructors in the Java programming language occur in the form of a special instance named init. Init the name of this method is named by the compile period because it is not a valid Java method name and cannot be implemented by program encoding. The instance initialization method can only be called through the invokespecial instruction of the Java Virtual machine during instance initialization, and the instance initialization method is accessible only when the instance is being constructed. A class or interface can contain up to an initialization method of no more than one class or interface, and the class or interface is initialized by this method. This method is a method that does not contain parameters and returns a type of void, named Clinit.
8. Exception throws
An operation that throws an exception is a precisely defined program control transfer operation in a Java virtual machine. Each method executed by a Java Virtual machine is equipped with 0 to many exception handlers, which describe its effective scope in the method code, the type of exception that can be handled, and where the code that handles the exception is located. To determine whether an exception handler can handle a specific exception, you need to check whether the location where the exception occurred is within the effective scope of exception handling, and whether the exception that occurs is the exception handling type declared by the exception handler or its subtype. When an exception is thrown, the Java virtual machine searches the individual exception handlers contained in the current method and, if found, transfers the control of the code to the branch of the exception handler that is described in handling the exception.

If not found, and the current method call does occur during the exception, then the current method operation of the local variable table, the operand stack will be discarded, and then its corresponding stack frame out of the stack, the thread's method stack back to the method caller's stack frame. Unhandled exceptions will be re-thrown in the caller stack frame of the method, and the process described above is repeatedly repeated throughout the method call chain, and the entire execution thread will be terminated if no proper exception handling is found until the top of the method call stack. The order in which the exception handlers are searched is critical, in the class file, where the exception handler for each method exists in a table, and when the runtime is thrown, the java Virtual opportunity is searched in the order of the exception handlers described in the Exception processor table in the class file. It is important to note that the Java virtual machine itself does not sort the exception handler order, so the semantics of exception handling in the Java language are actually facilitated by the compiler arranging the order of the exception handlers in the table appropriately. The exception handler lookup sequence is explicitly defined in the class file to ensure that the Java virtual machine behaves consistently regardless of the path that the class file is generated from.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JVM structure of Lesson2-java virtual machine

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.