Upload
jesse-morrin
View
278
Download
10
Embed Size (px)
Citation preview
2023.04.18 Institute of Computer SoftwareNanjing University
Memory Management
马晓星
南京大学计算机软件研究所南京大学计算机科学与技术系
2023.04.18 Institute of Computer SoftwareNanjing University
摘要 内存管理问题简介 手工内存管理
Problems of Memory Management in C++ 自动垃圾回收 小结
2023.04.18 Institute of Computer SoftwareNanjing University
内存管理问题简介 对象占用物理资源,而资源是有限的
对象的生命周期与系统的生命周期不一致 系统生命周期外的对象 -- persistent 系统生命周期里 对象不再有用 回收内存
教训 92 伦敦救护车调度事件
2023.04.18 Institute of Computer SoftwareNanjing University
内存管理问题简介 问题从哪里来?
三种对象内存管理模型: 静态分配 栈式动态分配 堆式动态分配
2023.04.18 Institute of Computer SoftwareNanjing University
静态分配
2023.04.18 Institute of Computer SoftwareNanjing University
class ClassA{…}
ClassA& test(){
ClassA MyA;
….
return MyA;
};
2023.04.18 Institute of Computer SoftwareNanjing University
栈式动态分配
2023.04.18 Institute of Computer SoftwareNanjing University
块结构语言的分配与去配
2023.04.18 Institute of Computer SoftwareNanjing University
堆式动态分配
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
What to do with unreachable objects
Reference assignments may make some
objects useless.
Two possible approaches: Manual reclamation (e.g. C++, Delphi). 自动垃圾回收 (e.g. Eiffel, Smalltalk, Simula, Java, .NET)
“Almaviva”name
landlord
loved_one
aO1
“Figaro”O2
“Susanna”
O3
2023.04.18 Institute of Computer SoftwareNanjing University
Manual Reclamation
看如下 C++ 代码?(引自 Effective C++ )
string *stringarry = new string[100];
……
delete stringarray;
delete [ ] stringarray;
2023.04.18 Institute of Computer SoftwareNanjing University
更糟糕的是typedef string addresslines[4];
// 一个人的地址,共 4 行,每行一个 string
// 因为 addresslines 是个数组,使用 new:
string *pal = new addresslines; // 注意 "new addresslines" 返回 string*, 和 // "new string[4]" 返回的一样 delete 时必须以数组形式与
之对应:
delete pal; // 错误 !
delete [] pal; // 正确
2023.04.18 Institute of Computer SoftwareNanjing University
Manual Reclamation
一般谁创建,谁回收 构造函数里 new 析构函数里 delete 进 block new 出 block delete
但是,共享的情况? 创建者先消亡;而被创建对象还须为其它对象使用
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
Mutable vs. Immutable?
2023.04.18 Institute of Computer SoftwareNanjing University
Automatic Garbage Collection
Why?
What?
How?
2023.04.18 Institute of Computer SoftwareNanjing University
Arguments for automatic collection
Manual reclamation is dangerous. Hampers software reliability.
In practice bugs arising from manual reclamation are among the most difficult to detect and correct. Manifestation of bug may be far from source.
Manual reclamation is tedious: need to write “recursive dispose” procedures.
Modern garbage collectors have acceptable overhead (a few percent) and can be made compatible with real-time requirement.
GC is tunable: disabling, activation, parameterization....
2023.04.18 Institute of Computer SoftwareNanjing University
What
何谓“无用”对象? 当如何处理之?
无用对象是否会对系统运行产生其他影响?
2023.04.18 Institute of Computer SoftwareNanjing University
有用对象
(no root object in java)
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
Properties of a garbage collector (GC)
Consistency (never reclaim a reachable object). Completeness (reclaim every unreachable object –
eventually).
Consistency (also called safety) is an absolute requirement. Better no GC than an unsafe GC.
But: safe automatic garbage collection is hard or impossible in a hybrid language environment (e.g. C++): pointers may masquerade as integers or other values.
2023.04.18 Institute of Computer SoftwareNanjing University
How ? 如何找到无用对象?
两类策略 引用计数 Tracing
回收时的额外处理
2023.04.18 Institute of Computer SoftwareNanjing University
引用计数 引用计数
简单 实用 分散回收
原理
问题
2023.04.18 Institute of Computer SoftwareNanjing University
引用计数 循环引用
2023.04.18 Institute of Computer SoftwareNanjing University
引用计数 回路检测
通常的 Garbage Collector 中不常用 Sequential implementation
但 在并行、分布、实时应用领域仍有用 局部性好 (cache friendly) 分散回收 (incremental)
2023.04.18 Institute of Computer SoftwareNanjing University
Tracing collectors
Mark and sweep Mark phase:
traverse the tree of references and marks each object it encounters.
Sweep phase: unmarked objects are freed, and the resulting memory
is made available to the executing program.
2023.04.18 Institute of Computer SoftwareNanjing University
Tri-color marking
white, grey, and black sets Initially all but root objects are in white, root
objects in grey. (repeat until the grey set is empty.) Pick an object
from the grey set. Blacken this object (move it to the black set), by greying all the white objects it references directly.
Invariant: No black object points directly to a white object.
2023.04.18 Institute of Computer SoftwareNanjing University
Tracing collectors
Finalization: sweep phase must include finalization of objects. objects referred to by a finalizable object must
remain on the heap until after the object has been finalized.
be aware that finalizers are called by GC,
2023.04.18 Institute of Computer SoftwareNanjing University
Mark and Sweep
看看 Bill Venners 的 Heap of Fish
http://cui.unige.ch/tios/staff/Jarle.Hulaas/cours-langages/doc/HeapOfFish/
2023.04.18 Institute of Computer SoftwareNanjing University
Problems?
Fragmentation?
When? On demand? -- avoid using virtual memory? Performance issues:
Overall performance Incrementality
2023.04.18 Institute of Computer SoftwareNanjing University
Defragmentation
两种策略 Compacting Copying
Compact Collectors 往一端移动对象 看 Bill Venners 的 Heap of Fish Reference updating?
Adding a level of redirection
2023.04.18 Institute of Computer SoftwareNanjing University
Copy Collectors
可用内存一分为二 move all live objects to a new area they are placed side by side old area is then known to be all free space
在 trace 的同时 move 留下 forwarding pointers
STOP and COPY
2023.04.18 Institute of Computer SoftwareNanjing University
Advanced techniques
Give some control to developers 打开 关闭 GC 强制调用 GC
On-the-fly (parallel) GC Separated execution thread for GC (with HW
support?) Generation Scavenging
Old objects will stay around “Tenuring”
2023.04.18 Institute of Computer SoftwareNanjing University
关于 Java 的 ref 包 GC 灵活性不够?让程序开发者适当参与
区分 Strongly reachable Softly rechable Weakly reachable Phantomly reachable Unreachable
通知 清除 soft weak 引用后 Reclaim 前
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
2023.04.18 Institute of Computer SoftwareNanjing University
Java GC 处理顺序 Soft references Weak references Finalization Phantom references Reclamation
2023.04.18 Institute of Computer SoftwareNanjing University
小结
对象生命周期 三种内存分配 对于堆式动态分配
手工回收 自动回收
自动回收的若干策略
自动内存管理是现代 OO 语言的重要特征
2023.04.18 Institute of Computer SoftwareNanjing University
作业 -4 (本次作业不用提交) 象 Java 这样具有 Garbage Collection 机制的
语言环境,在实际使用中也会出现类似于内存泄漏的问题。你可以想象这样的情况么?如何解决? ( 提示:考虑 Collections 的使用 )
学习并小结 Microsoft .NET 环境提供的Garbage Collection 机制。