JVM memory partition and Object Instantiation Analysis

Source: Internet
Author: User
1. Specific JVM Program Execution Process

Since Java programs are executed by JVM, when we talk about Java memory partition, it actually refers to JVM memory partition. Before discussing JVM memory partition, let's take a look at the specific execution process of the Java program:

As shown in, the Java source code file (. after being compiled into a bytecode file (. class suffix), and then the class loader in the JVM loads the bytecode files of each class. After loading, It is executed by the JVM execution engine. Throughout the program execution process, JVM uses a space to store the data and related information required during the program execution, this space is generally referred to as the runtime data area, which is also known as the JVM memory. Therefore, the memory management we often talk about in Java is to manage this space (how to allocate and recycle memory space ).

Ii. runtime data Zone

According to the Java Virtual Machine specification, the runtime data zone usually includes the following parts: program counter register and java stack) native method stack, Method Area, and heap ).


As shown in, the runtime data zone in jvm should include these parts. Although the JVM specification specifies that the data zone should include these parts when the program runs during execution, there is no provision on how to implement them, different virtual machine manufacturers can have different implementation methods.

Iii. Data stored in each part of the runtime data Zone

Next, let's take a look at each part of the data zone in the running hours, which is used to store data during program execution.

1. Program counters

Program counter register is also called as a PC register. A friend who has learned assembly language is familiar with the concept of program counters. In assembly language, program counters refer to registers in the CPU, it stores the address of the instruction currently executed by the Program (or the address of the storage unit where the next instruction is stored). When the CPU needs to execute the instruction, you need to obtain the address of the storage unit where the command to be executed is located from the program counter, and then obtain the Command Based on the obtained address. After obtaining the command, the program counter will automatically add 1 or get the address of the next instruction based on the transfer pointer, so that the loop ends until all the instructions are executed.

Although the program counters in JVM are not as physical-concept CPU registers as program counters in assembly languages, however, the program counter function in JVM is logically equivalent to the program counter function in assembly language, that is, it is used to indicate which command to execute.

In JVM, multithreading obtains the CPU execution time by switching threads in turn. Therefore, at any specific time, the kernel of a CPU executes only one instruction in the thread. Therefore, to enable each thread to resume the execution position of the program before the switch after the thread switch, each thread must have its own program counter and cannot be disturbed by each other, otherwise, the normal execution order of the program will be affected. Therefore, the program counter is private to each thread.

The JVM specification specifies that if the thread executes a non-native method, the program counter stores the address of the instruction to be executed. If the thread executes a native method, the value in the program counter is undefined.

Because the space occupied by the data stored in the program counter does not change with the execution of the program, there is no memory overflow (outofmemory) in the program counter.

2. java stack

Java stack is also called a virtual machine stack (Java vitual machine stack), which we often call a stack. It is similar to the stack in the C data segment. In fact, java stack is the memory model for Java method execution. Why? The following describes the causes.

The java stack stores stack frames. Each stack frame corresponds to a called method. The stack frame includes the local variables and operand stacks) reference to runtime constant pool) return address and some additional information. When a thread executes a method, a corresponding stack frame is created and the stack frame is pressed. After the method is executed, the stack frame is output from the stack. Therefore, the stack frame corresponding to the method currently executed by the thread must be at the top of the java stack. At this point, we should understand why stack memory overflow is easily caused when recursive methods are used and why the stack space does not need to be managed by programmers (of course, in Java, programmers do not need to worry about memory allocation and release, Because Java has its own garbage collection mechanism). The allocation and release of this part of space are automatically implemented by the system. For all programming languages, the stack space is not transparent to programmers. Indicates a java stack model:


The local variable table, as its name implies, does not need to be explained. you should understand its role. Is used to store local variables in the method (including non-static variables declared in the method and function parameters ). For a variable of the basic data type, its value is directly stored. For a variable of the reference type, the reference to the object is stored. The size of the local variable table can be determined by the compiler. Therefore, the size of the local variable table does not change during program execution.

The operand stack must have learned the stack in the data structure. It is certainly not unfamiliar with the expression evaluate problem. The most typical application of the stack is to evaluate the expression. Think about the process of executing a method in a thread, which is actually the process of continuously executing statements. In the final analysis, it is the process of computing. So it can be said that all the computing processes in the program are completed through the operand stack.

Point to the reference of the runtime constant pool. Because constants in the class may be used during method execution, a reference must point to the runtime constant.

Method return address. After a method is executed, it must be returned where it was previously called. Therefore, a method return address must be saved in the stack frame.

Since the methods being executed by each thread may be different, each thread has its own java stack and does not interfere with each other.

3. Local method Stack

The role and principle of the local method stack and java stack are very similar. The difference is that the java stack serves to execute Java methods, while the local method stack serves to execute native methods. The JVM specification does not specify the specific implementation methods and data structures for local development. virtual machines can freely implement it. In the hotsopt virtual machine, the local method stack and java stack are combined into one.

4. Heap

The heap in Java is used to store objects and arrays (of course, array references are stored in the java stack ). Different from the C language, in Java, programmers basically don't have to worry about space release issues. Java's garbage collection mechanism will automatically handle the issue. Therefore, this part of space is also the main area of Java Garbage Collector management. In addition, the heap is shared by all threads, and there is only one heap in the JVM.

5. Method Area

The method area is also a very important area in JVM, which is the same as the heap and is shared by threads. In the method area, information about each class (including the class name, method information, and field information), static variables, constants, and compiled code are stored.

In addition to class field, method, interface, and other description information, the class file also has a constant pool to store the literal volume and symbol reference generated during compilation.

A very important part in the method area is the runtime constant pool, which is the runtime representation of every constant pool of classes or interfaces. After classes and interfaces are loaded to the JVM, the corresponding runtime constant pool is created. Of course, it is not the content in the constant pool of the class file that can enter the runtime constant pool. During the runtime, you can also add new constants to the runtime constant pool, such as the intern method of string.

In the JVM specification, garbage collection is not required in the method area. Many people are used to calling the Method Area "permanent generation" because the hotspot Virtual Machine implements the method area on permanent generation, so that the JVM garbage collector can manage this area as it manages the heap area, therefore, no need to design a garbage collection mechanism for this part. However, since jdk7, the hotspot virtual machine has removed the runtime pool from the permanent generation.


Iv. Object Instantiation Analysis

The most common example of memory allocation analysis is Object Instantiation:

Object OBJ = new object ();

The execution of this Code involves the three most important memory areas: java stack, Java heap, and method zone. Assuming that this statement appears in the method body and is used in Java that is not known to the JVM virtual machine in a timely manner, you should also know that obj will be used as a reference type) the data is stored in the local variable table of the java stack, but the referenced instantiated object will be saved in the java stack, but it may not be known, the Java heap must also contain the address information (such as the object type, parent class, implemented interface, and method) that can be used to locate the data of this object type ), these types of data are stored in the method area.

In addition, because the reference type only specifies a reference pointing to an object in the Java Virtual Machine specification, it does not define the method in which the reference should be located, and the specific location of the object accessed to the Java heap. Therefore, object access methods implemented by different virtual machines are different. There are two mainstream access Methods: Using the handle pool and directly using pointers.

Access through the handle pool is as follows:


Direct pointer access is as follows:


The two object access methods have their own advantages. The biggest advantage of using the handle access method is that the reference stores a stable handle address, moving Objects (moving objects during garbage collection is a common behavior) only changes the instance data pointer in the handle, and the reference itself does not need to be modified. The biggest advantage of direct pointer access is fast speed, which saves the time overhead of one pointer location. Currently, the hotspot Virtual Machine Used by Java by default adopts the second method for object access.


JVM memory partition and Object Instantiation Analysis

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.