Java memory block, java memory
Data Area During Running
During Java execution, the Java Virtual Machine divides the managed memory into several different data regions. These regions have their own purposes, as well as the creation and destruction time. Some regions exist with the startup of virtual machine processes, while some regions are created and destroyed by the startup and termination of threads.
The Java Virtual Machine includes the following runtime data regions:
Program counters
A program counter is a small area. Its function can be seen as a row number indicator of the bytecode executed by the current thread. In the virtual machine model, the bytecode indicator specifies the next instruction to be executed by changing the value of the program counter. Branches, loops, and other basic functions depend on program counters.
Because Java Virtual Machine multithreading is completed by switching in turn and allocating the processor execution time, one processor will only execute the commands in one thread at a time. To restore the correct execution position after the thread recovers, each thread needs an independent program counter to ensure that the threads do not affect each other. Therefore, the program counter is a "thread-private" memory.
If the virtual machine is executing a Java method, the counter specifies the address corresponding to the bytecode command. If a local method is being executed, the counter specifies the blank undefined. The program counter area is the only region in the Java virtual machine that does not define an OutOfMemory exception.
Java Virtual Machine Stack
Similar to program counters, they are also thread-proprietary and have the same lifecycle as threads.The Virtual Machine stack describes the Memory Model of Java method execution. When each method is executed, a stack frame is created to store information such as the local variable table, Operation stack, dynamic link, and method exit. The process of calling each method corresponds to the process from the inbound stack to the outbound stack of a stack frame in the Virtual Machine stack.
Generally, virtual machines run on stacks and stacks. The stack here refers to the Virtual Machine stack or the local variable table in the Virtual Machine stack.
The local variable table stores various basic data types, object references, and returnAddress types known by the compiler (pointing to the address of a bytecode instruction ). The memory space required by the local variable table is allocated in the compiler. when you enter a method, the size of the memory space required by this method in the frame is completely determined, the local variable table size is not changed during running. (The 64-bit long and double fields occupy the space of two local variables, and the other data types occupy one space)
Java Virtual Machine Stack may have two types of exceptions: 1. the stack depth of thread requests is greater than the stack depth allowed by the virtual machine, and StackOverflowError is thrown. 2. the VM stack space can be dynamically expanded. When dynamic expansion fails to apply for sufficient space, an OutOfMemory exception is thrown.
Local method Stack
The local method stack is similar to the Virtual Machine stack, except that the Java code (bytecode) is executed on the Java Virtual Machine stack, and the local method stack executes the local method service. StackOverflowError and OutOfMemory exceptions are also thrown in the local method stack.
Heap
Heap is the largest memory unit managed by Java virtual machines. A heap is an area shared by all threads. It is created when the VM is started. The sole purpose of heap is to store object instances,Almost all object instances are allocated here, but with the development of JIT compiler and the maturity of escape technology, stack allocation and scalar replacement technology make this situation a subtle change, the upper allocation is becoming less absolute.
Appendix: in the Java programming language and environment, the real-time compiler (JIT compiler, just-in-time compiler) is a Java bytecode (including the program for instructions to be interpreted) it is converted into a program that can directly send commands to the processor. After you write a Java program, the statements in the source language will be compiled into bytecode by the Java compiler, instead of the command code corresponding to a specific processor hardware platform (for example, intel's Pentium microprocessor or IBM's System/390 processor ). Bytecode is a platform-independent code that can be sent to any platform and run on that platform.
Java heap is the main area of the garbage collector management, so it is also called the "GC Heap ". Because the current Garbage Collector basically uses the generational collection algorithm, Java heap can also be subdivided into the new generation and the old generation. In detail, it can be divided into Eden space, From two vor spaces, To two vor spaces. From the perspective of memory allocation, the Java heap shared by threads can be divided into private allocation buffers for multiple threads. However, no matter how it is divided, it is irrelevant to the storage content. No matter which region, it is used to store the object instance. The purpose of the subdivision is to better recycle memory or allocate memory faster.
Java heap can be physically discontinuous. As long as the logic is continuous, mainstream virtual machines are implemented in an extensible manner. If the current center does not have memory to complete the creation of the object instance and the memory cannot be expanded, an OutOfMemory exception will be thrown.
Method Area
The method area is also a thread-shared area used to store data such as class information, constants, static variables, and Code Compiled by the real-time Compiler (JIT) that has been loaded by virtual machines.The Java Virtual Machine describes the method area as a logical partition of the Heap, but the method area has an alias Non-Heap (Non-Heap), which is used to distinguish it from the Java Heap area.
The Java virtual machine specification also imposes very loose restrictions on this region. In addition to physical discontiguous space, it also allows a fixed size and scalability, and does not implement garbage collection. Relatively speaking, the garbage collection behavior rarely appears in this region (so pay more attention to the definition of constants and static variables ). The memory collection in the method area will still appear, but the memory collection in this area is mainly for the collection of constant pools and the uninstallation of types.
In general, memory collection in the method area is hard to satisfy. When the method area cannot meet the memory allocation requirements, an OutOfMemoryError error is thrown.
Runtime constant pool
The runtime constant pool is part of the Method Area. In addition to the version, field, method, interface, and other information of the Class file, another information is that the constant pool is used to store various types of literal and symbolic references generated by the compiler. This information will be stored in the runtime frequent pool of the method area after the class is loaded.. Java virtual machines have strict rules on each part of the class (including the constant pool). Each byte must have a standard requirement on which data to store so that it can be recognized by virtual machines, load and execute. In general, in addition to saving the symbolic references described in the Class file, the translated direct references are also stored in the runtime pool.
Another important feature of the runtime constant pool relative to the Class file constant pool is that it is dynamic. Java virtual machine does not require constants to be generated only during compilation, that is to say, it is not the content preset into the constant pool of the Class file that can enter the runtime constant pool of the method area. During the runtime, new constants can also be placed in the constant pool.
The constant pool is a part of the method area, so it is limited by memory. When you cannot apply for sufficient memory, an OutOfMemoryError error will be thrown.
Object Access
Object Access is everywhere in the Java language. Even the simplest access involves the association between the three most important memory areas: Java stack, java heap, and method zone. The following code:
Object obj = new Object ();
Assuming that this code appears in the method body, the semantics of the "Object obj" part will be reflected in the local variable table on the Java stack, which exists as a reference type data. The semantics of the "new Object ();" part will be reflected in the Java heap to form a structured memory that stores all Instance Data values of the Object type, the memory length varies according to the specific type and the distribution of objects implemented by virtual machines. In addition, the JAVA heap must contain the address information that can be used to query the memory data of this object. These types of data are stored in the method area.
Because the reference type specifies the reference to an object in the Java virtual machine, it does not specify the method in which the reference is to be located and the specific location of the object in the Java heap, therefore, the object access methods implemented by virtual machines are different.There are two mainstream access methods: handle access and direct pointer.
1. if a handle is used for access, a memory block is allocated to the Java heap as the handle pool. The reference stores the object address, the handle contains the specific address information of the object instance data and type data.
2. If you access the object by using a direct pointer, you must consider how to place information about the access type data in the layout of the Java heap object. The object address is directly stored in the reference.
The two methods have their own advantages. The biggest advantage of local access is that the reference stores a stable handle address. when an object is moved, only the instance data pointer in the handle is changed, the reference itself does not need to be modified. The biggest advantage of pointer access is its high speed, which saves the overhead of one pointer location. Because objects are frequently accessed in Java, it is also a considerable cost after a small amount of such overhead is accumulated.
The specific access methods are specified by virtual machines. The virtual machine Sun HotSpot uses direct pointers. However, from the perspective of the entire scope of software development, handle access is common for various languages and frameworks.