5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.

Slides:



Advertisements
Similar presentations
Memory Management Chapter FourteenModern Programming Languages, 2nd ed.1.
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
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: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
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
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.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
G Robert Grimm New York University Cool Pet Tricks with… …Virtual Memory.
Memory Management Professor Yihjia Tsai Tamkang University.
CS 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Memory Management Chapter 5 Mooly Sagiv
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run-Time Storage Organization
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Garbage collection (& Midterm Topics) David Walker COS 320.
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.
Garbage Collection Memory Management Garbage Collection –Language requirement –VM service –Performance issue in time and space.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
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.
Memory Management II: Dynamic Storage Allocation Mar 7, 2000 Topics Segregated free lists –Buddy system Garbage collection –Mark and Sweep –Copying –Reference.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
1 Advanced Memory Management Techniques  static vs. dynamic kernel memory allocation  resource map allocation  power-of-two free list allocation  buddy.
COMP3190: Principle of Programming Languages
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Heap storage & Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001.
Copyright © Genetic Computer School 2008 Computer Systems Architecture SA 7- 0 Lesson 7 Memory Management.
Memory Management -Memory allocation -Garbage collection.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Consider Starting with 160 k of memory do: Starting with 160 k of memory do: Allocate p1 (50 k) Allocate p1 (50 k) Allocate p2 (30 k) Allocate p2 (30 k)
Introduction to Garbage Collection. Garbage Collection It automatically reclaims memory occupied by objects that are no longer in use It frees the programmer.
CS 241 Discussion Section (2/9/2012). MP2 continued Implement malloc, free, calloc and realloc Reuse free memory – Sequential fit – Segregated fit.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
Eliminating External Fragmentation in a Non-Moving Garbage Collector for Java Author: Fridtjof Siebert, CASES 2000 Michael Sallas Object-Oriented Languages.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Object Lifetime and Pointers
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Chapter 12 Memory Management
Operating System Chapter 7. Memory Management
Dynamic Memory.
Heap storage & Garbage collection
Presentation made by Steponas Šipaila
Heap storage & Garbage collection
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.

Memory Management2 Introduction When a program is started, most operating systems allocate at least the following three memory segment for it: –The ‘code segment’ Read-only or execute only –The ‘stack segment’ With overflow and underflow detection mechanism Addressed by one or more stack pointers, automatic manipulation by machine instructions; –The ‘data segment’ A single contiguous stretch of memory locations, at the disposition of program for the purpose of storing data; Also called heap from the aspect of memory management

Memory Management3 Introduction It is the task of memory management to hand out and collect subsegments of the data segment such that –All subsegments fit inside the data segment and no memory location is ever part of more than one subsegment. Allocating memory in the heap is easy enough: –We keep a pointer to the first free location in the heap, –Allocate the request block from there, and –Bump the pointer to the next free location. In this chapter –Techniques for data allocation with explicit deallocation by the programmer, and –Techniques for data allocation with implicit deallocation.

Memory Management4 5.1 Data allocation with explicit allocation In most systems, basic memory allocation comes in the form of routine that –finds a block of unused memory of the requested size, –Marks it as used, and –Returns a pointer to the block. If no such block is available, the results varies –A null pointer may be returned, –And error routine may be called, or the program may be aborted. This is available in C as the routines void *malloc(size_t size) and free(*void *ptr)

Memory Management5 5.1 Data allocation with explicit allocation Considerations of the allocation and deallocation of two classes of data types are in great demand in compilers. –Linked lists and –Extensible arrays Some modern languages (notably the functional and logic languages, and Java) have automatic data allocation and deallocation mechanisms, but –in practice almost all compilers are written in a traditional language, such as C or C++. Data allocation and deallocation in these languages require considerable care, and experience has shown that it is very advantageous in compiler writing to organize memory management properly and systematically.

Memory Management Basic memory allocation

Memory Management7

8

9

Optimization for basic memory allocation Efficiency problem with the above implementation 1.Finding a suitable chunk in the free list requires linear search through the entire memory, which is unacceptable. 2.Coalescing is done in a separate phase, performed only when the usual linear search fails; this makes the performance of the algorithm irregular Solving the first problem –Chaining the free chunk in a linked list –Classifying the free chunks according to size and keeping a free list for each interval Solving the second problem –Coalescing can be done one the fly, during each call of Free if we have easy access to the chunk preceding the one being freed; –The one following it is already within easy reach, using the size of the chunk being freed.

Memory Management Linked lists Pages 403~404.

Memory Management Extensible arrays Pages 404~407.

Memory Management Data allocation with implicit allocation Implicit deallocation, or garbage collection, is the automatic reallocation of memory that is no longer in use by the application program. –Relieving the programmer from the error-prone task of reclaiming memory manually by using an explicit free() primitive. Correctly freeing blocks in handwritten code requires a considerable insight into the dynamics of the program. Errors hard to find and correct in freeing memory –Problems of dangling pointers –Hard to reproduce the errors

Memory Management Data allocation with implicit allocation Garbage collection is considered to be an important feature of modern programming systems that reduces programming efforts considerably. Examples of programming systems offering garbage collection –OO languages, like Java and Smalltalk, –Functional languages, like ML, Kaskell, login language, like Prolog, and –Scripting languages like awk and perl.

Memory Management Basic garbage collection algorithms The objective of garbage collection is to reclaim automatically the set of memory chunks that will no longer be used by the program, the garbage set. –Unattainable, because no automatic method can determine what the program is going to do Practical approximations for the garbage set – ‘the set of all chunks to which there are no pointers,” and –‘the set of all chunks that are not reachable from the non-heap-allocated program data.’ These approximations are safe because –No chunk in either of these sets can be in use in the program.

Memory Management Basic garbage collection algorithms The ‘no-pointers’ criterion leads to a technique called reference counting, and the ‘not-reachable’ criterion is exemplified by two techniques, mark scan and two-space copying. The three techniques are all different: –Reference counting directly identifies garbage chunks. Simple and reasonably efficient but requires all pointer actions to be monitored during program execution and may not recover all garbage chunks. –Mark and scan identifies reachable chunks and concludes that the rest is garbage. Reasonably efficient and not requires pointer monitoring. Quite complicated; the only one that will recover all available memory –Two-space copying is not concerned with garbage. Copying the reachable chunks from the ‘from-space’ memory region to ‘to-space’ region; the remaining space in to-space is a single free chunk. Very efficient, no pointer monitoring required, moderately complicated, waste half of the memory

Memory Management Basic garbage collection algorithms Memory fragmentation –Do memory compaction Varieties of garbage collection algorithms –One-shot –On-the-fly (also called incremental) –Concurrent

Memory Management18

Memory Management19

Memory Management20

Memory Management21

Memory Management22

Memory Management23

Memory Management24

Memory Management25

Memory Management26

Memory Management27

Memory Management28

Memory Management29