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)

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Lecture 10: Heap Management CS 540 GMU Spring 2009.
Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free.
KERNEL MEMORY ALLOCATION Unix Internals, Uresh Vahalia Sowmya Ponugoti CMSC 691X.
1. Dynamic Memory Allocation. Dynamic Memory Allocation Programmers use dynamic memory allocators (such as malloc ) to acquire VM at run time.  For data.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Dynamic Memory Allocation I October 16, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies lecture-15.ppt “The course that.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
1 Dynamic Memory Allocation: Basic Concepts Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaro.
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
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 Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation I Nov 5, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies class21.ppt “The course that gives.
Dynamic Memory Allocation I May 21, 2008 Topics Simple explicit allocators Data structures Mechanisms Policies.
1 Optimizing Malloc and Free Professor Jennifer Rexford
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Dynamic Memory Allocation I November 1, 2006 Topics Simple explicit allocators Data structures Mechanisms Policies class18.ppt “The course that.
Uniprocessor Garbage Collection Techniques Paul R. Wilson.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
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.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
1 Dynamic Memory Allocation: Basic Concepts. 2 Today Basic concepts Implicit free lists.
Writing You Own malloc() March 29, 2003 Topics Explicit Allocation Data structures Mechanisms Policies class19.ppt “The course that gives CMU its.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Memory Management -Memory allocation -Garbage collection.
Dynamic Memory Allocation II
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation: Basic Concepts :
Dynamic Memory Allocation April 9, 2002 Topics Simple explicit allocators Data structures Mechanisms Policies Reading: 10.9 Problems: and class21.ppt.
University of Washington Today More memory allocation!
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts Instructors: Adapted from CMU course
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
University of Washington Implementation Issues How do we know how much memory to free given just a pointer? How do we keep track of the free blocks? How.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
Memory Management I: Dynamic Storage Allocation Oct 8, 1998 Topics User-level view Policies Mechanisms class14.ppt Introduction to Computer Systems.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Chapter 17 Free-Space Management
Instructor: Phil Gibbons
Section 10: Memory Allocation Topics
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation I
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Memory Management I: Dynamic Storage Allocation Oct 7, 1999
Dynamic Memory Allocation
Memory Management Memory Areas and their use Memory Manager Tasks:
Dynamic Memory Allocation I October 28, 2003
Dynamic Memory Allocation: Basic Concepts CS220: Computer Systems II
Optimizing Malloc and Free
Memory Management I: Dynamic Storage Allocation March 2, 2000
User-Level Dynamic Memory Allocation: Malloc and Free
Dynamic Memory Allocation I
CS703 - Advanced Operating Systems
Dynamic Memory Allocation
Dynamic Memory Allocation I
Dynamic Memory Allocation: Basic Concepts /18-213/14-513/15-513: Introduction to Computer Systems 19th Lecture, October 30, 2018.
Operating System Chapter 7. Memory Management
Dynamic Memory Allocation November 2, 2000
Dynamic Memory Allocation: Basic Concepts CSCI 380: Operating Systems
Dynamic Memory Allocation I
Memory Management Memory Areas and their use Memory Manager Tasks:
CS703 - Advanced Operating Systems
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Run-time environments
Presentation transcript:

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) Allocate p3 (40 k) Allocate p3 (40 k) Free p2 Free p2 Allocate p4 (40 k) Allocate p4 (40 k) Free p3 Free p3 Allocate p5 (60 k) Allocate p5 (60 k) Free p1 Free p1 Allocate p6 (30k) Allocate p6 (30k)

Memory Allocation Algorithms Design YOUR algorithm for allocation and deallocation of memory

Memory Management Dynamic (heap) Dynamic (heap) Significant issues Significant issues Significant execution time (16%) Significant execution time (16%) Memory performance not uniform Memory performance not uniform Allocation policies Allocation policies Bugs Bugs Dereference problems Dereference problems Memory leaks Memory leaks

Memory Allocation Strategies Memory Allocation Strategies Explicit vs. Implicit Memory Allocator Explicit vs. Implicit Memory Allocator General purpose vs. custom allocator General purpose vs. custom allocator Software vs. hardware Software vs. hardware

Allocation Examples p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2)

Goals of Good malloc/free Good execution-time performance Good execution-time performance Good space utilization Good space utilization Good locality properties Good locality properties

Fragmentation Fragmentation Poor memory utilization --- fragmentation Poor memory utilization --- fragmentation Internal – overhead associated with a block of memory Internal – overhead associated with a block of memory External – have enough blocks of memory for a request, but not contiguous External – have enough blocks of memory for a request, but not contiguous Space in use Internal fragmentation

External Fragmentation p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(6) External fragmentation depends on future requests; thus difficult to anticipate

Bidirectional Coalescing Bidirectional Coalescing Boundary tags [Knuth73] Boundary tags [Knuth73] Replicate size/allocated word at bottom of free blocks Replicate size/allocated word at bottom of free blocks Allows us to traverse the “list” backwards, but requires extra space Allows us to traverse the “list” backwards, but requires extra space Important and general technique! Important and general technique!

Boundary Tags Boundary Tags size 1 word Format of allocated and free blocks Application Memory (and padding?) a = 1: allocated block a = 0: free block size: total block size Application memory (allocated blocks only) a sizea Boundary tag (footer) Header

Your turn Using boundary tag data structure, define algorithms for: Using boundary tag data structure, define algorithms for: Allocation Allocation Free Free

Key Allocator Policies Key Allocator Policies Placement policy: Placement policy: First fit, next fit, best fit, etc. First fit, next fit, best fit, etc. Trades off lower throughput for less fragmentation Trades off lower throughput for less fragmentation Splitting policy: Splitting policy: When do we go ahead and split free blocks? When do we go ahead and split free blocks? How much internal fragmentation are we willing to tolerate? How much internal fragmentation are we willing to tolerate? Coalescing policy: Coalescing policy: Immediate coalescing: coalesce adjacent blocks each time free is called Immediate coalescing: coalesce adjacent blocks each time free is called Deferred coalescing: try to improve performance of free by deferring coalescing until needed. e.g., Deferred coalescing: try to improve performance of free by deferring coalescing until needed. e.g.,

Refinements Separate lists Separate lists Binary buddy Binary buddy Lea allocator Lea allocator Custom allocators Custom allocators

Lea Allocator An approximate best-fit allocator with different behavior based on object size An approximate best-fit allocator with different behavior based on object size Small Objects (<64 bytes) allocated by exact-size quicklists Small Objects (<64 bytes) allocated by exact-size quicklists Medium Objects (<128K) – coalesce quicklists Medium Objects (<128K) – coalesce quicklists Large Objects – allocate and free by mmap Large Objects – allocate and free by mmap The best allocator known The best allocator known

Why programmers use Custom Allocators? Improving runtime performance Improving runtime performance Reducing memory consumption Reducing memory consumption Improving software engineering (?) Improving software engineering (?)

Alternative Memory Management Alternative Memory Management Region (arenas) Region (arenas) Reserve memory blocks for program “parts” Reserve memory blocks for program “parts” Deallocate entire regions, not per allocation Deallocate entire regions, not per allocation Garbage collection Garbage collection Programmer allocates but doesn’t free Programmer allocates but doesn’t free “System” keeps track of memory “pointed to” locations, removes the rest “System” keeps track of memory “pointed to” locations, removes the rest Java Java

Why Garbage Collect at All? Safety Safety Memory leaks Memory leaks Continued use of freed pointers Continued use of freed pointers Simplicity Simplicity Correctness Correctness Programming ease Programming ease

The Two-Phase Abstraction 1. Detection 1. Detection 2. Reclamation 2. Reclamation

Liveness and Garbage There is a root set which is defined as live. There is a root set which is defined as live. Anything reachable from a live pointer is also live Anything reachable from a live pointer is also live Everything else is garbage Everything else is garbage

The Root Set The Root Set The Root Set Static global and module variables Static global and module variables Local Variables Local Variables Variables on any activation stack(s) Variables on any activation stack(s) Everyone else Everyone else Anything Reachable From a live value Anything Reachable From a live value

Reference Counting Each allocated chunk has reference count that shows how many locations point (refer) to this one. Each allocated chunk has reference count that shows how many locations point (refer) to this one. Advantages ??? Advantages ??? Disadvantages ??? Disadvantages ???

Mark-Sweep Collection Starting from the root set traverse all pointers via depth/breadth first search. Starting from the root set traverse all pointers via depth/breadth first search. Free everything that is not marked. Free everything that is not marked.