Operating Systems ECE 344 Memory Management Overview Ashvin

  • Slides: 15
Download presentation
Operating Systems ECE 344 Memory Management Overview Ashvin Goel ECE University of Toronto

Operating Systems ECE 344 Memory Management Overview Ashvin Goel ECE University of Toronto

Outline q Introduction to memory management q Managing memory with bitmaps and lists q

Outline q Introduction to memory management q Managing memory with bitmaps and lists q Simple memory management and fragmentation 2

Introduction to Memory Management q OS needs to manage physical memory o q Allocate

Introduction to Memory Management q OS needs to manage physical memory o q Allocate memory for programs and for itself Requirements Isolation: programs should be protected from other programs o Abstraction: programs have the illusion that they have as much memory as their virtual address space o Sharing: programs should be able to share memory with other programs for communication o q Goals Low memory overhead: programs should be able to use as much as memory as possible o Low Performance overhead: CPU should be spend as much time executing programs as possible o 3

Managing Memory q q q Boot loader, loads OS code and data in low

Managing Memory q q q Boot loader, loads OS code and data in low memory After that, OS needs to track the usage of the rest of the physical memory OS tracks what memory is allocated (used) or free (unused), similar to heap o addr = kmalloc(size) § Allocate contiguous memory of a given size § Address of allocated memory is returned o kfree(addr) § Program frees memory previously allocated at address addr q Two common techniques for managing memory o Bitmaps, linked lists 4

Managing Memory With Bitmaps q A Bitmap is a long bit string o One

Managing Memory With Bitmaps q A Bitmap is a long bit string o One bit for every chunk of memory § 1 = in use § 0 = free q Size of chunk determines size of bitmap o Chunk size = 32 bits § Overhead for bitmap = 1/(32 + 1) = 3% o Chunk size = 4 KB § Overhead for bitmap = 1/(4 * 1024 * 8 + 1) = 1 / 32769 q Larger chunk size Lower overhead o Wastes more space (on average, half chunk size), called internal fragmentation o 5

Bitmap Operations q mem = allocate(K) Search bitmap for K consecutive 0 bits o

Bitmap Operations q mem = allocate(K) Search bitmap for K consecutive 0 bits o Set the K bits to 1 o Cost: linear search o q free(mem, K) Determine starting bit in bitmap based on memory address o Set next K bits to 0 o 6

Managing Memory With Linked Lists q mem = allocate(K) Search linked list for a

Managing Memory With Linked Lists q mem = allocate(K) Search linked list for a hole (free region) with size >= K o When size > K, break hole into allocated region, smaller hole o q free(mem, K) Determine region based on memory address o Convert allocated region into hole o Merge with adjacent hole to avoid small holes o 7

Searching Linked Lists q Could use a separate allocated and free list q First

Searching Linked Lists q Could use a separate allocated and free list q First Fit: Start searching at the beginning of the list q Best Fit: Find the smallest hole that will work o q Tends to create lots of little holes Quick Fit: Keep separate lists for common sizes o Efficient but more complicated implementation 8

Simple Memory Management q A basic memory management system assigns each program a contiguous

Simple Memory Management q A basic memory management system assigns each program a contiguous region in physical memory Region size depends on program size o OS occupies one region o P 2 P 1 q Problems OS Internal fragmentation: program doesn’t use entire region o External fragmentation: a large enough region cannot be allocated to the program, even if enough memory is available o § Can use compaction, i. e. , move processes periodically to collect all free space into one hole, expensive o Allocating additional memory is expensive: can’t grow region, requires copying entire program into another larger region 9

Programs and Memory Addresses q q q When a program is written, it uses

Programs and Memory Addresses q q q When a program is written, it uses variable names When a program runs, it accesses physical memory addresses Setting up mapping from program variables to memory addresses requires compiler, linker, OS and h/w support 10

Compiler, Linker, OS and H/W q Compiler Converts program source file to object file

Compiler, Linker, OS and H/W q Compiler Converts program source file to object file o Generates relocatable virtual memory addresses o q Linker Links together multiple object files to single program on disk o Generates absolute virtual memory addresses o q OS Loads program and dynamic libraries into physical memory o Links program code with dynamic library code o Sets up virtual memory hardware o q H/W o Translates virtual memory address to physical address 11

Generating Memory Addresses Virtual memory On disk main: 0 main: Physical memory . .

Generating Memory Addresses Virtual memory On disk main: 0 main: Physical memory . . . 1500 pr: 7500 pr: 2000 main: 8000 main: f(); call ? ? 20 call +35 2020 call +35 8020 call +35 pr(); call ? ? 25 call ? ? 2025 call -530 8025 call -530 2030. . . 8030. . . 2060 f: 8060 f: . . . Compilation 60 f: . . . Linker . . . OS Loader . . . MMU base: H/W 6000 12

Simple Memory Management q A basic memory management system assigns each program a contiguous

Simple Memory Management q A basic memory management system assigns each program a contiguous region in physical memory Region size depends on program size o OS occupies one region o P 2 P 1 q Problems OS Internal fragmentation: program doesn’t use entire region o External fragmentation: a large enough region cannot be allocated to the program, even if enough memory is available o § Can use compaction, i. e. , move processes periodically to collect all free space into one hole, expensive o Allocating additional memory is expensive: can’t grow region, requires copying entire program into another larger region 13

Summary q OS needs to allocate physical memory to programs (and to itself) with

Summary q OS needs to allocate physical memory to programs (and to itself) with low overhead Bitmaps, linked lists are two methods for managing memory o Programs uses similar techniques for managing their heap o q A simple memory management scheme allocates contiguous physical memory to programs Makes it hard to grow programs o Wastes memory due to internal and external fragmentation o 14

Think Time q q What is the difference between a virtual and a physical

Think Time q q What is the difference between a virtual and a physical memory address? Why is hardware support required for address translation? How does the OS manage memory using bitmaps? using lists? Under which conditions is each approach preferable? What is internal and external fragmentation? 15