A heap is a tree data structure where higher levels of the tree always contain greater (or lesser, if it's set up that way) values than lower levels.
Donald Knuth says (The Art of Computer Programming, Third Ed., Vol. 1, p. 435):
Several authors began about 1975 to call the pool of available memory a 'heap.' But in the present series of books, we will use that word only in its more traditional sense related to priority queues.
"The" heap is a bunch of free RAM that a program has available for dynamic allocation.
还有两幅形象的图片对比heap和stack
- stack(In a stack of items, items sit one on top of the other in the order they were placed there, and you can only remove the top one (without toppling the whole thing over).)
- heap(In a heap, there is no particular order to the way items are placed. You can reach in and remove items in any order because there is no clear 'top' item.)
从百度百科上看到一句话,“由于Stack的内存管理是顺序分配的,而且定长,不存在内存回收问题;而Heap 则是随机分配内存,不定长度,存在内存分配和回收的问题;” Why?
这个问题需要测试一下。