Home > Common Problem > body text

What are the differences between heap and stack

青灯夜游
Release: 2022-04-01 13:58:10
Original
114823 people have browsed it

The differences between heap and stack are: 1. Difference in space allocation; 2. Difference in caching method; 3. Difference in data structure. The heap space is generally allocated and released by the programmer, and the stack space is automatically allocated and released by the operating system (compiler). The stack uses the first level cache and the heap uses the second level cache.

What are the differences between heap and stack

What is the difference between heap and stack

1. Difference in stack space allocation

Stack (operating system): automatically allocated and released by the operating system (compiler), storing function parameter values, local variable values, etc. It operates like a stack in a data structure.

Heap (operating system): Generally allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS when the program ends. The allocation method is similar to a linked list.

2. Differences in stack caching methods

The stack uses a first-level cache. They are usually in the storage space when they are called and are released immediately after the call is completed.

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

3. Differences in stack data structure

Heap (data structure): The heap can be regarded as a tree, such as: heap sort.

Stack (data structure): A first-in, last-out data structure.

The above is the detailed content of What are the differences between heap and stack. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template