dynamic memory allocation and fragmentation

37
Dynamic memory allocation and fragmentation Seminar on Network and Operating Systems Group II

Upload: ashish-singh

Post on 22-May-2015

5.058 views

Category:

Education


6 download

TRANSCRIPT

Page 1: Dynamic memory allocation and fragmentation

Dynamic memory allocation and fragmentation

Seminar on Network andOperating SystemsGroup II

Page 2: Dynamic memory allocation and fragmentation

Schedule

Today (Monday):General memory allocation mechanismsThe Buddy System

Thursday:General Object CachingSlabs

Page 3: Dynamic memory allocation and fragmentation

What is an allocator and what must it do?

Page 4: Dynamic memory allocation and fragmentation

Memory Allocator

Keeps track of memory in use and free memory Must be fast and waste little memory Services memory requests it receives Prevent forming of memory “holes”

“For any possible allocation algorithm, there will always be a program behavior that forces it into severe fragmentation.”

Page 5: Dynamic memory allocation and fragmentation

The three levels of an allocator

Strategies Try to find regularities in incoming memory

requests. Policies

Decides where and how to place blocks in memory (selected by the strategy)

Mechanisms The algorithms that implement the policy

Page 6: Dynamic memory allocation and fragmentation

Policy techniques

Uses splitting and coalescing to satisfy the incoming requests.Split large blocks for small requestsCoalesce small blocks for larger requests

Page 7: Dynamic memory allocation and fragmentation

Fragmentation, why is it a problem?

Page 8: Dynamic memory allocation and fragmentation

Fragmentation

Fragmentation is the inability to reuse memory that is free

External fragmentation occurs when enough free memory is available but isn’t contiguous Many small holes

Internal fragmentation arises when a large enough block is allocated but it is bigger than needed Blocks are usually split to prevent internal

fragmentation

Page 9: Dynamic memory allocation and fragmentation

What causes fragmentation?

Isolated deaths When adjacent objects do not die at the

same time. Time-varying program behavior

Memory requests change unexpectedly

Page 10: Dynamic memory allocation and fragmentation

Why traditional approaches don’t work

Program behavior is not predictable in general

The ability to reuse memory depends on the future interaction between the program and the allocator100 blocks of size 10 and 200 of size 20?

Page 11: Dynamic memory allocation and fragmentation

How do we avoid fragmentation?

A single death is a tragedy. A million deaths is a statistic.

-Joseph Stalin

Page 12: Dynamic memory allocation and fragmentation

Understanding program behavior

Common behavioral patternsRamps

Data structures that are accumulated over time

Peaks Memory used in bursty patterns usually while

building up temporal data structures.

Plateaus Data structures build quickly and are used for long

periods of time

Page 13: Dynamic memory allocation and fragmentation

Memory usage in the GNU C CompilerK

Byt

es in

use

Allocation Time in Megabytes

Page 14: Dynamic memory allocation and fragmentation

Memory usage in the Grobner programK

Byt

es in

use

Allocation Time in Megabytes

Page 15: Dynamic memory allocation and fragmentation

Memory usage in Espresso PLA OptimizerK

Byt

es in

use

Allocation Time in Megabytes

Page 16: Dynamic memory allocation and fragmentation

Mechanisms

Most common mechanisms usedSequential fitsSegregated free lists

Buddy System

Bitmap fits Index fits

Page 17: Dynamic memory allocation and fragmentation

Sequential fits

Based on a single linear listStores all free memory blocksUsually circularly or doubly linkedMost use boundary tag technique

Most common mechanisms use this method.

Page 18: Dynamic memory allocation and fragmentation

Sequential fits

Best fit, First fit, Worst fit Next fit

Uses a roving pointer for allocation Optimal fit

“Samples” the list first to find a good enough fit

Half fit Splits blocks twice the requested size

Page 19: Dynamic memory allocation and fragmentation

Segregated free lists

Use arrays of lists which hold free blocks of particular size

Use size classes for indexing purposes Usually in sizes that are a power of

two

Requested sizes are rounded up to the nearest available size

2

4

8

16

32

64

128

Page 20: Dynamic memory allocation and fragmentation

Segregated free lists

Simple segregated list No splitting of free blocks Subject to severe external fragmentation

Segregated fit Splits larger blocks if there is no free block in the

appropriate free list Uses first fit or next fit to find a free block Three types: exact lists, strict size classes with

rounding or size classes with range lists.

Page 21: Dynamic memory allocation and fragmentation

Buddy system

A special case of segregated fitSupports limited splitting and coalescingSeparate free list for each allowable sizeSimple block address computation

A free block can only be merged with its unique buddy.Only whole entirely free blocks can be

merged.

Page 22: Dynamic memory allocation and fragmentation

Buddy system16 MB

8 MB

4 MB

3 MB

Free

Page 23: Dynamic memory allocation and fragmentation

Buddy system

Split

Free

16 MB

8 MB

4 MB

3 MB

Free

Page 24: Dynamic memory allocation and fragmentation

Buddy system

Split

Split

Free

Free

16 MB

8 MB

4 MB

3 MB

Free

Page 25: Dynamic memory allocation and fragmentation

Buddy system

Split

Split

Alloc. Free

Free

16 MB

8 MB

4 MB

Page 26: Dynamic memory allocation and fragmentation

Binary buddies

Simplest implementationAll buddy sizes are powers of twoEach block divided into two equal parts

Internal fragmentation very highExpected 28%, in practice usually higher

(Demonstration applet)

Page 27: Dynamic memory allocation and fragmentation

Fibonacci buddies

Size classes based on the fibonacci seriesMore closely-spaced set of size classesReduces internal fragmentationBlocks can only be split into sizes that are

also in the series Uneven block sizes a disadvantage?

When allocating many equal sized blocks

Page 28: Dynamic memory allocation and fragmentation

Fibonacci buddies

13

5 8

21

8 13

2 3 5 8 13 21 34 55 …Size series:

Splitting blocks:

Page 29: Dynamic memory allocation and fragmentation

Weighted buddies

Size classes are power of twoBetween each pair is a size three times a

power of two Two different splitting methods

2x numbers can be split in half2x*3 numbers can be split in half or unevenly

into two sizes.

Page 30: Dynamic memory allocation and fragmentation

Weighted buddies

6

3 3

6

2 4

2 3 4 6 8 12 16 24 …(21) (20*3) (22) (21*3) (23) (22*3) (24) (23*3) …

Size series:

Splitting of 2x*3 numbers:

Page 31: Dynamic memory allocation and fragmentation

Double buddies

Use 2 different binary buddy seriesOne list uses powers of two sizesOther uses powers of two spacing, offset by x

Splitting rulesBlocks can only be split in halfSplit blocks stay in the same series

Page 32: Dynamic memory allocation and fragmentation

Double buddies

6

3 3

6

2 4

2 4 8 16 32 64 128 …(21) (22) (23) (24) (25) (26) (27)…

Size series:

Splitting of 3*2x numbers:

3 6 12 24 48 96 192 …(3*20) (3*21) (3*22) (3*23) (3*24) (3*25) (3*26)…

Page 33: Dynamic memory allocation and fragmentation

Deferred coalescing

Blocks are not merged as soon as they are freed.

Uses quick lists or subpoolsArrays of free lists, one for each size class

that is to be deferredBlocks larger than those defined to be

deferred are returned to the general allocator

Page 34: Dynamic memory allocation and fragmentation

Deferred reuse

Recently freed blocks are not immediately reusedOlder free blocks used instead of newly freed

Compacts long-lived memory blocksCan cause increased fragmentation if only

short-lived blocks are requested

Page 35: Dynamic memory allocation and fragmentation

Discussion

Page 36: Dynamic memory allocation and fragmentation

Questions?

Why can deferred reuse cause increased fragmentation if only short-lived blocks are requested?

How can the order in which the requests arrive effect memory fragmentation?

Why is fragmentation at peaks more important than at intervening points?

Page 37: Dynamic memory allocation and fragmentation

Questions?

When would deferred coalescing be likely to cause more fragmentation?

What is a possible disadvantage when splitting blocks using the fibonacci buddy system?

In the double buddy system, why does the added size-class list reduce internal fragmentation by about 50%?