Briefly describe the difference between heap memory and stack memory in JAVA

Java divides memory into two types: one is stack memory and the other is heap memory.

One, stack memory

Store basic types of variables, object references and method calls, following the principle of first in, first out.
The "variables of some basic types and reference variables of objects" defined in the function in the stack memory are allocated in the stack memory of the function. When a variable is defined in a code block, Java allocates memory space for this variable on the stack. When the scope of the variable is exceeded, Java will automatically release the memory space allocated for the variable, and the memory space can be immediately used Use it for other purposes.

The code in Java is executed in the body of the function, and the body of each function is placed in the stack memory, such as the main function. If other functions are called in the main function, such as add(), then the storage in the stack is main at the bottom and add on the mian. The runtime of the stack is last-in, first-out, so when it is executed, add will be destroyed first, and then main will be destroyed.

The advantage of the stack is that the stack memory is very small compared to the heap memory, the access speed is faster than the heap, second only to the registers, and the stack data can be shared. But the disadvantage is that the size and lifetime of the data stored in the stack must be determined, which lacks flexibility. The stack mainly stores some basic types of variables (int, short, long, byte, float, double, boolean, char) and object handles. A very important particularity of the stack is that the data stored in the stack can be shared.

Two, heap memory

Store all new objects and arrays

It is hereby emphasized that the heap memory and the heap in the data structure are completely different things, and the allocation method is similar to a linked list.

Heap memory is another memory area that is different from the stack area, global data area, and code area. The heap allows the program to dynamically apply for a certain size of memory space at runtime. The heap memory actually refers to a data structure of the priority queue (which satisfies the nature of the heap memory), and the first element has the highest priority.

When allocating heap memory, you should first know that the operating system has a linked list that records free memory addresses. When the system receives an application from the program, it will traverse the linked list to find the first heap node with a larger space than the requested space, and then The node is deleted from the list of free nodes, and the space of the node is allocated to the program. In addition, for most systems, the size of this allocation will be recorded at the first address in this memory space. In this way, in the code The delete statement can release the memory space correctly.

In addition, since the size of the heap node found may not be exactly equal to the size of the application, the system will automatically put the extra part into the free list. Heap memory is a data structure that extends to high addresses and is a discontinuous memory area. This is because the system uses a linked list to store free memory addresses, which are naturally discontinuous, and the traversal direction of the linked list is from low addresses to high addresses. The size of the heap memory is limited by the effective virtual memory in the computer system. It can be seen that the space obtained by the heap memory is more flexible and larger. Heap memory is the memory allocated by new, generally slower and prone to memory fragmentation, but it is the most convenient to use.

Stack and heap are both places where Java stores data in Ram. Unlike C++, Java automatically manages the stack and heap, and programmers cannot directly set the stack or heap

Java's heap is a runtime data area, from which objects are allocated. These objects are created by instructions such as new, newarray, anewarray, and multianewarray. They do not need to be explicitly released by program code. The heap is collected by garbage. Responsible, the advantage of the heap is that the size of the memory can be dynamically allocated, and the lifetime does not need to be told to the compiler in advance, because it dynamically allocates memory at runtime, and the garbage collector of Java will automatically take away the data that is no longer used. .But the disadvantage is that the access speed is slow due to the dynamic allocation of memory at runtime.

Three, other data storage

1. Constant pool: store basic type constants and string constants (public static final)
2. Static domain: store static members (defined by static)
3. Non-RAM storage: permanent storage space such as hard disks

The difference between heap memory and stack memory:

1. All parts of the application use heap memory, and then the stack memory is used by running a thread.
2. No matter when the object is created, it will be stored in the heap memory, and the stack memory contains its references. The stack memory only contains references to original value variables and object variables in the heap.
3. Objects stored in the heap are globally accessible, but the stack memory cannot be accessed by other threads.
4. The memory management in the stack is done using LIFO, and the management of heap memory is more complicated because it is accessed globally.
5. The stack memory has a short life cycle, but the life cycle of the heap memory starts from the beginning of the program to the end.
6. We can use the -Xms and -Xmx JVM options to define the starting size and the maximum heap memory, and we can use -Xss to define the stack size.
7. When the stack memory is full, Java throws a java.lang.StackOverFlowError exception When the heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error
8. Compared with the heap memory, the stack memory is much smaller because the memory allocation rules (LIFO) are clearly used, which is compared with the heap memory. very fast.

to sum up:

1 Stack: automatically allocate and release for the compiler, such as function parameters, local variables, temporary variables, etc.

2 Heap: Allocate and release members, apply for and release by the programmer himself. Otherwise, a memory leak occurs. Typically, the contents of the heap used for the new application.
In addition to these two parts, there is another part:

3 Static storage area: The memory is allocated when the program is compiled, and this memory exists during the entire running period of the program. It mainly stores static data, global data and constants.

Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/113448069