上海交通大学:《编译原理》教学资源_第六周讲义_Heap Management

Heap Management slide I
Heap Mana gement slide 1

Quote of the day "Manually managing blocks of memory in C is like juggling bars of soap in a prison shower: It's all fun and games until you forget about one of them.” Unknown slide2
Quote of the Day “Manually managing blocks of memory in C is lik j li b f i i h like juggling bars of soap in a prison shower: It's all fun and games until you forget about one ofh ” t em. - Unknown slide 2

Major Areas of Memory ●Static area Fixed size,fixed content,allocated at compile time ·Run-time stack Variable size,variable content(activation records) -Used for managing function calls and returns ·Heap Fixed size,variable content Dynamically allocated objects and data structures Examples:ML reference cells,malloc in C,new in Java slide 3
Major Areas of Memory • Static area – Fixed size, fixed content, allocated at compile time • Run-time stack – Variable size, variable content (activation records) – Used for managing function calls and returns • Heap – Fixed size variable content Fixed size, variable content – Dynamically allocated objects and data structures • Examples: ML reference cells, malloc in C, new in Java slide 3

Heap Storage Memory allocation under explicit programmatic control -C malloc,C++/Pascal Java/C#new operation. Memory allocation implicit in language constructs -Lisp,Scheme,Haskel,SML,..most functional languages Autoboxing/unboxing in Java 1.5 and C# Deallocation under explicit programmatic control -C,C++,Pascal Deallocation implicit Java,C#,Lisp,Scheme,Haskel,SML
Heap Storage • Memory allocation under explicit programmatic control – C malloc, C++ / Pascal / Java / C# new operation. • Memory allocation implicit in language constructs – Lisp, Scheme, Haskel, SML, … most functional languages – Autoboxing/unboxing in Java 1.5 and C# • Deallocation under explicit programmatic control – C, C++, Pascal • Deallocation implicit – Java, C#, Lisp, Scheme, Haskel, SML, …

Heap Storage Deallocation Explicit versus Implicit Deallocation In explicit memory management,the program must explicitly call an operation to release memory back to the memory management system. In implicit memory management,heap memory is reclaimed automatically by a "garbage collector". Examples: Implicit:Java,Scheme Explicit:Pascal and C To free heap memory a specific operation must be called. Pascal ==dispose C ==free C++==delete Implicit and Explicit:Ada Deallocation on leaving scope
Heap Storage Deallocation Explicit versus Implicit Deallocation In explicit memory management, the program must explicitly In explicit memory management, the program must explicitly call an operation to release memory back to the memory management system. In implicit memory management, heap memory is reclaimed automatically by a “garbage collector”. Examples: • Implicit: Java, Scheme • Explicit: Pascal and C Explicit: Pascal and C To free heap memory a specific operation must be called. Pascal ==> dispose C == > free C++ ==> delete • Implicit and Explicit: Ada Deallocation on leaving scope

Cells and Liveness Cell data item in the heap -Cells are"pointed to"by pointers held in registers,stack,global/static memory,or in other heap cells Roots:registers,stack locations,global/static variables A cell is live if its address is held in a root or held by another live cell in the heap slide 6
Cells and Liveness • Cell = data item in the heap – Cells are “pointed to” by pointers held in registers, stack, global/static memory, or in other heap cells • Roots: registers, stack locations, global/static variables • A cell is live if its address is held in a root or held by another live cell in the heap slide 6

Garbage 。 Garbage is a block of heap memory that cannot be accessed by the program An allocated block of heap memory does not have a reference to it (cell is no longer "live") -Another kind of memory error:a reference exists to a block of memory that is no longer allocated 。 Garbage collection(GC)-automatic management of dynamically allocated storage Reclaim unused heap blocks for later use by program slide7
Garbage • Garbage is a block of heap memory that cannot be accessed by the program – An allocated block of heap memory does not have a reference to it (cell is no longer An allocated block of heap memory does not have a reference to it (cell is no longer “live”) – Another kind of memory error: a reference exists to a block of memory that is no longer allocated • Garbage collection (GC) - automatic management of dynamically allocated storage allocated storage – Reclaim unused heap blocks for later use by program slide 7

Example of Garbage class node int value; p=new node(); node next; q new node(); q=p; delete p, node p,q, null q (a) (b) (c) slide 8
Example of Garbage class node { int value; node next; p = new node(); q = new node(); node next; } node p, q; q (); q = p; delete p; p, q; slide 8

OO Languages and heap allocation Objects are a lot like records and instance variables are a lot like fields. =The representation of objects is similar to that of a record. Methods are a lot like procedures. =>Implementation of methods is similar to routines. But...there are differences: Objects have methods as well as instance variables,records only have fields. The methods have to somehow know what object they are associated with (so that methods can access the object's instance variables)
OO Langg p ua es and heap allocation Objects are a lot like records and instance variables are a lot like fields. like fields. => The representation of objects is similar to that of a record. Methods are a lot like procedures. => Implementation of methods is similar to routines. But… there are differences: there are differences: Objects have methods as well as instance variables, records only have fields. Th th d h t h k h t bj t th The methods have to somehow know what object they are associated with (so that methods can access the object’s instance variables) instance variables)

Example:Representation of a simple Java object Example:a simple Java object(no inheritance) class Point int x,y; (1public Point(int x,int y){ this.x=x;this.y=y; (2public void move(int dx,int dy){ x=x+dx;y=y+dy; (3public float area (){... (4public float dist (Point other){
Example: Representation of a simple Java object Example: a simple Java object (no inheritance) class Point { class Point { int x,y; ( 1 )p y ublic Point(int x, int y) { this.x=x; this.y=y; } ( ) public void move(int dx, int dy) { x=x+dx; y=y+dy; (2) x=x+dx; y=y+dy; } public float area() { ...} public float dist(Point other) { ... } } (3) (4)
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《编译原理》教学资源_第六周讲义_Intermediate Code Generation.pdf
- 上海交通大学:《编译原理》教学资源_第八周讲义_Machine-Independent Optimizations Ⅲ.pdf
- 上海交通大学:《编译原理》教学资源_第八周讲义_Machine-Independent Optimizations Ⅱ.pdf
- 上海交通大学:《编译原理》教学资源_第八周讲义_Machine-Independent Optimizations Ⅰ.pdf
- 上海交通大学:《编译原理》教学资源_第二周讲义_lex.pdf
- 上海交通大学:《编译原理》教学资源_第二周讲义_Syntax Analyzer.pdf
- 上海交通大学:《编译原理》教学资源_第二周讲义_Lexical Analyzer.pdf
- 上海交通大学:《编译原理》教学资源_第九周讲义_Machine-Independent Optimizations Ⅳ.pdf
- 上海交通大学:《编译原理》教学资源_第九周讲义_CS308 Compiler Theor.pdf
- 上海交通大学:《编译原理》教学资源_第九周讲义_CS308 Compiler Theor.pdf
- 上海交通大学:《编译原理》教学资源_第三周讲义_Bottom-Up Parsing.pdf
- 上海交通大学:《编译原理》教学资源_第三周讲义_Top-Down Parsing.pdf
- 上海交通大学:《编译原理》教学资源_第七周讲义_Code Generation.pdf
- 上海交通大学:《编译原理》教学资源_第一周讲义_A Simple Syntax-Directed Translator.pdf
- 上海交通大学:《编译原理》教学资源_第一周讲义_Introduction to Compilin.pdf
- 上海交通大学:《编译原理》教学资源_第一周讲义_Parameter Passing Mechanisms.pdf
- 《程序设计思想与方法》课程教学资源(书籍文献)简明Python教程.pdf
- 《程序设计思想与方法》课程教学资源(书籍文献)Python Programming:An Introduction to Computer Science.pdf
- 《程序设计思想与方法》课程教学资源(书籍文献)Python Programming:An Introduction to Computer Science.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_C 12.28上机测试题_C++ 上机测试题.pdf
- 上海交通大学:《编译原理》教学资源_第六周讲义_Run-Time Environments.pdf
- 上海交通大学:《编译原理》教学资源_第四周讲义_Syntax-Directed Translation.pdf
- 《计算机组成与系统结构》课程参考教材:Computer Organization and Design(fourth edition).pdf
- 《计算机组成与系统结构》课程参考教材:Computer Systems_A Programmer's Perspective-Pearson(THIRD EDITION,2015).pdf
- 《计算机组成与系统结构》课程参考教材:机械工业出版社《计算机组成与设计:硬件、软件接口》PDF电子书(中文第4版).pdf
- 上海交通大学:《计算机通讯与网络》教学资源(PPT课件)Chapter 1 roadmap.ppt
- 上海交通大学:《计算机通讯与网络》教学资源(PPT课件)Chapter 2 Application Layer.ppt
- 上海交通大学:《计算机通讯与网络》教学资源(PPT课件)Chapter 3 Transport Layer.ppt
- 上海交通大学:《计算机通讯与网络》教学资源_课程教学大纲.doc
- 上海交通大学:《面向对象分析与设计 Object Oriented Analysis and Design》课程教学资源(PPT课件讲稿)15 Class Design.ppt
- 上海交通大学:《面向对象分析与设计 Object Oriented Analysis and Design》课程教学资源(讲稿)绪论(主讲:李芳).pdf
- 上海交通大学:《面向对象分析与设计 Object Oriented Analysis and Design》课程教学资源(讲稿)软件开发过程.pdf
- 《程序设计思想与方法》课程教学资源(课程教材)How to Think Like a Computer Scientist(Learning with Python).pdf
- 《程序设计思想与方法》课程教学资源(课程教材)Python Programming:An Introduction to Computer Science.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(课程教材)简明Python教程(PDF版).pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT01 绪论.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT02 程序构件.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT03 数值计算.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT04 字符串计算.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT05 面向对象与图形编程.ppt