An article to understand the difference and connection between heap and stack

Heap and stack summary

In the computer field, the stack is a concept that cannot be ignored. The stack is two data structures. A stack is a data structure in which data items are arranged in order. Data items can only be inserted and deleted at one end (called the top). In SCM applications, the stack is a special memory area, the main function is to temporarily store data and addresses, usually used to protect breakpoints and sites.

The main points of heap and stack

Heap, queue priority, FIFO-first infirst out.

Stack, Advanced Out (FILO-First-In/Last-Out).

In general, if someone puts the stack together, it means stack, not heap.

An article to understand the difference and connection between heap and stack

Heap and stack comparison analysis

1, stack space allocation

Stack (operating system): The operating system automatically allocates and releases, stores the parameter values ​​of the functions, and the values ​​of local variables. It operates like a stack in a data structure.

Heap (operating system): generally released by the programmer to release, if the programmer does not release, the program may be recycled by the OS at the end, the distribution is similar to the linked list

2, stack cache

The stack uses a level 1 cache. They are usually stored in memory when called, and released immediately after the call is complete.

The heap is stored in the second-level cache, and the life cycle is determined by the virtual machine's garbage collection algorithm (not once it is an orphan object, it can be recycled). So the speed of calling these objects is relatively low.

3, stack data structure difference

Heap (data structure): Heap can be seen as a tree, such as: Heap sort.

Stack (data structure): An advanced data structure.

An article to understand the difference and connection between heap and stack

Heap and stack links

The main function advanced stack, a variable arr is defined in the stack, then the arr assignment, but the right is not a specific value, is an entity. The entity is created in the heap. In the heap, a space is first opened by the new keyword. The memory stores the data when it is reflected by the address. The address is a continuous binary, and then a memory address is assigned to this entity. Arrays all have an index. After the array entity is created in the heap memory, each space will be initialized by default. (This is a feature of heap memory. Uninitialized data cannot be used, but it can be used in a heap.) Because initialization was over, but not in the stack, different types of initialized values ​​are different. So variables and entities are created in the heap and stack:

An article to understand the difference and connection between heap and stack

Assign an address to the heap, assign the address of the heap to arr, and arr points to the array by address. So when arr wants to manipulate the array, it passes the address instead of directly assigning the entity to it. This we no longer call him the basic data type, but called the reference data type. The arr refers to entities in the heap memory. (It can be understood as a pointer to c or c++. Java grows much like C++ and C++, optimizing C++)

If int[]arr=null;

Arr doesn't do any pointing, and null's role is to dereference the reference data type.

When an entity does not have a reference to the data type, it is not released in the heap memory, but is treated as a garbage and is automatically recovered in an irregular time, because Java has an automatic recovery mechanism, (and C++ does not, The need for programmers to manually recycle, if you do not collect more heap, until full memory overflow, so Java is better than c++ in memory management). The automatic recovery mechanism (program) automatically monitors the heap for garbage, and if it does, it will automatically perform garbage collection, but when it does not, it will not be enough.

So the difference between heap and stack is obvious:

1. The stack memory stores local variables and the heap memory stores entities.

2. Stack memory updates faster than heap memory, because the life cycle of local variables is very short;

3. The life cycle of the variables stored in the stack memory will be released once it is finished, and the entities stored in the heap memory will be recycled from time to time by the garbage collection mechanism.

The main difference between heap and stack

1. Management: For the stack, it is automatically managed by the compiler and does not require manual control. For the heap, the release is controlled by the programmer and it is easy to generate memoryleak.

2, the size of the space: In general, 32-bit systems, heap memory can reach 4G space, from this point of view, there is almost no limit to the heap memory. However, for stacks, there is usually a certain amount of space. For example, under VC6, the default stack size is 1M (it seems that this is not clear). Of course, we can modify:

3, open the project, followed by the operation menu is as follows: Project-"Setting-"Link, select Output in Category, and then set the maximum value of the stack and commit in Reserve.

Note: The minimum value of reserve is 4 bytes; the commit is reserved in the virtual memory of the page file, and the larger it sets the stack will open up a larger value, which may increase the memory overhead and startup time.

4. Fragmentation problem: For the heap, frequent new/delete will inevitably cause the discontinuity of the memory space, resulting in a large number of fragments, which will reduce the efficiency of the program. For the stack, this problem does not exist, because the stack is advanced to the queue, they are so one-to-one correspondence, so that there can never be a block of memory from the middle of the stack, before he pops up, in The content of the backwards stack above him has already been popped up. For details, please refer to the data structure. We will not discuss them one by one here.

5, the growth direction: For the heap, the growth direction is upward, which is to increase the direction of the memory address; for the stack, its growth direction is downward, is toward the direction of the memory address decreases.

6. Allocation methods: Heaps are dynamically allocated and there is no statically allocated heap. The stack has two distribution methods: static allocation and dynamic allocation. Static allocation is done by the compiler, such as the allocation of local variables. Dynamic allocation is allocated by the alloca function, but the dynamic allocation of the stack and the heap are different, and its dynamic allocation is released by the compiler, and we do not need to manually implement it.

7, distribution efficiency: stack is the data structure provided by the machine system, the computer will provide support for the stack at the bottom: the allocation of a special register to store the stack address, push the stack have a dedicated instruction execution, which determines the efficiency of the stack Relatively high. The heap is provided by the C/C++ function library. Its mechanism is very complex. For example, to allocate a block of memory, the library function will search the heap memory according to a certain algorithm (the specific algorithm can refer to the data structure/operating system) The space of sufficient size, if there is not enough space (maybe due to too much memory fragmentation), it is possible to call system functions to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, and then proceed return. Obviously, the heap is much less efficient than the stack.

Entertainment Tablet

The advantage of entertainment tablet lies in having a powerful chip and excellent hardware. If you want to play various online videos on the tablet and run various software smoothly, the performance of the hardware is an absolute prerequisite. In addition, a high-speed and stable WIFI module is also required, which must be compatible with multiple 802.11 B/G/N protocols at the same time, so that the webpage can be loaded instantly when the webpage is opened, and the online video can be played smoothly. So hardware is the premise and foundation, without good performance everything else is empty talk.
In addition to having a good CPU as a prerequisite, it is equally important to carry a professional video player. A professional video player must perform equally well both online and offline.

Entertainment Tablet,High capacity battery Tablet,WIFI Tablet

Jingjiang Gisen Technology Co.,Ltd , https://www.jsgisengroup.com