Presentation is loading. Please wait.

Presentation is loading. Please wait.

2015/4/26Institute of Computer Software Nanjing University Memory Management 马晓星 南京大学计算机软件研究所 南京大学计算机科学与技术系.

Similar presentations


Presentation on theme: "2015/4/26Institute of Computer Software Nanjing University Memory Management 马晓星 南京大学计算机软件研究所 南京大学计算机科学与技术系."— Presentation transcript:

1 2015/4/26Institute of Computer Software Nanjing University Memory Management 马晓星 南京大学计算机软件研究所 南京大学计算机科学与技术系

2 2015/4/26Institute of Computer Software Nanjing University 摘要 内存管理问题简介 手工内存管理 Problems of Memory Management in C++ 自动垃圾回收 小结

3 2015/4/26Institute of Computer Software Nanjing University 内存管理问题简介 对象占用物理资源,而资源是有限的 对象的生命周期与系统的生命周期不一致 系统生命周期外的对象 -- persistent 系统生命周期里 对象不再有用 回收内存 教训 92 伦敦救护车调度事件

4 2015/4/26Institute of Computer Software Nanjing University 内存管理问题简介 问题从哪里来? 三种对象内存管理模型: 静态分配 栈式动态分配 堆式动态分配

5 2015/4/26Institute of Computer Software Nanjing University 静态分配

6 2015/4/26Institute of Computer Software Nanjing University class ClassA{…} ClassA& test(){ ClassA MyA; …. return MyA; };

7 2015/4/26Institute of Computer Software Nanjing University 栈式动态分配

8 2015/4/26Institute of Computer Software Nanjing University 块结构语言的分配与去配

9 2015/4/26Institute of Computer Software Nanjing University 堆式动态分配

10 2015/4/26Institute of Computer Software Nanjing University

11 2015/4/26Institute of Computer Software Nanjing 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 a O1 “Figaro” O2 “Susanna” O3

12 2015/4/26Institute of Computer Software Nanjing University Manual Reclamation 看如下 C++ 代码? (引自 Effective C++ ) string *stringarry = new string[100]; …… delete stringarray; delete [ ] stringarray;

13 2015/4/26Institute of Computer Software Nanjing University 更糟糕的是 typedef string addresslines[4]; // 一个人的地址,共 4 行,每行一个 string // 因为 addresslines 是个数组,使用 new: string *pal = new addresslines; // 注意 "new addresslines" 返回 string*, 和 // "new string[4]" 返回的一样 delete 时必须以数组形式与 之对应: delete pal; // 错误 ! delete [] pal; // 正确

14 2015/4/26Institute of Computer Software Nanjing University Manual Reclamation 一般谁创建,谁回收 构造函数里 new 析构函数里 delete 进 block new 出 block delete 但是,共享的情况? 创建者先消亡;而被创建对象还须为其它对象使用

15 2015/4/26Institute of Computer Software Nanjing University

16 2015/4/26Institute of Computer Software Nanjing University

17 2015/4/26Institute of Computer Software Nanjing University

18 2015/4/26Institute of Computer Software Nanjing University Mutable vs. Immutable?

19 2015/4/26Institute of Computer Software Nanjing University Automatic Garbage Collection Why? What? How?

20 2015/4/26Institute of Computer Software Nanjing 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....

21 2015/4/26Institute of Computer Software Nanjing University What 何谓 “ 无用 ” 对象? 当如何处理之? 无用对象是否会对系统运行产生其他影响?

22 2015/4/26Institute of Computer Software Nanjing University 有用对象 (no root object in java)

23 2015/4/26Institute of Computer Software Nanjing University

24 2015/4/26Institute of Computer Software Nanjing 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.

25 2015/4/26Institute of Computer Software Nanjing University How ? 如何找到无用对象? 两类策略 引用计数 Tracing 回收时的额外处理

26 2015/4/26Institute of Computer Software Nanjing University 引用计数 简单 实用 分散回收 原理 问题

27 2015/4/26Institute of Computer Software Nanjing University 引用计数 循环引用

28 2015/4/26Institute of Computer Software Nanjing University 引用计数 回路检测 通常的 Garbage Collector 中不常用 Sequential implementation 但 在并行、分布、实时应用领域仍有用 局部性好 (cache friendly) 分散回收 (incremental)

29 2015/4/26Institute of Computer Software Nanjing 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.

30 2015/4/26Institute of Computer Software Nanjing 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.

31 2015/4/26Institute of Computer Software Nanjing 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,

32 2015/4/26Institute of Computer Software Nanjing University Mark and Sweep 看看 Bill Venners 的 Heap of Fish langages/doc/HeapOfFish/

33 2015/4/26Institute of Computer Software Nanjing University Problems? Fragmentation? When? On demand? -- avoid using virtual memory? Performance issues: Overall performance Incrementality

34 2015/4/26Institute of Computer Software Nanjing University Defragmentation 两种策略 Compacting Copying Compact Collectors 往一端移动对象 看 Bill Venners 的 Heap of Fish Reference updating? Adding a level of redirection

35 2015/4/26Institute of Computer Software Nanjing 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

36 2015/4/26Institute of Computer Software Nanjing 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”

37 2015/4/26Institute of Computer Software Nanjing University 关于 Java 的 ref 包 GC 灵活性不够?让程序开发者适当参与 区分 Strongly reachable Softly rechable Weakly reachable Phantomly reachable Unreachable 通知 清除 soft weak 引用后 Reclaim 前

38 2015/4/26Institute of Computer Software Nanjing University

39 2015/4/26Institute of Computer Software Nanjing University

40 2015/4/26Institute of Computer Software Nanjing University

41 2015/4/26Institute of Computer Software Nanjing University

42 2015/4/26Institute of Computer Software Nanjing University

43 2015/4/26Institute of Computer Software Nanjing University Java GC 处理顺序 Soft references Weak references Finalization Phantom references Reclamation

44 2015/4/26Institute of Computer Software Nanjing University 小结 对象生命周期 三种内存分配 对于堆式动态分配 手工回收 自动回收 自动回收的若干策略 自动内存管理是现代 OO 语言的重要特征

45 2015/4/26Institute of Computer Software Nanjing University 作业 -4 (本次作业不用提交) 象 Java 这样具有 Garbage Collection 机制的语 言环境,在实际使用中也会出现类似于内存泄 漏的问题。你可以想象这样的情况么?如何解 决? ( 提示:考虑 Collections 的使用 ) 学习并小结 Microsoft.NET 环境提供的 Garbage Collection 机制。


Download ppt "2015/4/26Institute of Computer Software Nanjing University Memory Management 马晓星 南京大学计算机软件研究所 南京大学计算机科学与技术系."

Similar presentations


Ads by Google