The Java virtual opportunity opens up a memory area that is used by the Java program alone, and this area of memory can be divided into the following sections, as shown in Figure 1:
Figure 1
The following table lists some of the features and differences of these areas, such as Table 1:
Table 1
function Explanation:
Program Calculator: The address byte code used to store threads executing instructions for running Java programs, that is, the returnaddress type. A thread a program counter;
If the current thread executes a Java method, the instruction address is stored, and if the current thread executes the native method, the storage is undefined;
Java Virtual machine stack: The stack frame used to store methods executed by each thread. When each method is executed, a stack frame is created at the same time, into the stack to the Java Virtual machine stack;
Method completes, the stack frame is out of the stack. The stack frame will hold a series of data related to the method, including local variable table, operand stack, dynamic chain
Access, methods such as export information;
The size of the stack can be controlled by the parameter-xss;
Stack frame: Method of the call and completion, with the stack frame into the stack and out of the stack; the size of the stack frame, mainly the size of the local variable table and the depth of the operand stack, the compiler will decide
As shown in Figure 2:
Figure 2 Stack frame diagram
Local variables table: for storing 10 types of data in a Java Virtual machine (byte,short,int,long,float,double,char,boolean,reference,
ReturnAddress); 8 basic data types data values are stored directly in the stack, while the other two simply store the references; internal to slot
For the unit, in addition to the long,double64 bit length needs to store 2 slot, the rest only need 1 slot storage; The large of the local variable table
Small compile time fixed;
Slot in a local variable table can be reused, and if a local variable life cycle is over, the slot can be accounted for by the next defined local variable in the stack frame
With
The variables in the local variables table do not have the preparation phase when the class loads, there is no initial value stage, so it must be declared when the value is assigned, otherwise the compilation does not pass;
The first slot of a local variable table holds a reference to the object that the method belongs to, which can be invoked through the This keyword;
Operand stack: Used for the method of internal data operation of the storage space, the first operation of the data often from the top of the stack of two elements, the stack after the operation of the results into the stack;
In the phase of compile and class loading, the data type of the instruction must be strictly validated to match the data of the operand stack.
Note: When the method call, such as a call B, will normally save a current PC counter position in B's stack frame, when B back out of the stack, will let the PC counter refers to
This address is saved to the B stack frame;
Local method Stack: Like Java Virtual machine stack function, it only serves and native method;
Java heap: The area where the object instance is stored, the main function of the GC collector; the size of the heap can be controlled by the parameter-xmx,-xms;
Method area: Used to store some information about the class, some classes of variables (non-method instance variables, local variables), just-in-time compilation code, and so on, which has a static region, specifically used to store static variable
Quantities and static blocks of code, as well as method information, including the method table, and the compiled byte script in the method body; It can choose whether to implement garbage collection; General garbage collection is not here
Regions, which are therefore also known as "permanent generations";
Run a constant-amount pool (in the method area): Store the various literal and symbolic references generated by the compilation period;
Direct memory: Is outside the heap of memory, is directly called by Java, called Direct memory;
about memory overflow and stack overflow:
Stackoverflowerror: When the virtual machine is initialized, the stack is allocated a fixed size, when the thread requests the stack depth is larger than the stack size, it will throw the different
Often
OutOfMemoryError: Stack, the heap can be dynamically expanded, that is, when the stack depth is not enough time, you can apply for extended space size; no more empty applications
, and can not meet the current stack or heap space resource needs, it throws the exception;
For Example: for a statement such as Object = new Object (), such objects are created, in memory there are two types of memory call models, one for the handle type, one for the direct reference;
For direct references, Figure 3:
Figure 3
For a handle reference, Figure 4:
Figure 4 in the heap, the basic type of data referenced in the object is basically stored directly in the heap, and for reference types such as String, the reference is stored in the heap and the value is in the constant pool.