JVM Memory Structure---Learning summary of in-depth understanding of Java virtual machines

Source: Internet
Author: User

This article turns from: http://www.cnblogs.com/jilodream/, Wang Joei _ Grace Liberation

The Java Virtual machine divides the memory it manages into several different data regions during the execution of a Java program. These areas are used in different ways, and the data is created and destroyed independently, based on the respective rules of execution.

The partition of the virtual machine memory:

The areas where threads are independent of each other are:

Virtual machine stack, local method stack, program counter

Areas where threads can share data:

Method area, Heap

The roles of each region are as follows:

Program counter Programs Counter Register:

It is well known that when a virtual machine processes multiple threads, it is the opportunity to get the execution of the CPU by switching threads in turn. During the execution of a program by a virtual machine, when a thread executes to a location, the virtual machine gives the CPU an execution opportunity to the other thread, where the execution location of the original thread needs to be recorded, and the new thread that gets the execution opportunity needs to provide the location of the last execution. This ensures that multiple threads in the program can continue to execute in parallel.

The function of the program counter is to record the next execution (bytecode) line number of each thread (exactly the address of the instruction) to ensure that it will execute correctly the next time it executes.

According to the function of the program counter, we can know:

1, each thread in this area should have a service only for their own program counters, which are isolated storage, non-impact existence.

2. We can also know that the program counter only records the line number of the bytecode, so when the thread executes the local method (Native), the value of the counter is empty.

3, the program counter consumes very little memory space, so this area will not throw OutOfMemoryError error.

Virtual machine stack VM stack:

Threads want to run normally, it is far more than just a program counter to record line numbers. Threads also need to have their own running space, in this space, the virtual machine can save the execution order of the method, the internal local variables of the method, the method in the operation, the required memory space and so on.

In the data structure, the characteristics of the stack most satisfy the method of entering the returned structure. The main role of this area is the data that the thread needs to log when executing the Java method. So we call this area a virtual machine stack. But keep in mind that this is not the same stack that we usually refer to at work, which I'll introduce behind.

The structure of the virtual machine stack is as follows:

And for each stack frame inside the division is this:

The function of each part is as follows:

Local Variables Table: Each method can define a local variable that belongs only to itself, and when the method runs, the life cycle of the local variable is ended. So each method should have a chunk of its own memory area to hold local variables defined inside the method. This area is a local variable table, and the stack we refer to in our usual work is actually referring to the local variable table in the stack frame of the virtual machine stack.

Operand stacks: The data can be computed internally for each method, and the computational data is bound to have a memory area for the virtual machine to perform numerical calculations. Therefore, in the stack frame, it is necessary to have an area specifically for the current method to calculate the use of data, it is the operand stack.

After each complete calculation, the data in the stack has been stacked, so the space of the operand stack can be reused within a method. Therefore, when allocating memory size, the virtual machine allocates only the current method, the maximum memory space required for a single complete computation to the current stack frame, to reduce memory consumption.

At the same time, in order to increase the operation efficiency and reduce the duplication of data, in the implementation of most virtual machines, the local variable table of the current method and the memory formation of the upper method's operand stack are overlapped, thus reducing the performance consumption caused by the continuous copying of the parameters.

Dynamic Connection:

There are two forms of virtual machine execution methods that are used to determine the method that the execution instructions correspond to.

The first is when the class loads, you can directly determine the method to execute, such as static methods, private methods, final methods, and so on. This form is called static parsing.

The second is to judge the actual method that is currently being executed, based on the actual reference to the object, at runtime. This form is called dynamic connectivity.

In a bytecode file, there is a constant pool that holds a large number of symbolic references in this constant pool, which is an indirect reference to each method. In the bytecode directive, the symbol reference is used. But in the run-time phase, it is definitely necessary to call the actual address to execute the method in memory. This requires converting an indirect reference into a direct reference. The "Dynamic Connection" here is to ensure that in the runtime phase, the method can correctly find the method to invoke, each stack frame itself in the run constant pool in the corresponding location of the real address record.

It is important to note that the dynamic joins in the stack frame and the lookup symbol references to the dynamic connections in the real reference are two concepts. The former represents a region, and the latter represents a way of finding it.

Return Address:

There are two ways to exit the current method, the first of which is to exit the current method normally when the return instruction is encountered. Another form is the exception that is thrown when it encounters no catch. Regardless of the return form, the top of the stack frame should be the upper-level method of the current exit method after the method exits. The execution state of the upper method also needs to be re-adjusted according to the current return result. Therefore, each stack frame can use the "return address" area to help the upper-level method to restore the state.

Additional information: For the virtual machine specification, information that has the specified location can be placed in this area by the individual virtual machines themselves.

Local method Stack Native stack

In a virtual machine, not only does the Java method run, it also runs local methods, which are common methods of native keyword adornments. In a virtual machine stack, a stack of methods that specifically runs the Java language (more precisely the bytecode) is created for each thread, but for local methods, a separate chunk of memory is used to hold the thread's invocation state, which is the local method stack. His role is basically similar to that of a virtual machine stack, the difference being a Java method service, and a native light-emitting method. In the virtual machine specification, there is no mandatory requirement for the structure of the local method stack, the method's language, the way, each virtual machine can implement it freely.

Java Stack Java heap

What we are talking about is creating an instance in the heap, referring to this heap. This is the largest piece of memory managed by a virtual machine. In a virtual machine, almost all of the instances and the allocated memory space of the array are placed in the heap.

Because the Java heap is the primary storage location for an object instance, the virtual machine's garbage collection mechanism is the primary work area.

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

Depending on the nature of the 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 be used in the physical non-contiguous memory, as long as the logic to maintain a continuous.

Methods Area method

The main function of the method area is to preserve data such as class information, constants, static variables, and immediately compiled code. The data in this area will still be covered by GC generation recycling. The permanent generation we are talking about is the area.

Although this area is also known as a permanent generation, it may still be recycled when data enters the region. The recovery target for this area is primarily the collection of constant pools, as well as the type of uninstallation.

Running a constant pool runtime Constant pool

This area belongs to a sub-region 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 that is a constant pool. The data in the constant pool will be present in the run-time pool after the class is loaded.

A constant pool in a class file mainly includes a variety of literal and symbolic references. The symbol reference is involved when explaining the stack frame.

Literals can be understood as constants in the Java language, such as strings, final modified variables, and so on.

Symbolic references refer to the following three types of fixed information:

Fully qualified names for classes and interfaces

Name and descriptor of the field

The name and descriptor of the method

After the Java language is translated into class files, there is no information about the final layout of methods and fields in memory. So when a virtual machine uses these variables or methods, it needs to find the corresponding symbolic reference from the constant pool, and then find its corresponding memory real location in the dynamic connection area of the method's stack frame.

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

1, OutOfMemoryError

2, Stackoverflowerror

OutOfMemoryError refers to a region, because of the increasing number of data, the region can no longer from the total amount of physical memory to request to more space, or the area requested space has reached the virtual machine running parameters to the maximum value set for the region, then this error will be thrown.

Stackoverflowerror refers to the memory stack structure in the continuous stack, resulting in the depth of the stack than the virtual machine allows the stack depth, the error thrown.

JVM Memory Structure---in-depth understanding of Java Virtual Machine learning summary

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.