Understanding of heap area and stack area in java

Stack area: When a variable is defined, memory space is allocated in the stack area.
Heap area: When a new array or object is created, memory space is allocated in the heap area. By defining a variable in the stack area to point to the address of the heap area (why to do this, it is convenient to quickly access the array or object during operation, the variable is equivalent to the pointer in c)

Memory release:
stack area: After the scope of the variable is exceeded, Java automatically releases the memory space of the variable.
Heap area: The program exceeds the code block of the array or object generated by new, and the memory of the object or array in the heap will not be released. When there is no reference variable pointing to it, the array or object will become garbage and cannot be used. , but still occupies the memory and will be collected by the garbage collector at an indeterminate time.

—————————————————————————————

The storage of each area in the memory:
1. The stack area (stack) - is automatically allocated and released by the compiler, and stores the parameter value of the function, the value of the local variable, etc. It operates like a stack in a data structure. It is unique to each thread and saves its running state and local automatic variables. The stack is initialized when the thread starts, and the stack of each thread is independent of each other, so the stack is thread-safe. The data members of each C++ object also exist on the stack, each function has its own stack, and the stack is used to pass parameters between functions. The operating system will automatically switch the stack when switching threads, that is, switching the SS/ESP register. Stack space does not need to be allocated and freed explicitly in high-level languages.

2. The heap area (heap) - generally allocated and released by the programmer. If the programmer does not release it, it may be reclaimed by the os at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list, huh. It is the space shared by everyone, divided into global heap and local heap. The global heap is all unallocated space, and the local heap is user-allocated space. The heap is allocated when the operating system initializes the process. You can also ask the system for additional heap during the running process, but remember to return it to the operating system when it is used up, or it will be a memory leak. Static data is generally placed in the heap, such as static data and string constants. After the resources are loaded, they are generally placed in the heap. All threads of a process share these heaps, so the operation of the heap should consider the issues of synchronization and mutual exclusion. The compiled data segment in the program is part of the heap.

3. Global area (static area) (static) - The storage of global variables and static variables is put together, initialized global variables and static variables are in the same area, and uninitialized global variables and uninitialized static variables are in the same area. another adjacent area. There is a system release after the program ends

4. Text constant area - constant strings are placed here. Released by the system after the program ends

5. Program code area - store the binary code of the function body.

2. Theoretical knowledge of heap and stack
2.1 Application method
stack:
It is automatically allocated by the system. For example, a local variable int b declared in a function, the system automatically opens up space for b in the stack
heap: the
programmer needs to apply by himself and specify the size. In c, the malloc function
such as p1 = (char )malloc(10)
is in C++ uses the new operator
such as p2 = (char )new char[10]
but note that p1 and p2 themselves are in the stack.

2.2
Response of the system after application
Stack : As long as the remaining space of the stack is larger than the applied space, the system will provide memory for the program, otherwise an exception will be reported to indicate stack overflow.
Heap: First of all, you should know that the operating system has a linked list that records free memory addresses. When the system receives an application from a program, it will traverse the linked list to find the first heap node whose space is larger than the requested space, and then remove the node from the heap. Delete from the free node list and allocate the space of the node 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, the delete statement in the code In order to correctly release the memory space. In addition, since the size of the found heap node is not necessarily exactly equal to the size of the application, the system will automatically put the excess part back into the free list.

2.3 Application size limit
Stack : In Windows, the stack is a data structure that extends to a low address, and is a continuous memory area. This sentence means that the address of the top of the stack and the maximum capacity of the stack are predetermined by the system. Under Windows, the size of the stack is 2m (some say 1m, in short, it is a constant determined at compile time), if When the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, less space can be obtained from the stack.
Heap: The heap is a data structure that extends to high addresses and is a discontinuous area of ​​memory. 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 is limited by the virtual memory available in the computer system. It can be seen that the space obtained by the heap is more flexible and larger.

2.4 Comparison of application efficiency: The
stack is automatically allocated by the system, which is faster. But the programmer is out of control.
The heap is the memory allocated by new, which is generally slow and prone to memory fragmentation, but it is the most convenient to use.
In addition, under Windows, the best way is to use virtualalloc to allocate memory, which is not on the heap, nor on the stack. Reserve a block of memory directly in the address space of the process, although it is the most inconvenient to use. But fast and most flexible

2.5 Storage content in the heap and stack
Stack : When a function is called, the first thing pushed onto the stack is the address of the next instruction after the main function (the next executable statement of the function call statement), and then the parameters of the function , in most C compilers, parameters are pushed onto the stack from right to left, followed by local variables in the function. Note that static variables are not pushed onto the stack. When this function call ends, the local variables are popped off the stack first, then the parameters, and finally the top of the stack pointer points to the first stored address, which is the next instruction in the main function, and the program continues to run from this point.
Heap: Generally, one byte is used to store the size of the heap at the head of the heap. The specific content of the heap is arranged by the programmer.

2.6 Comparison of access efficiency
char s1[] = ” aaaaaaaaaaaaaaa”
char s2 = ” bbbbbbbbbbbbbbbbb”
aaaaaaaaaaa is assigned at runtime;
bbbbbbbbbbbb is determined at compile time;
however, in subsequent accesses, on the stack An array of is faster than a string pointed to by a pointer (such as the heap).
For example:
include
void main()
{
char a = 1
char c[] = ” 1234567890”
char p = ” 1234567890”
a = c[1]
a = p[1]
return
}
The corresponding assembly code
10: a = c[ 1]
00401067 8a 4d f1 mov cl byte ptr [ebp-0fh]
0040106a 88 4d fc mov byte ptr [ebp-4] cl
11: a = p[1]
0040106d 8b 55 ec mov edx dword ptr [ebp-14h]
00401070 8a 42 01 mov al byte ptr [edx+1]
00401073 88 45 fc mov byte ptr [ebp-4] al
The first type directly reads the elements in the string into the register cl when reading, and the second type reads the pointer value into edx first, in Reading characters according to edx, apparently slow.

2.7 Summary:
The difference between a heap and a stack can be seen with the following analogy:
using a stack is like going to a restaurant to eat, just order food (issue an application), pay, and eat (use), and leave when you are full. Don't worry about the preparations such as chopping and washing vegetables, and the finishing work such as washing dishes and washing pots. His advantage is that it is fast, but the degree of freedom is small.
Using the heap is like making your own favorite dishes. It is more troublesome, but it is more in line with your own taste and has a large degree of freedom.

The difference between the heap and the stack is mainly divided into:
the heap and stack of the operating system, such as those mentioned above, not much to say.
There are also heaps and stacks in terms of data structures, which are different concepts. The heap here actually refers to a data structure of a priority queue (satisfying the nature of the heap), and the first element has the highest priority; the stack is actually a mathematical or data structure that satisfies the first-in, last-out nature.
Although stacks and stacks are called together, they are still very different. The connection is only due to historical reasons.

This article is transferred from http://blog.csdn.net/qq125293177/article/details/52600923

Guess you like

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