Java Runtime Data area

Source: Internet
Author: User

The Java Virtual machine defines the data regions that are used during the run of the program, some of which are created with the JVM's startup, destroyed as the JVM exits, and others that are one by one-opposed to the thread's running, which are created as the thread begins. Destroyed as the thread ends. The following is a data area model diagram for the Java Runtime:

In general, the Java Runtime Data region can be divided into two parts: a thread-shared area and a thread-exclusive zone. The following one by one summarizes it.

Thread sharing zone: a thread-sharing area is a space area that each thread will use to apply for space and space. It can be divided into two parts according to the specific function, which are Java heap and method area respectively. The following are summarized separately.

1. Java heap: The Java heap is created when the virtual machine is started, and it stores various objects that are managed by the automatic Memory management system (also known as the Garbage Collector), which are not and cannot be destroyed by the display. The instance of the class we created and the array object are stored in this area. The memory used by the Java heap does not need to be guaranteed to be contiguous. Possible exceptions to the Java heap are: if the required heap exceeds the maximum capacity that the automatic memory management system can provide, the Java virtual opportunity throws a OutOfMemoryError exception.

2. Method Area: The method area is also a runtime store for each thread that stores information about the structure of each class , such as the run-time pool, the field and method data, the bytecode content of constructors and normal methods, and so on. The method area is also created as the virtual machine is started, which is a logical part of the Java heap , but the JVM can choose not to do garbage collection and compression in this area. Similarly, if the memory of the method area does not meet the memory allocation requirements, the JVM throws a OutOfMemoryError exception.

3. Run a constant pool: it is the runtime representation of the constants pool table for each class or interface in the class file. It includes a number of different constants, from the compile-time known value literal to the method or field reference that must be parsed at run time. each running constant pool is allocated in the method area of the Java Virtual Machine , and the corresponding run-time pool is created after the classes and interfaces are loaded into the virtual machine. When creating classes and interfaces, the JVM throws a OutOfMemoryError exception if the amount of memory required to construct a run-time pool exceeds the maximum value provided by the method area .

Second, the thread exclusive zone: The thread-exclusive zone is a piece of area that each thread will own separately, which is within the thread and is used to store the area that describes the information inside itself. Similarly, depending on the main functionality it provides, different data storage areas within a thread can be divided into the following sections:

1, PC Register: the PC (program counter) register is used to record the memory address of the executing thread, because a thread can not always enjoy the CPU, when the thread to switch the CPU, it must record the current thread's memory address, To restore the previous memory address when the thread is restarted, at any point in time, a thread executes only one method of code, and the method being executed becomes the current method of the thread. If this method is not native, then the PC register will save the address of the byte code instruction that the JVM is executing, and if the method is native, the value of the PC register is undefined.

2. Local method Warfare: The JVM may use traditional stacks (often called C stacks) to support the execution of the native method, which is a method written in a language other than Java, which is the local method stack (native). The JVM specification allows the local method stack implementations to be fixed size or dynamically scaled and shrunk based on calculations. With a fixed-size local method stack, the local method capacity of each thread can be selected independently when the stack is created.

3, Java Virtual machine stack: that is often said "Java stack", each JVM thread has its own private Java Virtual machine stack, which is created with the thread at the same time to store the stack frame . It is used to store some of the results that have not yet been calculated, and in addition, it plays an important role in the invocation and return of the method. The JVM specification allows both the Java Virtual machine stack to be fixed to an existing size, and also to allow scaling and shrinking based on calculations. With a fixed-size Java Virtual machine stack, the Java Virtual Machine stack capacity of each thread can be selected independently when threads are created. Possible exceptions to the Java Virtual machine stack are: if the thread requests a stack capacity that exceeds the maximum allowable capacity of the Java Virtual machine stack, the Java virtual opportunity throws a stackoverflowerror exception, and if the Java Virtual machine stack can be dynamically extended, The Java virtual machine throws a OutOfMemoryError exception when it tries to expand and cannot request enough memory or creates a new thread without enough memory to create the corresponding VM stack.

4. Stack frame: Stack frame is a data structure used to store data and partial process results. It is also used to handle dynamic linking, method return values, and exception assignments. The stack frame is created as the method is called, and is destroyed as the method ends (whether the method is completed normally or abnormally). The storage space for the stack frame is allocated in the Java Virtual machine stack by the thread that created it. Each stack frame has its own local variable table (locally variable), an operand stack (operand stack), and a reference to the run constant pool of the class to which the current method belongs. At some point in the execution of a thread, only the stack frame of the currently executing method is active, and this stack frame is the current frame, which is called the current method, The class that defines this method is called the current class. Note: The stack frame is a thread-local private data and cannot refer to another thread's stack frame within one stack frame .

5, local variable table: Also known as the local variable table, which is used to store a list of local variables. The length of a local variable table in a stack frame is determined by the compilation period and stored in a binary representation of the class or interface. A local variable can hold a data type of type Boolean, Byte, char, short, int, float. Two local variables can hold data of a type long or double. The JVM uses a local variable table to pass the parameter when a method call is completed .

6, the operation of the stack: each stack frame contains an operand stack, the stack frame is just created, the operand stack is empty . The JVM provides bytecode directives to copy constants or variables from a local variable table or an instance of an object into the operand stack, as well as instructions to take data from the operand stack, manipulate the data, and re-enter the results of the operation into a stack. At any given moment, the operand stack will have a certain stack depth, and a long, double type of data will take up to two depth of stack depth, while other data will occupy a stack depth.

6. Dynamic linking: Each stack frame contains a reference to a running constant pool of the type of the current method that is dynamically linked to the current code. A method that calls other methods, or accesses member variables, is represented by a symbolic reference (symbolic Reference), which is the function of a dynamic link to convert the method represented by a symbolic reference to a direct reference to the actual method. Because of late binding of methods and variables in other classes (late binding), they are not affected by the way they are called, even if those classes change.

The above summary from the "Java Virtual Machine Specification", feel the content is quite a lot of, refueling!

Java Runtime Data area

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.