上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_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
- 上海交通大学:《编译原理》教学资源_教学资料_第五周讲义_Type Checking.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
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_CS308 Compiler Theory.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT15 并发与多线程.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT14 Python GUI工具包:Tkinter.ppt
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Run-Time Environments.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第四周讲义_Syntax-Directed Translation.pdf
- 上海交通大学:《计算机辅助设计》教学资源_Product Visualization.doc
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 1:Introduction, calculus review and computer representation of numbers.pdf
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 2:Solution of nonlinear equations.pdf
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 3:Polynomial Interpolation.pdf
- 上海交通大学:《科学计算》课程教学资源(英文讲义)Lecture Note 4:Numerical differentiation and integration.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第一次上机_第一次上机内容_10.18.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第一次上机_第一次上机内容_10.18.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第三次上机_python第三次上机.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第三次上机_Python第三次上机解析-update.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第五次上机_第五次上机.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源(上机课)第四次上机_Python第四次上机题目.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源_作业_第一次作业内容要求.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源_作业_第一次作业内容要求.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_参考教材_HowToThink-Python.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_参考教材_参考教材-2002版-PythonProgrammingBook.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_参考教材_参考教材-python-programming.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源_往年试卷_CT2012-A卷-final 2_参考答案.doc
- 上海交通大学:《程序设计思想与方法》课程教学资源_往年试卷_CT2012-A卷-final.doc