The partition of memory in the jvm virtual machine

1. Method Area

The Method Area, like the Java heap, is a memory area shared by each thread. It is used to store data such as class information, constants, static variables, and code compiled by the real-time compiler that have been loaded by the virtual machine. Although the Java virtual machine specification describes the method area as a logical part of the heap, it has an alias called Non-Heap (non-heap), which should be distinguished from the Java heap.

2. Program Counter Register

The program counter (Program Counter Register) is a small memory space, and its role can be seen as a line number indicator of the bytecode executed by the current thread. In the conceptual model of the virtual machine (only the conceptual model, various virtual machines may be implemented in some more efficient ways), the bytecode interpreter selects the next item to be executed by changing the value of this counter when working. Bytecode instructions, branch, loop, jump, exception handling, thread recovery and other basic functions all need to rely on this counter to complete.

The following focuses on solving the stack and heap in Java memory management.

3. Stacks

In Java, the stack in the JVM records the method calls of threads. Each thread has a stack. During the running process of a thread, if there is a new method call, the stack corresponding to the thread will add a storage unit, that is, a frame. In the frame, the parameters, local variables and return addresses of the method call are stored.

Java parameters and local variables can only be variables of primitive types (such as int), or references to objects. Therefore, in the stack, only primitive types of variables and object references are stored. The object pointed to by the reference is kept on the heap. (The reference may be Null, i.e. not pointing to any object).

When the called method finishes running, the frame corresponding to the method will be deleted, and the space occupied by parameters and local variables will also be released. The thread returns to the original method and continues execution. The program ends when all stacks are cleared.

The difference between the native method stack and the virtual machine stack:

The role played by the native method stack (Native Method Stacks) and the virtual machine stack is very similar, the difference is that the virtual machine stack serves for the virtual machine to execute Java methods (that is, bytecode), while the native method stack is for the virtual machine. Native method service used by the virtual machine. The virtual machine specification does not mandate the language, usage and data structure of the methods in the native method stack, so a specific virtual machine can freely implement it. Even some virtual machines (such as the Sun HotSpot virtual machine) directly combine the native method stack and the virtual machine stack into one. Like the virtual machine stack, the native method stack area also throws StackOverflowError and OutOfMemoryError exceptions.

4. Heap

The heap is an area in the JVM that can be freely allocated to objects. When we talk about garbage collection, we mainly reclaim the heap space.

Ordinary objects in Java live on the heap. Unlike the stack, the heap is not emptied with the end of a method call. Therefore, objects created in a method can continue to exist in the heap after the method call ends. One problem with this is that if we keep creating new objects, the memory space will eventually run out.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324941838&siteId=291194637