Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Management Operating Systems Lecture 4, April 3, 2003 Mr. Greg Vogl Uganda Martyrs University.

Similar presentations


Presentation on theme: "Memory Management Operating Systems Lecture 4, April 3, 2003 Mr. Greg Vogl Uganda Martyrs University."— Presentation transcript:

1 Memory Management Operating Systems Lecture 4, April 3, 2003 Mr. Greg Vogl Uganda Martyrs University

2 April 3, 2003 Operating Systems: Memory Management 2 Overview Fixed and Variable Partition Paging Segmentation Virtual Memory and Page Replacement Segmented Paging Protection and Sharing DOS and UNIX Memory Management

3 April 3, 2003 Operating Systems: Memory Management 3 Sources Ritchie Ch. 5-7 Burgess 5.1-2 Solomon Part 6

4 April 3, 2003 Operating Systems: Memory Management 4 What is stored in memory? Operating system code and data User program code User program data Process Control Blocks Stack for executing subroutines Memory mapped I/O: device drivers Screen/display memory (Video RAM)

5 April 3, 2003 Operating Systems: Memory Management 5 Memory management goals/tasks Manage several processes at same time Load into memory, swap out to disk Run processes quickly, use available memory Protect most processes from each other But allow some processes to share memory Ease memory management for programmer Allocate memory in contiguous logical blocks Map logical addresses to physical addresses

6 April 3, 2003 Operating Systems: Memory Management 6 Fixed partition memory Each process gets fixed partition of memory Usually the OS is put at the bottom (address 0) Use different partition sizes Accommodates different possible process sizes Don’t let a process harm another’s memory Check that the addresses are in its partition  Every partition has unused (wasted) space  Not enough space for big new processes?

7 April 3, 2003 Operating Systems: Memory Management 7 Variable partition memory Allocate the memory each process needs Free the space of a terminated process Put new processes in empty “holes” Adjacent holes can be merged About 2x as many processes as holes  Holes maybe not right size for new processes How to choose a hole to put in a new process?

8 April 3, 2003 Operating Systems: Memory Management 8 Storage placement policies Best fit Put new process in smallest possible hole Remaining hole is as small as possible Worst fit Put new process in largest possible hole Reduces number of big holes, creates few small ones First fit (or next fit) Put new process in first (or next) hole big enough to fit No overhead of finding min/max hole sizes Which is best? Tradeoff: speed vs. space

9 April 3, 2003 Operating Systems: Memory Management 9 Memory implementation Functions to allocate, deallocate, reallocate UNIX uses malloc(), free(), realloc() C++ uses new and delete operators Lists keep track of allocated and free blocks Keep lists of various-sized holes (powers of 2)

10 April 3, 2003 Operating Systems: Memory Management 10 Fragmentation Gaps of unused storage space Occurs in all storage devices (memory, disk)  Wastes space, may also reduce performance Internal fragmentation Unused space within a process or block Can occur if word size > smallest data size External fragmentation Unused space between processes or blocks

11 April 3, 2003 Operating Systems: Memory Management 11 Compaction (defragmentation) Join together used memory; combine holes Move allocated blocks to an end of memory Calculate distance the block will move Add to pointers, then move the data  Need to move a lot of things in memory  Need to find and move all pointers  Need to suspend processes until done  Cannot use in time-critical systems

12 April 3, 2003 Operating Systems: Memory Management 12 When/how often to compact? When any process terminates When there is no more free memory At fixed intervals At user(s) request

13 April 3, 2003 Operating Systems: Memory Management 13 Coalescing holes Don’t coalesce Give entire hole (maybe used later by realloc) Buddy system Combined buddies align in powers of 2 When (de)allocating do the buddy block too

14 April 3, 2003 Operating Systems: Memory Management 14 Garbage Collection Find inaccessible blocks, add to free list Conservative Treat pointer-like memory addresses as pointers  Not all garbage found Reference count Each block stores a count of pointers to itself When a block’s count is 0, free the block  Does not detect circular lists of garbage

15 April 3, 2003 Operating Systems: Memory Management 15 Mark and sweep Algorithm Mark used blocks using depth-first search Sweep (free) unused blocks and compact Disadvantages  Not helpful if memory is almost full  Must load many swapped pages into memory

16 April 3, 2003 Operating Systems: Memory Management 16 Generational Divide memory into spaces Objects are usually short- or long-lived Keep long-lived objects in their own spaces Clean out mostly empty spaces Copy objects to other spaces when accessed