Java memory area · stdwal

Java virtual machine memory division during the execution of the Java program in which it will manage for a number of different data areas. These regions have their own purposes, and the time of creation and destruction, some areas along with the virtual machine process exists, some regions are dependent on start and end users to create and destroy threads while. Java virtual machine memory management will include the following run-time data area:

1. Program Counter

Program counter (Program Counter Register) is a small memory space, its role can be regarded as the row number designator bytecode executed by the current thread. When the bytecode interpreter is working by changing the value of the counter byte code to select a next instruction to be executed, a branch, looping, branching, exception handling, and other basic functions thread recovery rely on this counter complete.

Since the multi-threaded Java Virtual Machine is in turn switched mode execution time and processor allocation achieved by a thread, at any time a determined, a processor will perform (for multi-core processors is a kernel) a thread instruction. Therefore, in order to restore the thread switch to the correct execution position, each thread requires a separate program counter, between the threads independently of each other, independent store, we call this type of memory area is "thread-private "memory.

If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instructions being executed, if the method is being executed Native, this counter value is null (Undefine). This memory area is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.

2.Java virtual machine stack

Like the program counter, Java Virtual Machine stack (Java Virtual Machine Stacks) is also a thread private, its life cycle and the same thread. It is a virtual machine stack Java memory model described method of execution: when each method is executed will create a stack frame (Stack Frame) to store information local variable table, the operand stack, dynamic linking, method exports. Each method is called until the completion of the execution procedure, a stack frame corresponds to a virtual machine from the stack to the stack of a process stack.

The local variable table stored compile various known basic data types, object reference type and returnAddress type (address pointing to a byte code instruction).

Wherein the long and double-length 64-bit data will account for two local variable space (Slot), remaining only one type of data. Desired local variable table created during compilation memory space allocated, when entering a method, this method requires much space for local variables allocated in the frame is completely determined during operation of the method does not change the size of the local variable table.

Java Virtual Machine specification for this region provides for two anomalies: If the stack is greater than the depth of the thread requested virtual machine allowable depth, StackOverFlow will throw an exception; if the virtual machine can dynamically expand the stack, when the application can not expand enough It will throw OutOfMemoryError exception memory.

3. The native method stacks

Native method stacks (Native Mehtod Stacks) and the role played by the virtual machine stack is very similar, but the difference is the virtual machine execution stack for the Java Virtual Machine method (ie bytecode) service, and is for the native method stacks virtual machine to use a method of Native service.

4.Java heap

For most applications, Java heap (Java Heap) is the largest piece of memory in the Java virtual machine management. Java heap is shared by all threads in a memory area is created when the virtual machine starts. The sole purpose of this memory area is stored object instance, almost all object instances are here to allocate memory.

Java heap is the main area of ​​the garbage collector, and therefore often also called GC heap (Garbage Collected Heap). From the perspective of memory recovery, due to current collectors are basically used generational collection algorithm, so the Java heap can also be broken down into: the new generation and the old era. From the perspective of memory allocation point of view, shared by the threads of the Java heap may be divided into multiple threads private allocate a buffer (Thread Local Allocation Buffer, TLAB). The purpose is further divided to better recovery of memory, or memory allocation faster.

Java stack may be in a discontinuous physical memory space, as long as can be logically contiguous. When implemented, may be implemented as a fixed size, it may be scalable. If there is no complete examples in the heap memory allocation, and the stack can no longer expand, it will throw an OutOfMemoryError.

The method area

District method (Method Area) and the Java heap, as each thread is a shared memory area, which is used to store class information has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other data.

Java Virtual Machine specification limits of this area is very relaxed, and in addition to the same Java heap memory and does not require continuous can choose a fixed size or can be expanded, you can also choose not to implement garbage collection. In contrast, garbage collection behavior in this area is relatively small appearance, but not the data into the area on a permanent method existed. Garbage collection target this area is mainly for recycling and unloading of constant pool types.

When the method of memory allocation area can not meet demand, it will throw an OutOfMemoryError.

6. runtime constant pool

Runtime constant pool (Runtime Constant Pool) is part of the zone method. Class file versions in addition to the classes, fields, methods, and interface description information, the information is also a constant pool (Constant Pool Table), and for storing various literal compile generated reference symbols, this part the method to store in the zone is run after loading the class constant pool.

Java Virtual Machine Specification requirements do not have any details on the runtime constant pool, different providers implement virtual machines according to their own needs to achieve this memory area. However, in general, in addition to saving Class symbol described in the document cited, it will also translate out of the direct reference is also stored in the runtime constant pool.

Another important feature of the runtime constant pool file relative to Class constant pool is equipped dynamic, Java language does not require constant must only be produced in the compiler, which is not preset the contents of Class file constant pool to enter the method run constant pool area during the operation may be the new constants into the pool, such as the intern () method of the String class.

When the constant pool will no longer apply to the memory OutOfMemoryError is thrown.

Reference: "in-depth understanding of the Java Virtual Machine"

Original: Big Box  Java memory area · stdwal


Guess you like

Origin www.cnblogs.com/petewell/p/11612238.html