[WEEK 06] C - 동적 메모리 할당

Dynamic Memory Allocation

Introduction

Programmers use dynamic memory allocators(such as malloc) to acquire VM at run time. For data structures whose size is only known at runtime.

Dynamic memory allocators manage an area of process virtual memory known as the heap.

Allocator maintains heap as collection of variable sized blocks, which are either allocated or free.

Types of allocators

  1. Explicit allocator: application allocates and frees space (e.g. malloc and free in C)
  2. Implicit allocator: application allocates, but does not free space (e.g. garbage collection in Java, ML, and Lisp)

malloc Package

  1. malloc()
#include <stdlib.h>

void *malloc(size_t size)

Successful: returns a pointer to a memory block of at least size bytes aligned to an 8-byte(x86) or 16-byte(x86-64) boundary

Unsuccessful: returns NULL(0) and sets errno

  1. free()
#include <stdlib.h>

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

  1. Other fuctions
  • calloc: version of malloc that initializes allocated block to zero
  • realloc: changes the size of a previously allocated block
  • sbrk: used internally by allocators to grow or shrink the heap

Constraints

Applications

  • can issue arbitrary sequence of malloc and free requests
  • free request must be to a malloc'd block

Allocators

  • can't control number or size of allocated blocks
  • must respond immediately to malloc requests (i.e., can't reorder or buffer requests)
  • must allocate blocks from free memory (i.e., can only place allocated blocks in free memory)
  • must align blocks so they satisfy all alignment requirements (8-byte(x86) or 16-byte(x86-64) on Linux boxes)
  • can manipulate and modify only free memory
  • can't move the allocated blocks once they are malloc'd (i.e., compaction is not allowed)

Performance Goal: Throughput

  • Given some sequence of malloc and free requests
  • Goals: maximize throughput and peak memory utilization

Throughput

  • Number of completed requests per unit time
  • Example: 5,000 malloc calls and 5,000 free calls in 10 seconds - Throughput is 1,000 operations/second

Performance Goal: Peak Memory Utilization

  • Given some sequence of malloc and free requests
  • Def: Aggregate payload P
    - malloc(p) results in a block with a payload of p bytes
    - After request R has completed, the aggregate payload P is the sum of currently allocated payloads
  • Def: Current heap size H
    - Assume H is monotonically nondecreasing (i.e., heap only grows when allocator uses sbrk)
  • Def: Peak memory utilization after k+1 requests
    - U = (max P) / H

Fragmentation

  • Poor memory utilization caused by fragmentation
    - Internal fragmentation
    - External fragmentation

Internal Fragmentation

  • For a given block, internal fragmentation occurs if payload is smaller than block size
  • Caused by
    - Overhead of maintaining heap data structures
    • Padding for alignment purposes
    • Explicit policy decision
  • Depends only on the pattern of previous requests

External Fragmentation

  • Occurs when there is enough aggregate heap memory, but no single free block is large enough
  • Depends on the pattern of future requests

Implementation Issues

  • How do we know how much memory to free given just 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?

Knowing How Much to Free

Standard Method

  • Keep the 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

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

Method 1: Implicit List

  • For each block we need both size and allocation status
    - Could store this information in two words
  • If blocks are aligned, some low-order address bits are always 0
  • Instead of storing an always-0 bitm use it as a allocated/free flag
  • When reading size word, must mask out this bit

좋은 웹페이지 즐겨찾기