Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Memory Allocation

Similar presentations


Presentation on theme: "Dynamic Memory Allocation"— Presentation transcript:

1 Dynamic Memory Allocation
CSCE 410/611 Dynamic Memory Allocation Memory Allocation: where? Fragmentation external vs. internal Implementation Approaches Naïve vs. Buddy System Dynamic Memory Management

2 Dynamic Memory Allocation – when?
CSCE 410/611 Dynamic Memory Allocation – when? When does new memory get allocated in the kernel? New process gets created (fork()) New program gets loaded into memory (execve()) Process stack grows What about thread stacks? Process expands heap (malloc()) Process “creates” (attaches to) shared memory segment (shmat()) Map a file to memory (mmap()) Note: These memory segments “belong to” the user! For internal memory management OS uses Slab Allocator. Dynamic Memory Management

3 libraries & dev.drivers
CSCE 410/611 Memory Allocation memory space user program area libraries & dev.drivers Monitor Job1 Job2 Job3 Job4 Dynamic Memory Management

4 Memory Allocation X First Fit Worst Fit Best Fit CSCE 410/611
Dynamic Memory Management

5 (External) Fragmentation
CSCE 410/611 (External) Fragmentation allocate() ?! Dynamic Memory Management

6 (Internal) Fragmentation
CSCE 410/611 (Internal) Fragmentation Internal Fragmentation Dynamic Memory Management

7 (Internal) Fragmentation
CSCE 410/611 (Internal) Fragmentation Dynamic Memory Management

8 Memory Allocation: Data Structures
CSCE 410/611 Memory Allocation: Data Structures global memory descriptor next memory regions start end memory space start end next free list Dynamic Memory Management

9 (Binary) Buddy System Allocation
CSCE 410/611 (Binary) Buddy System Allocation Maintain multiple free lists, one for each power-of-2 size. Allocation: Increase size of request to next power of 2*. Look up segment in free lists. If exists, allocate. If none exists, split next larger segment in half, put first half (the “buddy”) on free list, and return second half. Harry Markowitz 1927- 1990 Nobel Memorial Prize in Economics (nobelprize.org) De-Allocation: Return segment to free list. Check if buddy is free. If so, coalesce. References: Donald Knuth: The Art of Computer Programming Volume 1: Fundamental Algorithms. Second Edition (Reading, Massachusetts: Addison-Wesley, 1997), pp ISBN Dynamic Memory Management

10 How to find a buddy? The buddy of each block can be found with an Exclusive OR of the block’s address and the block’s size. Ex) 32KB size = First 32KB Block = XOR

11 Allocation at Different Levels
user level explicit user-level allocation: malloc(size) allocate virtually contiguous sequence of bytes at user level user-level library kernel level byte-sized allocations: kmalloc(size, gfp_mask) allocate physically contiguous sequence of bytes vmalloc(size, gfp_mask) allocate virtually contiguous sequence of bytes Slab Allocator (+ caching) alloc_pages() and __get_free_pages() allocate frames or pages, at low level useful to allocate contiguous frames/pages. Buddy System!

12 Summary: Dynamic Memory Allocation
CSCE 410/611 Summary: Dynamic Memory Allocation Memory Allocation Internal and External Fragmentation Implementation Approaches Free Lists Buddy System Dynamic Memory Management


Download ppt "Dynamic Memory Allocation"

Similar presentations


Ads by Google