School of Computer Science and Technology, HIT
第9章虚拟内存:
动态内存分配——高级概念
教 师:吴锐
计算机科学与技术学院
哈尔滨工业大学
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1
School of Computer Science and Technology, HIT
主要内容
显式空闲链表
分离的空闲链表
垃圾收集
内存相关的风险和陷阱
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2
School of Computer Science and Technology, HIT
跟踪空闲块
方法1: 隐式空闲链表通过头部中的大小字段隐含地连接空
闲块
5 4 6 2
方法2: 显式空闲链表在空闲块中使用指针连接空闲块
5 4 6 2
方法3: 分离的空闲链表
每个大小类的空闲链表包含大小相等的块
方法4: 按照大小排序的块
可以使用平衡树(例如红黑树),在每个空闲块中有指针,大小作
为键。
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
School of Computer Science and Technology, HIT
显式空闲链表
已分配块 空闲块
块大小 a 块大小 a
上一个(前驱)
下一个(后继)
有效区域
块大小 a 块大小 a
保留空闲块链表, 而不是所有块
“下一个”空闲块可以在任何地方
因此我们需要存储前/后指针,而不仅仅是大小
还需要合并边界标记
幸运的是,我们只跟踪空闲块,所以我们可以使用有效区域。
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4
School of Computer Science and Technology, HIT
显式空闲链表
逻辑地:
A B C
物理地: 块的顺序是任何的
从前向后(后继) 链接
A