什么是内存堆? [英] What is a Memory Heap?
问题描述
什么是内存堆?
大概是从内存分配的角度而不是从数据结构的角度来看 heap (该术语具有多种含义).
一个非常简单的解释是,堆是动态分配的内存所在的内存部分(即通过malloc
分配的内存).从堆分配的内存将保持分配状态,直到发生以下情况之一:
- 内存是
free
' - 程序终止
如果所有对已分配内存的引用都丢失了(例如,您不再存储指向该内存的指针),那么您就会遇到内存泄漏.这是仍在分配内存的地方,但是您再也没有简单的方法来访问它了.泄漏的内存无法回收以用于将来的内存分配,但是在程序结束时,操作系统将释放内存.
将此与 stack 内存进行对比,该内存是局部变量(在方法中定义的变量)所在的位置.通常,只有在函数返回之前,堆栈上分配的内存才有效(这有一些例外,例如静态局部变量).
您可以在本文中找到有关堆的更多信息.. >
What is a memory heap ?
Presumably you mean heap from a memory allocation point of view, not from a data structure point of view (the term has multiple meanings).
A very simple explanation is that the heap is the portion of memory where dynamically allocated memory resides (i.e. memory allocated via malloc
). Memory allocated from the heap will remain allocated until one of the following occurs:
- The memory is
free
'd - The program terminates
If all references to allocated memory are lost (e.g. you don't store a pointer to it anymore), you have what is called a memory leak. This is where the memory has still been allocated, but you have no easy way of accessing it anymore. Leaked memory cannot be reclaimed for future memory allocations, but when the program ends the memory will be free'd up by the operating system.
Contrast this with stack memory which is where local variables (those defined within a method) live. Memory allocated on the stack generally only lives until the function returns (there are some exceptions to this, e.g. static local variables).
You can find more information about the heap in this article.
这篇关于什么是内存堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!