Knowledge Base 文章 Q10758,“用 calloc() 和 malloc() 管理内存” (搜索 文章编号), 包含了有关这些主题的更多背景知识。另外,有关堆实现和设计的详细讨论也可在下列著作中找 到:“Dynamic Storage Allocation: A Survey and Critical Review”, 作者 Paul R. Wilson、Mark S. Johnstone、 Michael Neely 和 David Boles;“International Workshop on Memory Management”, 作者 Kinross, Scotland, UK, 1995 年 9 月(http://www.cs.utexas.edu/users/oops/papers.html)(英文)。
Windows NT 的实现(Windows NT 版本 4.0 和更新版本) 使用了 127 个大小从 8 到 1,024 字节的 8 字节对齐块空闲列表和一个“大块”列表。“大 块”列表(空闲列表[0]) 保存大于 1,024 字节的块。空闲列表容纳了用双向链表链接在一起的对象。默认情况下, “进程堆”执行收集操作。(收集是将相邻空闲块合并成一个大块的操作。)收集耗费了额外的周期,但减少了堆块的内部碎片。
单一全局锁保护 堆,防止多线程式的使用。(请参见 “Server Performance and Scalability Killers”中的第一个注意事 项, George Reilly 所著, 在 “MSDN Online Web Workshop”上(站点:http://msdn.microsoft.com/workshop/server/iis/tencom.asp(英文)。)单一全局锁本质上是用来保护堆数据结构,防止跨多线程的随机存取。若堆操作太频繁,单一全局锁会对性能有不利的影响。
上述改进已 在 Windows 2000 beta 2 和 Windows NT 4.0 SP4 中使用。改进后,堆锁的竞争率显著降低。这使所 有 Win32 堆的直接用户受益。CRT 堆建立于 Win32 堆的顶部,但它使用自己的小块 堆,因而不能从 Windows NT 改进中受益。(Visual C++ 版 本 6.0 也有改进的堆分配程序。)
1) about stack, system will allocate memory to the instance of object automatically, and to the
heap, you must allocate memory to the instance of object with new or malloc manually.
2) when function ends, system will automatically free the memory area of stack, but to the
heap, you must free the memory area manually with free or delete, else it will result in memory
leak.
3)栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。
4)堆上分配的内存可以有我们自己决定,使用非常灵活。 作者: cao1n 时间: 2012-12-1 19:02