The memory structure of the Java Virtual machine

Source: Internet
Author: User

We all know that the memory of a virtual machine divides multiple areas, not a piece of pie. So why divide into multiple areas, directly engaged in an area, all the memory of the place to the area to throw the line, not happy. Yes, if you do not divide the region, throw the time really happy, when available to find how to do it, this introduced the first problem, classification management, similar to the wardrobe, system disk, etc., in order to facilitate the search, we will be partitioned classification. In addition, if you do not partition, the memory is exhausted how to do? This introduces the second reason for memory partitioning, which is to facilitate the recovery of memory. If not, the recovery of memory needs all memory scanning, it is slow to die, memory according to different use functions into different areas, then memory recycling can be based on the specific recovery of each region, such as stack in the memory of the stack frame, as the method of execution stack frame, the method executes after the stack, As for the recovery of heap-like memory needs to use the classic recycling algorithm for recycling, so it seems that classification is so troublesome, in fact, it is very good.

Referring to the memory structure of a virtual machine, the first thing you might think about is the stack. Objects are allocated on the heap, and the stack is used to assign the reference to the object and some basic data type-related values. However, the memory structure of the virtual machine is far more complex than this. There are program counters, local method stacks, and method areas in addition to the stacks that we know (not yet fully aware of). The stack memory that we usually refer to is the local variable table in the Stack memory. The following is an official memory structure for the virtual machine

You can see that there are 5 large areas of memory that can be divided into two parts depending on whether the thread is shared, part of the thread exclusive zone, including the Java stack, the local method stack, and the program counter. There are also parts that are shared by threads, including method areas and heaps. What is thread sharing and thread exclusivity, very well understood, we know that every Java is going to have multiple threads running concurrently, so this area of the thread share is used by all threads, regardless of the number of threads, this space is always on this one. The exclusive area of the thread is that each thread has such a memory space, and each thread's space is unique, and how many threads have such a space. The size of the area does not represent the size of the actual memory area, and the size of the memory area can be dynamically adjusted during the actual operation. The main functions of each area are explained below.

Program counter, we write code in the process, the development tool will generally give us the line number to easily view and read the code. Then in the process of running the program also has a similar line number to facilitate the execution of the virtual machine, is the program counter, in C, we know there will be a goto statement, in fact, jump to the specified line, this line number is the program counter. Stored is the next command executed by the program. This part of the area is a thread-exclusive area, we know that the thread is a sequential execution flow, each thread has its own order of execution, if all the threads share a program counter, then the program execution will definitely go wrong. To guarantee the order of execution of each thread, the program counter is unique to a single thread. Program counter this memory area is the only one that does not specify a memory overflow in the JVM specification.

Java Virtual machine stack, Java Virtual machine stack is the dynamic area of the program run, each method is accompanied by the stack frame in the stack and out of the stack. A stack frame is also called a process activity record, which is a data structure used by the compiler to implement a procedure/function call. The stack frame includes a local variable table, an operand stack, a method return address, and additional additional information, in the compilation process, the size of the local variable table has been determined, the operation of the stack depth has been determined, so the stack frame in the process of operation need to allocate how much memory is fixed, not affected by the runtime. For non-escaping objects will also allocate memory on the stack, the size of the object is actually determined at run time, so even if the memory allocation on the stack, it will not cause the stack frame to change size.

In one thread, the chain of calls may be long and many methods are in the execution state at the same time. For the execution engine, in the active thread, only the stack frame at the top of the stack is the most efficient, called the current stack frame, and the method associated with the stack frame is called the current method. The bytecode instruction run by the execution engine operates only on the current stack frame.

Local Variables table: the memory of the stack is usually referred to as the local variable table in the Stack memory. This is mainly used to store variables. Stores its value directly for the base data type, and stores its address for the reference data type. The minimum storage unit for a local variable table is the slot, where each slot can hold a Boolean, Byte, char, short, int, float, reference, or returnaddress type of data.

Now that the data type is mentioned, by the way, a slot can hold a 32-bit data type with a Boolean, Byte, char, short, int, float, and a data type of 32 bits or less in Java. There are eight types of reference and ReturnAddress. The previous six types do not need to be explained, everyone knows, and the reference is the object reference. The virtual machine specification does not describe its length or the structure of the reference, but in general, the virtual machine implementation should at least be able to find, directly or indirectly from this reference, the object type data in the starting address index and the method area of the objects in the Java heap. Instead, ReturnAddress is serving the bytecode directive JSR, Jsr_w, and RET, which points to the address of a bytecode directive.

For a 64-bit data type, the virtual opportunity allocates two contiguous slot spaces to the high-level in the previous way. The 64-bit data types explicitly specified in the Java language are only long and double two (the reference type may be 32 bits or 64 bits). It is worth mentioning that this is similar to splitting a long and double data types into two read and write 32 reads and writes. However, because the local variable table is built on the thread's stack, the data is private to the threads, regardless of whether the read-write two consecutive slots are atomic operations, it does not cause data security issues.

The operand stack is a post-in, first-out, or LIFO stack. As with the local variable table, the maximum depth of the operand stack is also written to the bytecode file at compile time, and I will describe the bytecode file in detail later. Each element of the operand stack can be any Java data type, including a long and a double. The 32-bit data type occupies a stack capacity of 1, and the 64-bit data type occupies a stack capacity of 2. At any point in the execution of the method, the depth of the operand stack does not exceed the maximum value set in the Max_stacks data item.

When a method is just beginning to execute, this method's operand stack is empty, during the execution of the method, there will be various bytecode instructions to the operand stack to write and extract content, that is, into the stack operation. For example, the arithmetic operation is done by the operand stack, or when the other method is called by the operand stack to pass the parameter.

For example, the byte-code instruction for integer addition Iadd requires that the two elements closest to the top of the stack in the operand stack have been stored in two int, and when executed, the two int values are added together, and the result of the addition is added to the stack.

The data type of the elements in the operand stack must match the sequence of the bytecode instruction strictly, and the compiler should strictly guarantee this when compiling the program code, and verify this again in the data flow analysis of the class check phase. In the case of the iadd instruction above, this instruction is used for integer number addition, when it executes, the data type of the two elements closest to the top of the stack must be int, and a long and a float can not be added using the Iadd command.

The role of the local method stack with the virtual machine stack is very similar, but the difference is that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, while the local method stack serves the native method used by the virtual machine. The language, usage, and data structure of the methods used in the local method stack in the virtual machine specification are not mandatory, so the virtual machine can implement it freely. Even some virtual machines, such as sun hotspot virtual machines, combine the local method stack and the virtual machine stack directly. As with virtual machine stacks, the local method stack area throws Stackoverflowerror and OutOfMemoryError exceptions.

The method area is often called the permanent generation, but these two are not a concept. First of all, the concept of permanent generation exists only in the hotspot virtual machine, unfortunately, in Jdk8, the hotspot removed the permanent generation of this statement, using the native memory, that is, metaspace space. So what does the method area do? We can understand that we want to run the Java code, first we need to compile, and then to run. In the process of running, we know that we need to load the bytecode file first. This means that you want to load the bytecode file into memory. Okay, here's the question, where the bytecode file is placed in memory, in the method area. Of course, in addition to the compiled bytecode, the method area will also hold constants, static variables and timely compiler compiled code and other data.

Heap, in general, heap memory is the largest chunk of memory in a Java virtual machine, and, like the method area, is the area shared by all threads. The only purpose for this zone is to hold an instance of the object (the object instance is not necessarily all created in the heap). Heap memory is the main area of the garbage collector, in general, depending on the garbage collector used, the heap will be divided into some areas, such as the new generation and the old age. The Cenozoic can also be divided into eden,survivor and other regions. In addition, for performance and security purposes, the threads are also divided into separate areas in the heap, called thread allocation buffers. A more granular division is designed to allow the garbage collector to work more efficiently and improve the efficiency of garbage collection.

If you want to learn more about virtual machines, you are welcome to watch the recorded < in- depth understanding of Java Virtual Machines > This set of video tutorials.

The memory structure of the Java Virtual machine

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.