Dynamic memory allocation and fragmentation Seminar on Network

  • Slides: 37
Download presentation
Dynamic memory allocation and fragmentation Seminar on Network and Operating Systems Group II

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

Schedule n Today (Monday): ¨ General memory allocation mechanisms ¨ The Buddy System n

Schedule n Today (Monday): ¨ General memory allocation mechanisms ¨ The Buddy System n Thursday: ¨ General ¨ Slabs Object Caching

What is an allocator and what must it do?

What is an allocator and what must it do?

Memory Allocator n n Keeps track of memory in use and free memory Must

Memory Allocator n n 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. ”

The three levels of an allocator n Strategies ¨ n Policies ¨ n Try

The three levels of an allocator n Strategies ¨ n Policies ¨ n Try to find regularities in incoming memory requests. Decides where and how to place blocks in memory (selected by the strategy) Mechanisms ¨ The algorithms that implement the policy

Policy techniques n Uses splitting and coalescing to satisfy the incoming requests. ¨ Split

Policy techniques n Uses splitting and coalescing to satisfy the incoming requests. ¨ Split large blocks for small requests ¨ Coalesce small blocks for larger requests

Fragmentation, why is it a problem?

Fragmentation, why is it a problem?

Fragmentation n n Fragmentation is the inability to reuse memory that is free External

Fragmentation n n Fragmentation is the inability to reuse memory that is free External fragmentation occurs when enough free memory is available but isn’t contiguous ¨ Many n 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

What causes fragmentation? n Isolated deaths ¨ n When adjacent objects do not die

What causes fragmentation? n Isolated deaths ¨ n When adjacent objects do not die at the same time. Time-varying program behavior ¨ Memory requests change unexpectedly

Why traditional approaches don’t work Program behavior is not predictable in general n The

Why traditional approaches don’t work Program behavior is not predictable in general n The ability to reuse memory depends on the future interaction between the program and the allocator n ¨ 100 blocks of size 10 and 200 of size 20?

How do we avoid fragmentation? A single death is a tragedy. A million deaths

How do we avoid fragmentation? A single death is a tragedy. A million deaths is a statistic. -Joseph Stalin

Understanding program behavior n Common behavioral patterns ¨ Ramps n Data structures that are

Understanding program behavior n Common behavioral patterns ¨ Ramps n Data structures that are accumulated over time ¨ Peaks n Memory used in bursty patterns usually while building up temporal data structures. ¨ Plateaus n Data structures build quickly and are used for long periods of time

KBytes in use Memory usage in the GNU C Compiler Allocation Time in Megabytes

KBytes in use Memory usage in the GNU C Compiler Allocation Time in Megabytes

KBytes in use Memory usage in the Grobner program Allocation Time in Megabytes

KBytes in use Memory usage in the Grobner program Allocation Time in Megabytes

KBytes in use Memory usage in Espresso PLA Optimizer Allocation Time in Megabytes

KBytes in use Memory usage in Espresso PLA Optimizer Allocation Time in Megabytes

Mechanisms n Most common mechanisms used ¨ Sequential fits ¨ Segregated free lists n

Mechanisms n Most common mechanisms used ¨ Sequential fits ¨ Segregated free lists n Buddy System ¨ Bitmap fits ¨ Index fits

Sequential fits n Based on a single linear list ¨ Stores all free memory

Sequential fits n Based on a single linear list ¨ Stores all free memory blocks ¨ Usually circularly or doubly linked ¨ Most use boundary tag technique n Most common mechanisms use this method.

Sequential fits n n Best fit, First fit, Worst fit Next fit ¨ n

Sequential fits n n Best fit, First fit, Worst fit Next fit ¨ n Optimal fit ¨ n Uses a roving pointer for allocation “Samples” the list first to find a good enough fit Half fit ¨ Splits blocks twice the requested size

Segregated free lists n n Use arrays of lists which hold free blocks of

Segregated free lists n n 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 n Requested sizes are rounded up to the nearest available size 2 4 8 16 32 64 128

Segregated free lists n Simple segregated list ¨ ¨ n No splitting of free

Segregated free lists n Simple segregated list ¨ ¨ n 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.

Buddy system n A special case of segregated fit ¨ Supports limited splitting and

Buddy system n A special case of segregated fit ¨ Supports limited splitting and coalescing ¨ Separate free list for each allowable size ¨ Simple block address computation n A free block can only be merged with its unique buddy. ¨ Only whole entirely free blocks can be merged.

Buddy system 16 MB 3 MB Free 8 MB 4 MB

Buddy system 16 MB 3 MB Free 8 MB 4 MB

Buddy system 16 MB 3 MB Split 8 MB Free 4 MB Free

Buddy system 16 MB 3 MB Split 8 MB Free 4 MB Free

Buddy system 16 MB 3 MB Split 8 MB Split Free 4 MB Free

Buddy system 16 MB 3 MB Split 8 MB Split Free 4 MB Free

Buddy system 16 MB Split 8 MB Split Free 4 MB Alloc. Free

Buddy system 16 MB Split 8 MB Split Free 4 MB Alloc. Free

Binary buddies n Simplest implementation ¨ All buddy sizes are powers of two ¨

Binary buddies n Simplest implementation ¨ All buddy sizes are powers of two ¨ Each block divided into two equal parts n Internal fragmentation very high ¨ Expected n 28%, in practice usually higher (Demonstration applet)

Fibonacci buddies n Size classes based on the fibonacci series ¨ More closely-spaced set

Fibonacci buddies n Size classes based on the fibonacci series ¨ More closely-spaced set of size classes ¨ Reduces internal fragmentation ¨ Blocks can only be split into sizes that are also in the series n Uneven block sizes a disadvantage? ¨ When allocating many equal sized blocks

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

Fibonacci buddies Size series: 2 3 5 8 13 21 34 55 … Splitting blocks: 13 5 21 8 8 13

Weighted buddies n Size classes are power of two ¨ Between each pair is

Weighted buddies n Size classes are power of two ¨ Between each pair is a size three times a power of two n Two different splitting methods ¨ 2 x numbers can be split in half x ¨ 2 *3 numbers can be split in half or unevenly into two sizes.

Weighted buddies Size series: 2 3 4 6 8 12 16 24 … (21)

Weighted buddies Size series: 2 3 4 6 8 12 16 24 … (21) (20*3) (22) (21*3) (22*3) (24) (23*3) … Splitting of 2 x*3 numbers: 6 3 2 4

Double buddies n Use 2 different binary buddy series ¨ One list uses powers

Double buddies n Use 2 different binary buddy series ¨ One list uses powers of two sizes ¨ Other uses powers of two spacing, offset by x n Splitting rules ¨ Blocks can only be split in half ¨ Split blocks stay in the same series

Double buddies Size series: 2 (21) 3 4 8 16 32 64 128 …

Double buddies Size series: 2 (21) 3 4 8 16 32 64 128 … (22) (23) (24) (25) (26) 12 24 48 96 192 … 6 (27)… (3*20) (3*21) (3*22) (3*23) (3*24) (3*25) (3*26)… Splitting of 3*2 x numbers: 6 3 2 4

Deferred coalescing Blocks are not merged as soon as they are freed. n Uses

Deferred coalescing Blocks are not merged as soon as they are freed. n Uses quick lists or subpools n ¨ Arrays of free lists, one for each size class that is to be deferred ¨ Blocks larger than those defined to be deferred are returned to the general allocator

Deferred reuse n Recently freed blocks are not immediately reused ¨ Older n free

Deferred reuse n Recently freed blocks are not immediately reused ¨ Older n free blocks used instead of newly freed Compacts long-lived memory blocks ¨ Can cause increased fragmentation if only short-lived blocks are requested

Discussion

Discussion

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

Questions? Why can deferred reuse cause increased fragmentation if only short-lived blocks are requested? n How can the order in which the requests arrive effect memory fragmentation? n Why is fragmentation at peaks more important than at intervening points? n

Questions? When would deferred coalescing be likely to cause more fragmentation? n What is

Questions? When would deferred coalescing be likely to cause more fragmentation? n What is a possible disadvantage when splitting blocks using the fibonacci buddy system? n In the double buddy system, why does the added size-class list reduce internal fragmentation by about 50%? n