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.

Slides:



Advertisements
Similar presentations
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Advertisements

Dynamic Memory Management
C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
Garbage Collection CS Introduction to Operating Systems.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Garbage Collection Introduction and Overview Christian Schulte Excerpted from presentation by Christian Schulte Programming Systems Lab Universität des.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Increasing Memory Usage in Real-Time GC Tobias Ritzau and Peter Fritzson Department of Computer and Information Science Linköpings universitet
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management, Exceptions, From kernel to practical language.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Data Abstraction and State Abstract Data Types (3.7) State/Data Abstraction(6.1, 6.3, 6.4.1,
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Garbage collection (& Midterm Topics) David Walker COS 320.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management, Exceptions, From kernel to practical language.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Non-declarative needs (VRH 3.8) Program design in the.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Declarativeness, iterative computation (VRH 3.1-2) Higher-order.
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.
Memory Management Last Update: July 31, 2014 Memory Management1.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Compiler Construction Lecture 17 Mapping Variables to Memory.
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.
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.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC Lecture 4 Procedures, last call optimization.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Copyright © Genetic Computer School 2008 Computer Systems Architecture SA 7- 0 Lesson 7 Memory Management.
1 Lecture 22: Object Lifetime and Garbage Collection (Section 10.3 & 7.7) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by.
Introduction to Garbage Collection. Garbage Collection It automatically reclaims memory occupied by objects that are no longer in use It frees the programmer.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
An Efficient, Incremental, Automatic Garbage Collector P. Deutsch and D. Bobrow Ivan JibajaCS 395T.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
GARBAGE COLLECTION Student: Jack Chang. Introduction Manual memory management Memory bugs Automatic memory management We know... A program can only use.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Higher-Order Programming: Iterative computation (CTM Section 3
Object Lifetime and Pointers
Garbage Collection What is garbage and how can we deal with it?
Core Java Garbage Collection LEVEL – PRACTITIONER.
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Memory Management 6/20/ :27 PM
Higher-Order Programming: Iterative computation (CTM Section 3
Concepts of programming languages
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
Storage.
Object-Oriented Programming Encapsulation Control/Visibility (VRH 7. 3
Closure Representations in Higher-Order Programming Languages
Higher-Order Programming: Closures, procedural abstraction, genericity, instantiation, embedding. Control abstractions: iterate, map, reduce, fold, filter.
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
Introduction to Programming Concepts (VRH )
Declarative Computation Model Single assignment store (VRH 2
Dynamic Memory.
Automating Memory Management
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

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 Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy2 Memory Management Semantic stack and store sizes during computation –analysis using operational semantics –recursion used for looping efficient because of last call optimization –memory life cycle –garbage collection

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy3 Last call optimization Consider the following procedure proc {Loop10 I} if I ==10 then skip else {Browse I} {Loop10 I+1} end end This procedure does not increase the size of the STACK It behaves like a looping construct Recursive call is the last call

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy4 Last call optimization proc {Loop10 I} if I ==10 then skip else {Browse I} {Loop10 I+1} end end ST: [ ({Loop10 0}, E 0 ) ] ST: [({Browse I}, {I  i 0,...}) ({Loop10 I+1}, {I  i 0,...}) ]  : {i 0 =0,...} ST: [({Loop10 I+1}, {I  i 0,...}) ]  : {i 0 =0,...} ST: [({Browse I}, {I  i 1,...}) ({Loop10 I+1}, {I  i 1,...}) ]  : {i 0 =0, i 1 =1,...}

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy5 Stack and Store Size proc {Loop10 I} if I ==10 then skip else {Browse I} {Loop10 I+1} end end ST: [({Browse I}, {I  i k,...}) ({Loop10 I+1}, {I  i k,...}) ]  : {i 0 =0, i 1 =1,..., i k-1 =k-1, i k =k,... } The semantic stack size is bounded by a constant. But the store size keeps increasing with the computation. Notice that at (k+1) th recursive call, we only need i k If we can keep the store size constant, we can run indefinitely with a constant memory size.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy6 Garbage collection proc {Loop10 I} if I ==10 then skip else {Browse I} {Loop10 I+1} end end ST: [({Browse I}, {I  i k,...}) ({Loop10 I+1}, {I  i k,...}) ]  : {i 0 =0, i 1 =1,..., i k-i =k-1, i k =k,... } Garbage collection is an algorithm (a task) that removes from memory (store) all cells that are not accessible from the stack ST: [({Browse I}, {I  i k,...}) ({Loop10 I+1}, {I  i k,...}) ]  : { i k =k,... }

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy7 The memory life cycle Active memory is what the program needs to continue execution (semantic stack + reachable part of store) Memory that is no longer needed is of two kinds: –Can be immediately deallocated (i.e., semantic stack) –Simply becomes inactive (i.e., store) Reclaiming inactive memory is the hardest part of memory management –Garbage collection is automatic reclaiming ActiveFree Inactive Allocate/ deallocate Become inactive (program execution) Reclaim (garbage collection)

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy8 Garbage Collection Lower-level languages (C, C++) do not have automatic garbage collection. Manual memory management can be more efficient but it is also more error-prone, e.g.: –Dangling references Reclaiming reachable memory blocks –Memory leaks Not reclaiming unreachable memory blocks Higher-level languages (Erlang, Java, Lisp, Smalltalk) typically have automatic garbage collection. Modern algorithms are efficient enough---minimal memory and time penalties.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy9 Garbage Collection Algorithms Reference Counting algorithms –Keep track of number of references to memory blocks –When count is 0, memory block is reclaimed. –Cannot collect cycles of garbage. Mark-and-Sweep algorithms –Phase 1: Determine active memory Following pointers (in Oz, referenced store variables) from a root set (in Oz, the semantic stack). –Phase 2: Compact memory in one contiguous region. Everything outside this region is free. –Generally must briefly pause the application memory mutation while collecting.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy10 Avoiding memory leaks Consider the following function fun {Sum X L1 L} case L1 of Y|L2 then {Sum X+Y L2 L} else X end end local L in L= [1 2 3 … ] {Sum 0 L L} end Since it keeps a pointer to the original list L, L will stay in memory during the whole execution of Sum.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy11 Avoiding memory leaks Consider the following function fun {Sum X L1} case L1 of Y|L2 then {Sum X+Y L2} else X end end local L in L= [1 2 3 … ] {Sum 0 L} end Here, the reference to L is lost immediately and its space can be collected as the function executes.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy12 Managing external references External resources are data structures outside the current O.S. process. There can be pointers from internal data structures to external resources, e.g. –An open file in a file system –A graphic entity in a graphics display –If the internal data structure is reclaimed, then the external resource needs to be cleaned up (e.g., remove graphical entity, close file) There can be pointers from external resources to internal data structures, e.g. –A database server –A web service –If the internal data structure is reachable from the outside, it should not be reclaimed.

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy13 Local Mozart Garbage Collector Copying dual-space algorithm Advantage : Execution time is proportional to the active memory size, not total memory size. Disadvantage : Half of the total memory is unusable at any given time

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy14 Exercises 55.What do you expect to happen if you try to execute the following statement? Try to answer without actually executing it! local T = tree(key:A left:B right:C value:D) in A = 1 B = 2 C = 3 D = 4 end 56.CTM Exercise (page 109). 57.Any realistic computer system has a memory cache for fast access to frequently used data. Can you think of any issues with garbage collection in a system that has a memory cache?