Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

Similar documents
Dynamic Memory Allocation I Nov 5, 2002

Dynamic Memory Allocation I

Dynamic Memory Allocation. Basic Concepts. Computer Organization 4/3/2012. CSC252 - Spring The malloc Package. Kai Shen

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Dynamic Memory Allocation

CS 33. Storage Allocation. CS33 Intro to Computer Systems XXVII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dynamic Memory Alloca/on: Basic Concepts

Dynamic Memory Allocation

Dynamic Memory Alloca/on: Basic Concepts

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

Dynamic Memory Allocation

CS 153 Design of Operating Systems

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Allocation II October 22, 2008

Foundations of Computer Systems

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Today. Dynamic Memory Allocation: Advanced Concepts. Explicit Free Lists. Keeping Track of Free Blocks. Allocating From Explicit Free Lists

Binghamton University Dynamic Memory Alloca/on

Dynamic Memory Alloca/on: Advanced Concepts

Dynamic Memory Allocation: Advanced Concepts

Internal Fragmentation

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Allocation

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

ANITA S SUPER AWESOME RECITATION SLIDES

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Dynamic Memory Allocation. Zhaoguo Wang

Dynamic Memory Management

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Dynamic Memory Management! Goals of this Lecture!

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Memory Allocation III CSE 351 Spring (original source unknown)

Run-time Environments -Part 3

Run-time Environments - 3

Foundations of Computer Systems

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Dynamic Memory Management

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Memory management. Johan Montelius KTH

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Carnegie Mellon. Malloc Boot Camp. Stan, Nikhil, Kim

Recitation #12 Malloc Lab - Part 2. November 14th, 2017

Optimizing Dynamic Memory Management

Engine Support System. asyrani.com

10.1. CS356 Unit 10. Memory Allocation & Heap Management

CS 153 Design of Operating Systems

CS61C : Machine Structures

CS61C : Machine Structures

CS61 Section Notes. Section 5 (Fall 2011) Topics to be covered Common Memory Errors Dynamic Memory Allocation Assignment 3: Malloc

Memory Allocation III

Memory Allocation III

Dynamic Memory Alloca/on: Advanced Concepts

Lectures 13 & 14. memory management

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

Memory Allocation III

Memory Management. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Lecture 13: Garbage Collection

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

ECE 598 Advanced Operating Systems Lecture 10

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

CMSC 313 Spring 2010 Exam 3 May 17, 2010

In Java we have the keyword null, which is the value of an uninitialized reference type

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Heap Management. Heap Allocation

Memory Management Basics

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CSCI 237 Sample Final Exam

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

CS 2461: Computer Architecture I: Heap: Dynamic Memory Allocation Data Structures. Recap. Variables and Structures

Memory Allocation III

ECE 598 Advanced Operating Systems Lecture 12

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Operating System Labs. Yuanbin Wu

CS61C Midterm Review on C & Memory Management

Lecture Notes on Garbage Collection

Limitations of the stack

CSE351 Autumn 2013 Final Exam (11 Dec 2013)

VMem. By Stewart Lynch.

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

CSE351 Spring 2018, Final Exam June 6, 2018

Compiler Construction

CS61C : Machine Structures

CMSC 330: Organization of Programming Languages

Dynamic Storage Allocation

HW 3: Malloc CS 162. Due: Monday, March 28, 2016

DAY 3. CS3600, Northeastern University. Alan Mislove

Transcription:

/9/01 Process s Address Space Dynamic Memory 0x7fffffff Stack Data (Heap) Data (Heap) 0 Text (Code) Backing the Heap When a process starts the heap is empty The process is responsible for requesting memory from the OS System Call: void * sbrk(int increment) Basically: The process asks for an increase of a certain size The OS provides a large contiguous memory space, which the process manages itself Dynamic memory allocation Two basic memory allocator types: Explicit: application allocates and frees space E.g., malloc and free in C Implicit: application allocates, but does not free space E.g. garbage collection in Java, ML or Lisp Allocation In both cases the memory allocator provides an abstraction of memory as a set of blocks Doles out free memory blocks to application 4 1

/9/01 p1 = malloc(4) p = malloc(5) p = malloc(6) free(p) p4 = malloc() Allocation examples Goals of good malloc/free Primary Good time performance for malloc and free Ideally should take constant time (not always possible) Should certainly not take linear time in the number of blocks Good space utilization User allocated structures should be large fraction of the heap Want to minimize fragmentation Some Others Good locality properties Structures allocated close in time should be close in space Similar objects should be allocated close in space Robust Can check that free(p1) is on a valid allocated object p1 Can check that memory references are to allocated space 6 Internal fragmentation Fragmentation causes poor memory utilization Comes in two forms: internal and external fragmentation Internal fragmentation The difference between the block size and the payload size External fragmentation Occurs when there is enough aggregate heap memory, but no single free block is large enough p1 = malloc(4) p = malloc(5) block p = malloc(6) Internal fragmentation payload Internal fragmentation free(p) Due to: overhead of maintaining heap data structures padding for alignment purposes explicit policy decisions (e.g., not to split the block). Depends only on the pattern of previous requests, so easy to measure 7 p4 = malloc(6) oops! Depends on the pattern of future requests, so it s difficult to measure 8

/9/01 What to blocks to allocate? Implementation issues First fit: Search list from beginning, select first free block that fits ( free Can take linear time in num. of blocks (allocated and In practice it can cause splinters at beginning of list Next fit: Like first-fit, but start from end of previous search Research suggests that fragmentation is worse Best fit: Search the list, select free block with closest size that fits Keeps fragments small --- usually helps fragmentation Will typically run slower than first-fit How do we know how much memory to free just given a pointer? How do we keep track of the free blocks? What do we do with the extra space when allocating a structure that is smaller than the free block it is placed in? How do we pick a block to use for allocation many might fit? How do we reinsert freed block? 9 10 Knowing how much to free Standard method Keep length of a block in the word preceding the block This word is often called the header field or header Requires an extra word for every allocated block p0 = malloc(4) free(p0) p0 Block size data What is the first thing free has to do? 5 11 Keeping track of free blocks (The Easy Way) A B C D 11111100 00111000 01111111 11111000 8 16 4 Memory regions Bitmap Pros: EASY Cons: Takes up a lot of space Bitmap

/9/01 Keeping track of free blocks Method 1: Implicit List Implicit list using lengths -- links all blocks 5 4 6 Need to identify whether each block is free or allocated Can use extra bit Bit can be put in the same word as the size if block sizes are always multiples of /4/8 (for alignment) mask out low order bit when reading size 1 word Explicit list tracks only free blocks using pointers inside the free blocks 5 4 6 Segregated free list different free lists for different size classes Blocks sorted by size Can use a balanced tree with pointers within each free block, and the length used as a key Format of allocated and free blocks size payload optional padding a a = 1: allocated block a = 0: free block size: block size payload: application data ( only (allocated blocks 14 Implicit list: Allocating in free block Implicit list: Freeing a block Allocating from a free block splitting Since allocated space might be smaller than free space, we need to split the block Simplest implementation: Only need to clear allocated flag But can lead to false fragmentation 4 6 char * p = malloc(4); free(p) 4 4 p 4 4 4 4 malloc(5) Oops! There is enough free space, but the allocator won t be able to find it p 16 4

/9/01 Implicit list: Coalescing Join (coalesce) with next and/or previous block if free Coalescing with next block Implicit list: Bidirectional coalescing Boundary tags [Knuth7] Replicate size/allocated word at bottom of free blocks Allows traversing a list backwards, but requires extra space Important and general technique! free(p) 4 4 4 6 p Format of allocated and free blocks Header Boundary tag ( footer ) 1 word size payload and padding size a a a = 1: allocated block a = 0: free block size: total block size payload: application data ( only (allocated blocks Donald Knuth 198- But how do we coalesce with previous block? 17 4 4 4 4 6 6 4 4 ( 1 Constant time coalescing (Case Both adjacent blocks are allocated No coalescing is possible Simple mark block free ( Constant time coalescing (Case Merge current and next block Update header of current and footer of next n 0 n+m 0 n 0 m 0 m 0 n+m 0 5

/9/01 ( Constant time coalescing (Case Previous block is merged with current Update header of previous block and footer of current block ( 4 Constant time coalescing (Case All three blocks are merged Update header of previous and footer of next block n+ n+m1+m 0 n+ m 0 m 0 n+m1+m 0 Overwriting memory Freeing blocks multiple times Off-by-one errors allocates N, operates on N+1 #define BUFSIZE 10 char * p = malloc(bufsize); // Add a NULL terminator to be safe p[bufsize] = 0; x = malloc(n*sizeof(int)); <manipulate x> free(x); y = malloc(m*sizeof(int)); <manipulate y> free(x); What did we just overwrite? 4 6

/9/01 Referencing freed blocks x = malloc(n*sizeof(int)); <manipulate x> free(x);... y = malloc(m*sizeof(int)); (++ i for (i=0; i<m; y[i] = x[i]++; Slow, long-term killer Failing to free blocks (memory leaks) foo() { int *x = malloc(n*sizeof(int));... return; } 5 6 7