JVM memory structure --- deep understanding of Java Virtual Machine (jvm)

Source: Internet
Author: User

JVM memory structure --- deep understanding of Java Virtual Machine (jvm)

When executing a Java program, the Java Virtual Machine divides the memory it manages into several different data regions. These regions have different purposes and create and destroy data independently based on their respective execution rules.

Virtual Machine memory Division ,:

The regions with independent threads are as follows:

Virtual Machine stack, local method stack, program counter

Regions where threads can share data:

Method Area and heap

Each region has the following functions:

Program Counter Register:

As we all know, when a virtual machine processes multiple threads, it obtains the cpu execution opportunity through switching threads in turn. In the process of executing a program on a virtual machine, when the thread is executed to a certain position, the virtual machine transfers the cpu execution opportunity to other threads. At this time, the execution of the original thread (anti-theft connection: the beginning of this article from the http://www.cnblogs.com/jilodream/) location needs to be recorded, and the new thread to get the execution opportunity, but also need to provide the location of the last execution, this ensures that multiple threads in the program can be continuously executed in parallel.

The purpose of the program counter is to record the (bytecode) line number (accurately the instruction address) executed by each thread next time to ensure that it can be correctly executed during the next execution.

Based on the function of the program counter, we can know:

1. Each thread should have a program counter that only provides services for itself in this region. They are stored independently and do not affect each other.

2. We can also know that the program counter only records the row number of the bytecode. Therefore, when the thread executes the Native method, the counter value is null.

3. The memory used by the program counter is very small, so this region will not throw an OutOfMemoryError.

VM Stack:

If the thread wants to run normally, it is far more than recording the row number by program counters. The thread also needs to have its own runtime space. In this space, the virtual machine can save the execution sequence of the method, the local variables in the method, and the memory space required for the method during operation.

In the data structure, the stack features the most satisfying method to enter the returned structure. And this area of the main (anti-theft connection: This paper first from the http://www.cnblogs.com/jilodream/) to be used is the thread in the execution of the java method needs to record the data. Therefore, we call this region a virtual machine stack. But remember that this is not the same as the stack we usually refer to at work. I will introduce it later.

The structure of the VM stack is as follows:

The internal division of each stack frame is as follows:

Each part has the following functions:

(1) local variable table: each method can define a local variable that only belongs to itself. After the method is run, the lifecycle of the local variable ends. Therefore, each method should have a block in its own memory region to save the local variables defined in the method. This area is a local variable table. The stack we usually refer to actually refers to the local variable table in the stack frame of the Virtual Machine stack.

(2) operand Stack: data can be calculated within each method, and computing data must have a memory area for virtual machines to perform numerical computation. Therefore, in the stack frame, a region is required for the current method to calculate data, which is the operand stack.

After each complete calculation, the data in the stack has been released, so the space of the operand stack can be used repeatedly within a method. Therefore, when the VM allocates memory for an hour, only the current method is allocated. The maximum memory space required for a single full calculation is allocated to the current stack frame to reduce memory consumption.

At the same time, in order to increase the running efficiency and reduce the continuous data replication, in most Virtual Machine implementations, the local variable table of the current method and the memory of the upper method's operand Stack are partially overlapped, this reduces the performance consumption caused by continuous parameter replication. (Anti-theft connection: This article first from the http://www.cnblogs.com/jilodream)

(3) dynamic connection:

The virtual machine can be used to determine the method for executing commands in two ways,

The first method is to determine the method to be executed during class loading, such as static method, private method, final method, etc. This form is called static parsing.

The second is to judge the method to be executed based on the actual reference of the object during actual running. This form is called dynamic connection.

There is a constant pool in the bytecode file, which stores a large number of symbolic references. This symbolic reference is indirect reference of every method. In the bytecode command, this symbol is used for reference. However, in the runtime stage, you must call the actual address of the method to be executed in the memory. This requires converting indirect references into direct references. Here, the "dynamic connection" is used to ensure that the method to be called can be found correctly in the runtime stage, each stack frame records its actual address location in the runtime pool.

It should be noted that dynamic connections and search symbol references in stack frames are two concepts: Dynamic connections in real references. The former represents a region, and the latter represents a search method.

(4) return address:

There are two ways to exit the current method. The first is to exit the current method normally when a command is returned. Another way is to encounter exceptions that are not captured and thrown. Regardless of the return form, after the method exits, the top of the stack frame should be the upper method of the current exit method. At the same time, the execution status of upper-layer methods also needs to be adjusted based on the current returned results. Therefore, each stack frame can use the "return address" area to restore the upper layer.

(5) Additional information: If the VM specification is not stated, the information with the specified storage location can be determined by the virtual machines themselves and placed in this region.

Local method Stack Native Stack

In a virtual machine, not only the java method is run, but also the local method is run, that is, the common Native keyword modifier method. In the virtual machine stack, for each thread independent open up a dedicated java language (more accurate should be bytecode) method (anti-theft connection: This paper first from the http://www.cnblogs.com/jilodream/) stack, however, for local methods, another memory area is used to save the call status of the thread. This area is the local method stack. Its role is basically similar to that of the Virtual Machine stack. The difference is that it serves the java method and the Native method. In virtual machine specifications, there are no mandatory rules on the structure, method language, and method of the local method stack. Each virtual machine can freely implement it.

Java Heap

What we usually call creating an instance in the heap refers to the heap. This is the largest piece of memory managed by virtual machines. In virtual machines, almost all instances and the memory space allocated by the array will be placed in this heap.

Because java heap is the main storage location of object instances, the main work area of the garbage collection mechanism of virtual machines.

Based on the Java memory recycle mechanism, we can divide the heap size and content into the following forms:

Based on the features of java heap, we can also know that this area is a thread-shared area. At the same time, we can also see that this area can use non-contiguous physical memory, as long as it is logically continuous.

Method Area

The main function of the method area is to save data such as class information, constants, static variables, and compiled code. Data in this region will still be involved in GC replacement. What we usually call permanent generation refers to this region.

Although this region is also called permanent generation, data may still be recycled when it enters this region. The main purpose of this region is to recycle the constant pool and unload the type.

Runtime Constant Pool

This area is a subarea in the method area.

In the Class file, in addition to the Class version, fields, methods, interfaces, and so on, there is an information area (anti-theft connection: This paper first from the http://www.cnblogs.com/jilodream/) domain is a constant pool. The data in the constant pool will exist in the runtime constant pool after the class is loaded.

The constant pool in class files mainly includes various literal and symbolic references. Symbol references are involved in stack frames.

A literal can be understood as a constant in java, such as a string or a final variable.

Symbol Reference refers to the following three types of fixed information:

(1) fully qualified names of classes and interfaces

(2) field name and Descriptor

(3) method name and Descriptor

After the java language is compiled into a Class file, there is no information about the final layout of methods and fields in the memory. So when the virtual machine uses these variables or methods, you need to first find the symbol reference corresponding to the data from the constant pool, find the real memory location in the dynamic connection area of the method stack frame.

In our daily work, we often encounter two kinds of memory overflow errors:

1. OutOfMemoryError

2. StackOverflowError

OutOfMemoryError refers to an area in which, due to the increasing data, the area cannot be applied for a larger space from the total physical memory, or the space applied for by the region has reached the maximum value set for the region by the VM running parameter, this error will be thrown.

StackOverflowError refers to the error thrown when the stack structure in the memory is continuously written into the stack, and the stack depth exceeds the stack depth allowed by the 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.