Top Banner
Dynamic Memory Allocation CS 351: Systems Programming Michael Saelee <[email protected]>
44

Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Jul 29, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Dynamic Memory Allocation

CS 351: Systems Programming Michael Saelee <[email protected]>

Page 2: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

registers

cache (SRAM)

main memory (DRAM)

local hard disk drive (HDD/SSD)

remote storage (networked drive / cloud)

from:

The Memory Hierarchy

Page 3: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

we now have:

Virtual Memory

Page 4: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

now what?

Page 5: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

- code, global variables, jump tables, etc.

- allocated at fork/exec - lifetime: permanent

Static Data

Page 6: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

pages allocated as needed (up to preset stack limit)

- function activation records - local vars, arguments,

return values - lifetime: LIFO

The Stack

Page 7: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

- for dynamic allocation - lifetime: arbitrary!

The Heap

explicitly requested from the kernel

Page 8: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

brk

- starts out empty - brk pointer marks top of

the heap

The Heap

Page 9: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

void *sbrk(int inc); /* increments brk by inc, returns old brk value */

brk

heap mgmt syscall:

The Heap

Page 10: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

hp

void *hp = sbrk(N);

brk

N

The Heap

Page 11: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

after the kernel allocates heap space for a process, it is up to the process to manage it!

Page 12: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

“manage” =tracking memory in use, tracking memory not in use, reusing unused memory

Page 13: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

job of the dynamic memory allocator — typically included as a user-level library and/or language runtime feature

Page 14: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

User Process

sbrk

Disk

RAM

dynamic memory allocator

Heap OS kernel

malloc

application program

Page 15: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

User Process

Disk

RAM

dynamic memory allocator

Heap

application program

free(p) OS kernel

Page 16: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

User Process

Disk

RAM

dynamic memory allocator

Heap

application program

free(p) OS kernel

(heap space may not be returned to the kernel!)

Page 17: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

the DMA constructs a user-level abstraction (re-usable “blocks” of memory) on top of a kernel-level one (virtual memory)

Page 18: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

the user-level implementation must make good use of the underlying infrastructure (the memory hierarchy)

Page 19: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

e.g., the DMA should:

- maintain data alignment

- maximize throughput of requests

- help maximize memory utilization

- leverage locality

how to quantify this?

Page 20: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

utilization = fraction of memory in use

- “in use” is a relative concept

- for DMA, “in use” = amount of memory actually requested by user (aka payload)

- vs. heap space obtained via sbrk

Page 21: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

p1 = malloc(1024); // util = 1K/4K = 25%

Heap

4KB

(given: DMA requests memory in 4KB chunks)

Page 22: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

p1 = malloc(1024); // util = 1K/4K = 25% p2 = malloc(2048); // util = 3K/4K = 75%

4KB

Heap(given: DMA requests memory in 4KB chunks)

Page 23: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

p1 = malloc(1024); // util = 1K/4K = 25% p2 = malloc(2048); // util = 3K/4K = 75% free(p1); // util = 2K/4K = 50%

4KB

Heap(given: DMA requests memory in 4KB chunks)

Page 24: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

p1 = malloc(1024); // util = 1K/4K = 25% p2 = malloc(2048); // util = 3K/4K = 75% free(p1); // util = 2K/4K = 50% p3 = malloc(2048); // util = 4K/8K = 50%

8KB

4KB

Heap(given: DMA requests memory in 4KB chunks)

Page 25: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

p1 = malloc(1024); // util = 1K/4K = 25% p2 = malloc(2048); // util = 3K/4K = 75% free(p1); // util = 2K/4K = 50% p3 = malloc(2048); // util = 4K/8K = 50% free(p3); // util = 2K/8K = 25% free(p2); // util = 0/8K = 0%

// all non-leaking // programs end in 0%

8KB

Heap(given: DMA requests memory in 4KB chunks)

Page 26: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

makes no sense to measure utilization at the end of process execution,

and it makes no sense to arbitrarily measure utilization during execution

Page 27: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

instead, measure peak memory utilization - ratio between maximum aggregate payload

and maximum heap size - “high water mark” measure

- assuming the heap never shrinks, end heap size = max heap size

Page 28: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

- max agg. payload = 4K - max heap size = 8K - peak memory util = 50%

p1 = malloc(1024); // util = 1K/4K = 25% p2 = malloc(2048); // util = 3K/4K = 75% free(p1); // util = 2K/4K = 50% p3 = malloc(2048); // util = 4K/8K = 50% free(p3); // util = 2K/8K = 25% free(p2); // util = 0/8K = 0%

// all non-leaking // programs end in 0%

Page 29: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

p1 = malloc(100); p2 = malloc(200); free(p1); p3 = malloc(300); free(p2); p4 = malloc(100); p5 = malloc(200); free(p3); p6 = malloc(100); p7 = malloc(300); free(p4); free(p5); p8 = malloc(200);

// measured heap size // at end is 1K

// 100 // 300 // 200 // 500 // 300 // 400 // 600 // 300 // 400 // 700 // 600 // 400 // 600

aggregate payload

peak memory util = 700 / 1024 ≈ 68%

Page 30: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

utilization is affected by memory fragmentation two forms: 1. internal fragmentation 2. external fragmentation

Page 31: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

when allocating blocks of memory, it is convenient to make them self-describing i.e., store metadata alongside blocks with size, allocation status, etc.

Page 32: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

allocator must also adhere to alignment requirements (to help optimize cache/memory fetches)

Page 33: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

payload

metadata

padding (for alignment)

“block” internal fragmentation

Page 34: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

amount of internal fragmentation is easy to predict, as it’s based on pre-determined factors

- metadata = fixed amount

- k-byte alignment → max k –1 padding

Page 35: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

Heap

Page 36: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

Heap

Page 37: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

Heap

external fragmentation

Page 38: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

Heap

external fragmentation may affect future heap utilization;

i.e., by preventing free space from being re-used

Page 39: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

Heap

malloc?

Page 40: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

Heap

Page 41: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

Heap

malloc?

forced to request more heap space

Page 42: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

hard to predict the effect of external fragmentation on utilization

in general, we might:

- prefer fewer, larger spans of free space

- try to keep similarly sized blocks together in memory

Page 43: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

but these recommendations are heuristics! - may be defeated by pathological cases

- don’t account for real-world behavior

Page 44: Dynamic Memory Allocation · 2020-04-13 · RAM dynamic memory allocator Heap application program free(p) OS kernel (heap space may not be returned to the kernel!) Computer Science

Computer ScienceScience

It has been proven that for any possible allocation algorithm, there will always be the possibility that some application program will allocate and deallocate blocks in some fashion that defeats the allocator’s strategy and forces it into severe fragmentation ... Not only are there no provably good allocation algorithms, there are proofs that any allocator will be bad for some possible applications.

P. Wilson, M. Johnstone, M. Neely, D. Boles, Dynamic Memory Allocation: A Survey and Critical Review