1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Run-Time Storage Organization
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Basic Semantics Associating meaning with language entities.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
C++ Memory Overview 4 major memory segments Key differences from Java
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Object-Oriented Programming in C++
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Memory Management.
Object Lifetime and Pointers
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Garbage Collection What is garbage and how can we deal with it?
Chapter 10 : Implementing Subprograms
Dynamic Storage Allocation
Data Types In Text: Chapter 6.
Run-Time Environments Chapter 7
Storage 18-May-18.
CS 326 Programming Languages, Concepts and Implementation
CSE 374 Programming Concepts & Tools
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
Concepts of programming languages
Run-Time Storage Organization
Automatic Memory Management
Java Review: Reference Types
Pointers and Dynamic Variables
Dynamic Memory Allocation
Simulated Pointers.
Simulated Pointers.
Memory Management Kathryn McKinley.
Linked Lists.
Closure Representations in Higher-Order Programming Languages
UNIT V Run Time Environments.
Run Time Environments 薛智文
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Classes and Objects Object Creation
Reference Counting.
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
CMSC 202 Constructors Version 9/10.
Presentation transcript:

1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi

Outline 1. Introduction to Garbage Collection 1. Design Goals for Garbage Collectors 2. Reachability 3. Reference Counting Garbage Collectors 2. Summary 2

Run-Time Environments Lecture:

Introduction to Garbage Collection Data that cannot be referenced is generally known as garbage Many high-level programming languages remove the burden of manual memory management from the programmer by offering automatic garbage collection Garbage collector deallocates unreachable data The notion of an object being “reachable” is perhaps intuitive so we need precise rules to declare an object as reachable or unreachable 4

Introduction to Garbage Collection (Continue…) Garbage collection dates back to the initial implementation of Lisp in Languages that offer garbage collection include Java, Perl, ML, Modula-3, Prolog, and Smaltalk Many language cannot offer garbage collection due to their method of managing types 5

Design Goals for Garbage Collectors Garbage collection is the reclamation of chunks of storage holding objects that can no longer be accessed by a program We need to assume that objects have a type that can be determined by garbage collector at run time The type tells how large the object is and which components of the object contain references (pointers) to other objects We also assume that references to objects are always to the address of the beginning of the object, never pointers to places within the object 6

Design Goals for Garbage Collectors (Continue…) If references point to the beginning of the address, it makes sure that all references have same values A user program, which we shall refer to as mutator, modifies the collection of objects on heap The mutator creates objects by acquiring space from the memory manager and mutator may intrduce or drop references to existing objects Objects become garbage when mutator program cannot reach them The garbage collector finds such unreachable objects and reclaim their space 7

Design Goals for Garbage Collectors (Continue…) A Basic Requirement: Type Safety Not all languages are good candidates for automatic garbage collection For a garbage collector to work, it must be able to tell whether any given data element or its component is, or could be used as, pointer to a chunk of allocated memory space A language in which the type of any data component can be determined is said to be type safe  There are type-safe languages like ML, for which we can determine types at compile time 8

Design Goals for Garbage Collectors (Continue…) There are other type-safe languages, like Java, whose types cannot be determined at compile time, but can be determined at run-time  These languages are dynamically typed languages If a language neither statically nor dynamically type safe, then it is said to be unsafe Unsafe languages e.g. C and C++, are bad candidates for automatic garbage collection because memory addresses can be manipulated  Arbitrary arithmetic operations can be performed on pointers to form new pointers 9

Design Goals for Garbage Collectors (Continue…) In C and C++, theoretically a program could refer to any location in memory at any time Consequently, no memory location can be considered as inaccessible, and no storage can ever be reclaimed safely Despite the inherent problem in C/C++, an unsound garbage collector has been developed for these languages also and experiments have shown good results about its working 10

Design Goals for Garbage Collectors (Continue…) Performance Metrics Garbage collection is often so expensive that, although it was invented decades ago & absolutely prevents memory leaks, it has yet to be adopted by many mainstream programming languages Many different approaches have been proposed but still there is not one clearly best garbage-collection algorithm There are many performance metrics that should be considered while designing a garbage collector 11

Design Goals for Garbage Collectors (Continue…) Overall Execution Time  Garbage collection can be very slow so it is important that it not significantly increase total run time of an application Space Usage  It is important that garbage collection avoids fragmentation and makes good use of the available memory Pause Time  Garbage collectors kicks in without warning and they are notorious of causing programs – the mutators – to pause suddenly for extremely long time. So it is desirable that the pause time is minimized. For real time applications we may avoid running garbage collection or restrict max pause time 12

Design Goals for Garbage Collectors (Continue…) Program Locality  We cannot evaluate speed of a garbage collector solely by its running time. The garbage collector controls the placement of data and thus influences the data locality of the mutator program Some of these design goals conflict with one another, and tradeoffs must be made carefully by considering how programs typically behave Also objects of different characteristics may have different treatments so garbage collector should handle them accordingly 13

Reachability We refer to all the data that can be accessed directly by a program without having to dereference any pointer, as the root set  For example, in Java the root set of a program consists of all the static field members and all the variables on its stack A program can reach any member of its root set at any time Recursively any object with a reference that is stored in the field members or array elements of any reachable object is itself reachable 14

Reachability (Continue…) The reachability becomes a bit more complex when the program has been optimized by the compiler The set of reachable objects changes as a program executes It grows as new objects created and shrinks as objects become unreachable It is important to remember that once an object becomes reachable, it cannot become reachable again There are four basic operations that mutator performs to change the set of reachable objects 15

Reachability (Continue…) 1. Object Allocations  These are performed by memory manager, which returns a reference to each newly allocated chunk of memory. This operation adds members to the set of reachable objects 2. Parameter Passing & Return Values  References to objects are passed from the actual input parameter to the corresponding formal parameter, and from the returned result back to the callee. Objects pointed to these references remain reachable 16

Reachability (Continue…) 3. Reference Assignment  Assignments of the form u = v, where u and v are references, have two effects.  First, u is now reference to the object referred to by v. As long as the u is reachable, the object it refers to is surely reachable  Second, the original reference in u is lost. If this reference is the last to some reachable object, then that object becomes unreachable.  Any time an object becomes unreachable, all objects that are reachable only through references contained in that object also become unreachable 17

Reachability (Continue…) 4. Procedure Returns  As a procedure exits, the frame holding its local variables is popped off the stack. If the frame holds the only reachable reference to any object, that object becomes unreachable. Again, if the now unreachable objects hold the only reference to other objects, they too become unreachable, and so on. 18

Reachability (Continue…) There are two basic ways to find unreachable objects First, we catch the transitions as reachable objects turn unreachable  Reference counting is a well-known approximation to this approach Second, we periodically locate all the reachable objects and then infer that all the other objects are unreachable  A trace-based garbage collector is built on this approach 19

Reference Counting Garbage Collectors This garbage collector is based on technique reference counting, which identifies garbage as an object changes from being reachable to unreachable, the object can be deleted when the count drops to zero With a reference-counting garbage collector, every object must have a field for the reference count Reference count can be maintained as follows 1. Object Allocation  The reference count to the new object is set to 1 20

Reference Counting Garbage Collectors (Continue…) 2. Parameter Passing  The reference count of each object passed into a parameter is incremented 3. Reference Assignments  For statement u = v, where u and v are references, the reference count of the object referred to by v goes up by one and the count of the old object referred to by u goes down by one 21

Reference Counting Garbage Collectors (Continue…) 4. Procedure Returns  As the procedure exits, all the references held by the local variables of that procedure activation records must also be decremented. If several local variables hold the reference to same object, that object’s count must be decremented once for each such reference 5. Transitive Loss of Reachability  Whenever reference count of an object becomes zero, we must also decrement the count of each object pointed to by a reference within the object 22

Reference Counting Garbage Collectors (Continue…) Reference counting has two main disadvantages 1. It cannot collect unreachable cyclic data structures 2. This approach is expensive Cyclic data structures are plausible  Data structures often point back to their parent nodes  Data structures point to each other as cross reference 23

Reference Counting Garbage Collectors (Continue…) Consider the figure below;  There are three objects with references to each other but no reference from anywhere else  If none of these are part of root set then they are all garbage but since their reference count is greater than 0, the reference counting garbage collector will not delete them 24

Reference Counting Garbage Collectors (Continue…) The overhead of reference counting is high because additional operations are introduced with each reference assignment, and at procedure entries and exits This overhead is proportional to the amount of computation in the program The advantage of reference counting on the other hand is that garbage collection is done in an incremental fashion Even though the overhead may be large, operations are spread throughout the mutator’s computation 25

Reference Counting Garbage Collectors (Continue…) Although removing one reference may render a large number of objects unreachable, the operations of recursively modifying reference counts can easily be deferred and performed piecemeal across time Reference counting is particularly attractive algorithm when timing deadlines must be met as well as for interactive applications, where long sudden pauses are unacceptable 26

27 Summary Any Questions?