Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Dynamic Memory Management
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Run-Time Storage Organization
Run time vs. Compile time
Catriel Beeri Pls/Winter 2004/5 environment 68  Some details of implementation As part of / extension of type-checking: Each declaration d(x) associated.
Linked lists and memory allocation Prof. Noah Snavely CS1114
Run-time Environment and Program Organization
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.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
Programming Languages and Paradigms Object-Oriented Programming.
COP4020 Programming Languages
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
SPL/2010 StackVsHeap. SPL/2010 Objectives ● Memory management ● central shared resource in multiprocessing RTE ● memory models that are used in Java and.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Object-Oriented Programming in C++
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
COMP3190: Principle of Programming Languages
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Object-Oriented Programming Chapter Chapter
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
ISBN Chapter 12 Support for Object-Oriented Programming.
Storage Allocation Mechanisms
Data Types In Text: Chapter 6.
CS 326 Programming Languages, Concepts and Implementation
Storage Management.
Run-time organization
CS 153: Concepts of Compiler Design November 28 Class Meeting
Storage.
Closure Representations in Higher-Order Programming Languages
Topic 3-b Run-Time Environment
Introduction to Data Structure
Dynamic Memory.
Java Programming Language
Course Overview PART I: overview material PART II: inside a compiler
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Run-time environments
Course Overview PART I: overview material PART II: inside a compiler
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review

Runtime Organization (Chapter 6) 2 Heap Storage Allocation Data Representation: how to represent values of the source language on the target machine Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) Stack Storage Allocation: How to organize storage for variables (considering lifetimes of global and local variables) Routines: How to implement procedures, functions (and how to pass their parameters and return values) Heap Storage Allocation: How to organize storage for variables (considering lifetimes of heap variables) Object Orientation: Runtime organization for OO languages (how to handle classes, objects, methods)

Runtime Organization (Chapter 6) 3 Heap Storage Allocation Recap: We have seen 2 storage allocation models so far: static allocation global variables exist “forever” stack allocation local variables and arguments for procedures and functions lifetime is from when procedure called until procedure returns Next: Heap allocation. Memory allocation is explicitly under the programmer’s control. (C: malloc, C ++ / Java / Pascal: new operation) Example: (Java) int[ ] nums = new int[sizeExpression];

Runtime Organization (Chapter 6) 4 Heap Storage Allocation Explicit versus Implicit Deallocation Examples: Implicit: Java, Scheme Explicit: Pascal, C, C ++ To free heap memory a specific operation must be called. Pascal ==> dispose C ==> free C ++ ==> delete 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”.

Runtime Organization (Chapter 6) 5 Heap Storage Allocation Memory managers (both implicit as well as explicit) have been studied extensively by researchers. => Algorithms for fast allocation / deallocation of memory and low memory overhead for memory management administration. => There are many complicated, sophisticated, and interesting algorithms. We could dedicate an entire course to this topic alone! However, we only have time to look at memory management superficially. We will look at the kinds of algorithms/issues associated with explicit and implicit memory management.

Runtime Organization (Chapter 6) 6 Where to put the heap? The heap is an area of memory which is dynamically allocated. Like a stack, it may grow and shrink during runtime. (Unlike a stack, a heap is not LIFO => more complicated to manage.) In a typical programming language implementation, we will have both heap-allocated and stack-allocated memory that co-exist. Q: How do we allocate memory for both? A simple approach is to divide the available memory at the start of the program into two areas: stack and heap. Another question then arises: - How do we decide what portion to allocate for stack vs. heap? - Issue: if either one of the areas is full, then even though memory is available (in the other area) we will get out-of-memory errors. Q: Isn’t there a better way?

Runtime Organization (Chapter 6) 7 Where to put the heap? Q: Isn’t there a better way? A: Yes, there is an often used “trick”: let both stack and heap share the same memory area, but grow towards each other from opposite ends! ST SB HB HT Stack memory area Heap memory area Stack grows downward Heap can expand upward

Runtime Organization (Chapter 6) 8 How to keep track of free memory? Stack is LIFO allocation => ST moves up/down. Everything above ST is in use/allocated. Below ST is free memory. This part is easy! Heap is not LIFO, how to manage free space in the “middle” of the heap? HB HT Allocated ST SB Free Mixture of Allocated and Free space How to reuse?

Runtime Organization (Chapter 6) 9 How to keep track of free memory? How to manage free space in the “middle” of the heap? HB HT => Keep track of free blocks in a data structure: the “free list”. For example use a linked list pointing to the free blocks. Free Next freelist Free Next A freelist! Good idea! But where do we find the memory to store this data structure? A freelist! Good idea! But where do we find the memory to store this data structure?

Runtime Organization (Chapter 6) 10 How to keep track of free memory? HB HT Q: Where do we find the memory to store a freelist data structure? A: Since the free blocks are not used for anything by the program => memory manager can use them for storing the freelist itself. HF free block size next free

Runtime Organization (Chapter 6) 11 Simple Explicit Memory Manager Algorithm We next consider a memory manager that is intended for a programming language with explicit deallocation of memory. Q: What operations does the memory management library provide? Pointer malloc(int size); Asks memory manager to find and allocate a block of specified size. Returns a pointer to beginning of that block. void free(Pointer toFree, int size); Called when a block toFree is released by the program and returned to the memory manager.

Runtime Organization (Chapter 6) 12 Simple Explicit Memory Manager Algorithm Pointer malloc(int size) { block = search the freelist until a block of at least size is found. if (block is found) and (block.size == size) delete block from free list. return pointer to start of block. else if (block found) and (block.size > size) delete block from free list. insert remainder of block into free list. return pointer to start of block. else // no block found try to expand heap by size. if (expansion was successful) then return HT; else generate out-of-memory error. }

Runtime Organization (Chapter 6) 13 Simple Explicit Memory Manager Algorithm Pointer free(Pointer toFree, int size) { insert block(toFree,size) into the freelist } Pointer free(Pointer toFree, int size) { toFree[0] = size; // size at offset 0 toFree[1] = HF; // next pointer at offset 1 HF = toFree; } This algorithm is rather simplistic and has some rather big problems. Q: What are those problems? How could we fix them? A bit more detail:

Runtime Organization (Chapter 6) 14 Fragmentation One of the biggest problems with the simplistic algorithm is memory fragmentation. blocks are split to create smaller blocks to fit the requested size. blocks are never merged. => Blocks will continually get smaller and smaller. Better algorithm merges released blocks with neighboring blocks that are free. => less fragmentation Q1: Analyze impact on the performance (time) for allocating and releasing blocks. Q2: Does this solve the fragmentation problem completely?

Runtime Organization (Chapter 6) 15 Heap Compaction To fight fragmentation, some memory management algorithms perform “heap compaction” once in a while. HB HT HF a b c HB HT a b d d c beforeafter Q: Can compaction be done for all programming language implementations?

Runtime Organization (Chapter 6) 16 Complication with Explicit Memory Allocation “Problems” with explicit memory allocation algorithms: Two common, nasty kinds of bugs in programs using explicit memory management are Memory leaks (garbage) Dangling pointers Q1: What are memory leaks and dangling pointers? Q2: Is it true that there can be no memory leaks in a garbage- collected language? Q3: Is it true that there can be no dangling pointers in a garbage- collected language?

Runtime Organization (Chapter 6) 17 Automatic Storage Deallocation (Garbage Collection) Everybody probably knows what a garbage collector is. But here are two statements to make you think about what a garbage collector really does. 1. A garbage collector provides the “illusion of infinite memory”. 2.A garbage collector predicts the future: it runs when “almost” about to run out of memory. Next we consider one of the most often used garbage collection algorithms (if time permits).

Runtime Organization (Chapter 6) 18 Mark and Sweep Garbage Collection HT e c a HB SB ST b d before gc HT e c a SB ST b d HB mark as free phase X X X X X

Runtime Organization (Chapter 6) 19 Mark and Sweep Garbage Collection HT e c a SB ST b d HB mark as free phase X X X X X HT e c a SB ST b d HB X X X X X X X X mark reachable SB ST HB HT e b d X X X X X X collect free

Runtime Organization (Chapter 6) 20 Mark and Sweep Garbage Collection Algorithm pseudo code: void garbageCollect( ) { mark all heap variables as free for each frame in the stack scan(frame) for each heapvar that is still marked as free add heapvar to freelist } void scan(region) { for each pointer p in region if p points to region marked as free then mark region at p as reachable scan(region at p ) } Q: This algorithm is recursive. What do you think about that?

Runtime Organization (Chapter 6) 21 Object Orientation Data Representation: how to represent values of the source language on the target machine Expression Evaluation: How to organize computing the values of expressions (taking care of intermediate results) Stack Storage Allocation: How to organize storage for variables (considering lifetimes of global and local variables) Routines: How to implement procedures, functions (and how to pass their parameters and return values) Heap Storage Allocation: How to organize storage for variables (considering lifetimes of heap variables) Object Orientation: Runtime organization for OO languages (how to handle classes, objects, methods)

Runtime Organization (Chapter 6) 22 Runtime Organization for OO Languages What is this about? How to represent/implement object-oriented constructs such as objects, classes, methods, instance variables, and method calls. Some definitions and relationships for these concepts: An object is a group of instance variables to which a group of instance methods is attached. An instance variable is a named component of a particular object. An instance method is a named operation that is attached to a particular object and that is able to access that object’s instance variables. A class is a family of objects with similar instance variables and identical methods.

Runtime Organization (Chapter 6) 23 Runtime Organization for OO Languages 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/functions. => Implementation of methods is similar to routines. But there are differences: Objects have methods as well as instance variables, whereas 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).

Runtime Organization (Chapter 6) 24 Example: Representation of a simple Java object Example 1: a simple Java object (no inheritance) class Point { int x, y; public Point(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public float area( ) { return 0.0; } public float dist(Point other) {... } } class Point { int x, y; public Point(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public float area( ) { return 0.0; } public float dist(Point other) {... } } (1) (2) (3) (4)

Runtime Organization (Chapter 6) 25 Example: Representation of a simple Java object Example: a simple Java object (no inheritance) Point class Point move area dist constructor(1) method(2) method(3) method(4) Point p = new Point(2,3); Point q = new Point(0,0); pqpq class xyxy 2323 xyxy 0000 new allocates an object in the heap

Runtime Organization (Chapter 6) 26 Inheritance Example 2: Points and other “shapes” abstract class Shape { int x, y; // “origin” of the shape public Shape(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public abstract float area( ); public float dist(Shape other) {... } } abstract class Shape { int x, y; // “origin” of the shape public Shape(int x, int y) { this.x=x; this.y=y; } public void move(int dx, int dy) { x=x+dx; y=y+dy; } public abstract float area( ); public float dist(Shape other) {... } } (S1) (S2) (S3)

Runtime Organization (Chapter 6) 27 class Point extends Shape { public Point(int x, int y) { super(x,y); } public float area( ) { return 0.0; } } class Point extends Shape { public Point(int x, int y) { super(x,y); } public float area( ) { return 0.0; } } Inheritance Example 2: Points and other “shapes” (P1) (P2)

Runtime Organization (Chapter 6) 28 Inheritance Example 2: Points and other “shapes” class Circle extends Shape { int r; public Circle(int x, int y, int r) { super(x,y); this.r = r; } public int radius( ) { return r; } public float area( ) { return 3.14 * r * r; } class Circle extends Shape { int r; public Circle(int x, int y, int r) { super(x,y); this.r = r; } public int radius( ) { return r; } public float area( ) { return 3.14 * r * r; } (C1) (C3) (C2)

Runtime Organization (Chapter 6) 29 Inheritance Shape class Shape move area dist constru(S1) method(S2) method(S3) Circle class Circle move area dist constru(C1) method(S2) method(C3) method(S3) radius method(C2) Inherited from shape Point class Point move area dist constru(P1) method(S2) method(S3) method(P2) Note the similar layout of each class object. Q: Why is that important? Q: Why don’t we need a pointer to the super-class in a class object?

Runtime Organization (Chapter 6) 30 Inheritance Shape[ ] s = new Shape[2]; s[0] = new Point(2,3); s[1] = new Circle(4,5,6); s class xyrxyr xyxy 2323 Point class Circle class Note the similar layout between Point and Circle objects! s[0] s[1] s[0].x =...; s[1].y =...; float sumAreas = s[0].area( ) +s[1].area( );