Dynamic Memory Allocation: Basic Concepts and Implicit Free Lists, Slides of Software Engineering

The basic concepts of dynamic memory allocation, focusing on implicit free lists. It includes the malloc package, malloc example, assumptions made in the lecture, an allocation example, constraints, performance goals, fragmentation, and implementation issues. It also discusses knowing how much memory to free and keeping track of free blocks using different methods.

Typology: Slides

2011/2012

Uploaded on 04/16/2012

shyrman
shyrman 🇺🇸

4.2

(6)

239 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Carnegie Mellon
1
Dynamic(Memory(Alloca/on:((
Basic(Concepts(
15#213:'Introduc0on'to'Computer'Systems ''
17th'Lecture,'Oct.'21,'2010'
Instructors:''
Randy'Bryant'and'Dave'O’Hallaron'
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Dynamic Memory Allocation: Basic Concepts and Implicit Free Lists and more Slides Software Engineering in PDF only on Docsity!

Dynamic Memory Alloca/on:

Basic Concepts

15-­‐213: Introduc0on to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant and Dave O’Hallaron

Today

 Basic concepts

 Implicit free lists

Dynamic Memory Alloca/on

 Allocator maintains heap as collec/on of variable sized

blocks , which are either allocated or free

 Types of allocators

 Explicit allocator : applica0on allocates and frees space

 E.g., malloc and free in C

 Implicit allocator: applica0on allocates, but does not free space

 E.g. garbage collec0on in Java, ML, and Lisp

 Will discuss simple explicit memory alloca/on today

The malloc Package

#include <stdlib.h> void *malloc(size_t size)

 Successful:

 Returns a pointer to a memory block of at least size bytes (typically) aligned to 8-­‐byte boundary  If size == 0 , returns NULL

 Unsuccessful: returns NULL (0) and sets errno

void free(void *p)

 Returns the block pointed at by p to pool of available memory

 p must come from a previous call to malloc or realloc

Other func/ons

 calloc : Version of malloc that ini0alizes allocated block to zero.

 realloc: Changes the size of a previously allocated block.

 sbrk : Used internally by allocators to grow or shrink the heap

Assump/ons Made in This Lecture

 Memory is word addressed (each word can hold a

pointer)

Allocated block (4 words) Free block (3 words) (^) Free word Allocated word

Alloca/on Example

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

Performance Goal: Throughput

 Given some sequence of malloc and free requests:

 R 0 , R 1 , ..., Rk, ... , Rn-­‐

 Goals: maximize throughput and peak memory u/liza/on

 These goals are o]en conflic0ng

 Throughput:

 Number of completed requests per unit 0me

 Example:

 5,000 malloc calls and 5,000 free calls in 10 seconds  Throughput is 1,000 opera0ons/second

Performance Goal: Peak Memory U/liza/on

 Given some sequence of malloc and free requests:

 R 0 , R 1 , ..., Rk, ... , Rn-­‐

 Def: Aggregate payload P

k

 malloc(p) results in a block with a payload of p bytes

 A]er request Rk has completed, the aggregate payload Pk is the sum of

currently allocated payloads

 Def: Current heap size H

k

 Assume Hk is monotonically nondecreasing

 i.e., heap only grows when allocator uses sbrk

 Def: Peak memory u@liza@on aAer k requests

 Uk = ( maxi<k Pi ) / Hk

Internal Fragmenta/on

 For a given block, internal fragmenta@on occurs if payload is

smaller than block size

 Caused by

 Overhead of maintaining heap data structures

 Padding for alignment purposes

 Explicit policy decisions

(e.g., to return a big block to sa0sfy a small request)

 Depends only on the paUern of previous requests

 Thus, easy to measure

Payload Internal fragmenta/on Block Internal fragmenta/on

External Fragmenta/on

 Occurs when there is enough aggregate heap memory,

but no single free block is large enough

 Depends on the paUern of future requests

 Thus, difficult to measure

p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2)

p4 = malloc(6) Oops! (what would happen now?)

Knowing How Much to Free

 Standard method

 Keep the length of a block in the word preceding the block.

 This word is o]en called the header field or header

 Requires an extra word for every allocated block

p0 = malloc(4) p free(p0) block size data 5

Keeping Track of Free Blocks

 Method 1: Implicit list using length—links all blocks

 Method 2: Explicit list among the free blocks using pointers

 Method 3: Segregated free list

 Different free lists for different size classes

 Method 4: Blocks sorted by size

 Can use a balanced tree (e.g. Red-­‐Black tree) with pointers within each

free block, and the length used as a key 5 4 6 2 (^5) 4 6 2

Method 1: Implicit List

 For each block we need both size and alloca/on status

 Could store this informa0on in two words: wasteful!

 Standard trick

 If blocks are aligned, some low-­‐order address bits are always 0

 Instead of storing an always-­‐0 bit, use it as a allocated/free flag

 When reading size word, must mask out this bit

Size 1 word Format of allocated and free blocks Payload a = 1: Allocated block a = 0: Free block Size: block size Payload: applica/on data (allocated blocks only) a Op/onal padding

Detailed Implicit Free List Example

Start of heap Double-­‐word aligned 8/0 16/1 32/0 16/ Unused 0/ Allocated blocks: shaded Free blocks: unshaded Headers: labeled with size in bytes/allocated bit