Stack, heap, method area in java

Stack (stack)
The Java stack is different from the heap. Each thread has a stack. The area of ​​the stack is very small, only about 1M, but the storage speed is very fast, so we store fast-executing tasks in the stack.

Features: automatic allocation, continuous space, first-in-last-out principle.

1. Basic data types (a total of eight types: char, byte, short, int, long, float, double, boolean) are directly allocated in the stack space.

2. The reference data type is directly allocated in the stack space, for example

int[] nums = new int[10];

This nums is a reference to the object. The JVM allocates an address space for nums in the stack space, and the address reference in the stack space points to the object in the heap space.

3. The formal parameters of the method are also directly allocated in the stack space.

4. Local variables (defined in the method, exist with the method call, and are destroyed after the method call is completed. There is no initialized value, and must be defined and assigned before use) are directly allocated in the stack space. When the method where the local variable is located is executed The space is then immediately reclaimed by the JVM.

The heap (heap)
has only one heap in the JVM, which is created when the virtual machine is started, and all threads share this heap.

Features: It is discontinuous and is a memory area shared by all threads.

1. Store and create new objects, each object contains a corresponding class information, and the reference address of the new object will be stored in the stack.

2. Storage array.

Method area (method)
method area (method) is also called static area,

Features: Like the heap, the method area is a memory area shared by all threads, and the method area contains the only elements that are always unique in the entire program.

1. The main storage here is class (class), static method, static variable, constant and member method, and the constant pool we often say is also part of the method area.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45817985/article/details/130839661