1. Overview
The Java virtual machine will divide the memory it managed into several different data areas in the process of executing the Java program. These areas have their own use and time to create and destroy. The memory managed by the Java virtual machine will include the following runtime data areas, as shown in the figure below:
The following is explained in every area.
Second, the data area <br /> program counter <br /> program counter can be regarded as a line number indicator of the byte code executed by the current thread. In the concept model of the virtual machine, the work code interpreter work is to select the byte code instructions that need to be executed by changing the values of the program counter. Basic functions such as branches, circulation, jumping, abnormal processing, thread recovery and other basic functions must all must be Relying on this counter to complete.
In multi -threaded, in order to allow the thread to return to the correct execution position, each thread needs to have an independent programmeter. The various threads do not affect each other and store independently. Therefore, this memory is private.
When the thread is being executed is a Java method, this counter records the address of the virtual machine byte code instruction that is being executed; when the Native method is executed, the counter is empty.
This memory area is the only area that does not specify any OutofMemoryerror.
Java virtual machine stack
The Java virtual machine stack is also private, and its life cycle is the same as threads. The virtual machine stack describes the memory model executed by the Java method: each method will create a stack frame to store local variable tables, operate stacks, dynamic linked lists, method export information, etc. From the process of calling until the completion process is called, it corresponds to the process of entering the stack in a virtual machine stack to the stack.
In the local variable table, various basic data types (Boolean, Byte, Char, SHORT, FLOAT, Long, Double), object references and ReturnAddress types are stored in the compiler.
If you cannot apply for sufficient memory during extension, the OutofMemoryerror exception will be thrown.
The local method stack <br /> local method stack is similar to the virtual machine. The difference is that the virtual machine stack serves the Java method performed by the virtual machine, and the local method stack serves the Native method used by the virtual machine. Some virtual machines directly combine the local method stack and virtual machine stack into one.
Will throw Stackoverflowerror and OutofMemoryerror abnormalities.
Java pile
The Java pile is a memory area shared by all threads. It is created when the virtual machine starts. The only purpose of this memory area is to store object instances.
The Java pile is the main area of the garbage collector management. Since the collector is basically used for the division of recycling algorithms, the Java pile can also be subdivided as: the new generation and the old age. From the perspective of memory distribution, the thread -shared Java pile may divide multiple threads privately distributed buffer (TLAB).
The Java pile can be in a physical discontinuous memory space, as long as it is logically continuous. In terms of implementation, it can not only achieve fixed size or extension.
If there is no memory completion instance allocation in the heap, and the pile cannot be completed, the OutofMemoryerror will be thrown out.
Method area
The method area is a memory area shared by various threads. It is used to store data such as information, constant, static variables, and instant compilers compiled by the virtual machine.
Relatively speaking, garbage collection behaviors have rarely appeared in this area, but it is not permanently existing in the method area of data entering.
When the method area cannot meet the needs of memory distribution, the OutofMemoryerror will be thrown out.
Running constant pool:
It is a part of the method area, which is used to store various types and symbol references generated by the compilation period.
Direct memory
Direct memory is not part of the data area when the virtual machine is running. In the NIO class, an IO method based on channel and buffer can be introduced. The object is operated as a reference to this memory.
Direct memory allocation will not be limited by the size of the Java pile, but it will be limited by the size of the memory, and all may also throw an OutofMemoryRerror exception.
Third, the creation, layout and access process of objects
Object creation
Creating an object usually requires the NEW keyword. When a virtual opportunity is to a new instruction, first check whether the parameters of this instruction are positioned in the constant pool , Analysis and initialization. If the corresponding class loading process is executed.
After the class is loaded, the virtual machine will allocate memory for new students. The task of allocating space for the object is equivalent to dividing a piece of memory determined by the size from the Java pile. There are two ways to allocate: one is called a pointer collision. Suppose the memory in the Java pile is absolutely regular. The pointer moved to the side of the idle space. Another name is empty list: If the memory in the Java pile is not regular, the virtual machine needs to maintain one list, which records which memory block is available. When the distribution is allocated, find a large enough space to divide it to the object instance. And update the records on the list. Which distribution method is determined by whether the Java heap is regulated, and whether the Java heap is determined by whether the garbage collector is used with compression and organizational function. Another problem that needs to be considered is the thread security problem when the object creates. There are two solutions: one is to synchronize the action of allocating memory space; It is carried out, that is, each thread is pre -allocated a small piece of memory (TLAB) in the Java pile. Which thread is assigned to the TLAB of which thread is allocated on the TLAB. Only when the TLAB is used up and allocated a new TLAB, it needs to be locked simultaneously.
After the memory distribution is completed, the virtual machine needs to initialize the distributed memory space to zero value. This step ensures that the instance field of the object can be used directly in the Java code without the initial value.
Next, the virtual machine needs to set the necessary settings, such as which class of this object is the instance of the class, how to find the metadata information of the class, etc. This information is stored in the object head of the object.
After the above work is completed, a new object has been generated from the perspective of the virtual machine. However, from the perspective of the Java program, the init method is also required to initialize the object according to the programmer's wishes. Such a real available object is completely generated.
Memory layout of the object
In the HOTSPOT virtual machine, the layout of the objects in memory can be divided into three parts: object head, instance data, and alignment filling.
The object head includes two parts: the first part is used to store the data of the object itself, such as the hash code, the GC aging age, and the locks held by threads. The official is called "Mark Word". The second part is the type pointer, that is, the pointer of the object to its class metad data, and the virtual machine determines which class of this object is by this pointer.
The instance data is an effective information stored in the object, and it is also a variety of field content defined in the program code.
Alignment filling does not necessarily exist, just plays the role of the occupation character. , HotPot VM requires that the starting address of the object must be an integer multiple of 8 bytes. The header of the object is exactly a multiples of 8 bytes. Therefore, when the instance data part is not aligned, it is necessary to fill in alignment.
Object access positioning
The Java program operates the specific object on the stack through the Reference data on the stack. The main access methods are two types: handle and direct pointer:
Staff: Java heaps will draw a piece of memory as the handle pool. The handle address is stored in the reference, and the handle contains the specific address information of the object instance data and the type data. As shown in the figure:
Direct pointer: The layout of the Java pile object should consider how to place the relevant information of the type data of the type of access, and the object address is stored in the reference. As shown in the figure:
The two methods have their own advantages. The biggest advantage of using the handle is that the stable handle address is stored in the reference. When the object is moved, the address of the instance in the handle will only be changed. Quickly, it saves a time expenditure for pointer positioning.
The above is all the contents of this article. I hope it will be helpful to everyone's learning.