Java stack and heap, static area

The memory of Java JVM can be divided into three areas: heap, stack and method

Stacking area:

1. All objects stored are objects, and each object contains the information of a corresponding class. (the purpose of class is to get operation instructions).

2. The JVM has only one heap shared by all threads. The heap does not store basic types and object references, but only the object itself.

Stack area:

1. Each thread contains a stack area. The stack only stores the references of basic data type objects and user-defined objects (not objects), and the objects are stored in the heap area.

2. The data (original type and object reference) in each stack is private and cannot be accessed by other stacks.

3. The stack is divided into three parts: basic type variable area, execution environment context and operation instruction area (storing operation instructions).

Method area:

1. It is also called static area. Like heap, it is shared by all threads. The method area contains all class and static variables.

2. The method area contains elements that are always unique in the whole program, such as class and static variables.

The difference between heap and stack in Java language:

1. Both stack and heap are places where Java stores data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set stacks or heaps.

2. The stack stores local variables (variables of basic types) and object references. The advantage of the stack is that the access speed is faster than the heap, second only to registers, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be determined and lack flexibility. The stack follows threads, and there is a stack with threads.

3. Store objects in the heap, including object variables and object methods. The advantage of heap is that it can dynamically allocate the memory size, and the lifetime does not need to tell the compiler in advance. The Java garbage collector will automatically collect the data that is no longer used. However, the disadvantage is that the access speed is slow due to the dynamic allocation of memory at run time. The heap follows the JVM, and there is heap memory when there is a JVM.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>