The data area of the Java Runtime includes: (where the first two are thread-shared)
1. Method area stores data such as class information, constants, static variables, instant compiler compiled code, etc. loaded by the virtual machine
2. Heap holds object instances where almost all object instances allocate memory
3. The virtual machine stack (VM stack) describes the memory model that is executed by the Java method: Each method executes while creating a Stack Frame (the underlying data structure of the method runtime) to store information such as local variable tables, operand stacks, dynamic joins, method exits, and so on
4. The local method stack (Native) is similar to the virtual machine stack, but the Native method service used by the virtual machine. (Some virtual machines, such as sun hotspot virtual machines, directly merge the local method stack with the virtual machine stack)
5. Program Counter Register can be regarded as the identifier of the line number of the byte code executed by the current thread
The total JVM memory is divided into 4 parts:
1. Stack segment
2. Heap Segment
3. Code Segment
4. Data segment)
When we declare a local variable in the program, this variable is stored in the stack segment (stack);
This object is placed in the heap segment when new object is set;
Static variables or string constants exist in the data segment.
The methods in the class are present in code segment.
The memory of a Java Virtual machine can be divided into three regions:
-
Stack stack
-
-
-
- (1), the stack describes the method execution of the memory model, each method is called to create a stack frame (store local variables, operands, method entry, etc.);
-
-
- (2), the JVM creates a stack for each thread that holds information about the execution method of the thread (actual parameters, local variables, etc.);
-
-
- (3), the stack belongs to the thread private, can not realize the sharing between threads;
-
-
- (4), the storage characteristics of the stack is advanced, after-first out ;
-
-
- (5), the stack is automatically allocated by the system, the speed is fast, the stack is a continuous memory space.
-
Heaps heap
-
-
-
- (1), the heap is used to store the created objects and arrays (arrays are objects);
-
-
- (2), the JVM has only one heap, is shared by all threads;
-
-
- (3), Heap is a discontinuous memory space, the allocation of flexible, slow speed.
-
Method Area (in heap)
-
-
-
- (1), the JVM has only one method area, is shared by all threads;
-
-
- (2), the method area is actually a heap, only for storing classes, constants related information;
-
-
- (3), the use of the storage program is always the same or unique content. (class Information "Class object", static variable, string constant, etc.)
Java Runtime Data area
Runtime data area includes: Virtual machine stack, heap area, method area, local method stack, program counter
-
-
Virtual Machine Stack Area: that is, we often say that the stack area, thread-private, storage base type, object reference and ReturnAddress, during compilation to complete the allocation.
-
-
Heap area, Java heap, also known as GC heap, all threads shared, storage instances and arrays of objects, Java heap is the main area of garbage collector management.
-
-
Method Area: All threads are shared, storing data such as class information, constants, static variables, and instant compiler compiled code that have been loaded by the virtual machine. The target of memory reclamation in this area is mainly the collection of objects for the constant pool and the unloading of the types.
-
-
Program counter: Thread private, each thread has its own independent program counter, used to indicate the address of the next instruction.
Java memory model
Accumulation of knowledge points
1, String str1 = "abc", "ABC" is allocated in the memory character constant area;
2, the program counter is a relatively small area of memory, used to indicate that the current thread executes the bytecode executed to the first line, is thread-isolated;
3. Virtual machine stack describes the memory model of Java method execution, which is used to store local variables, operand stacks, dynamic links, method exits and other information, and is thread-isolated;
4. In principle, all objects allocate memory on the heap, which is shared between threads.
Java Memory and data area