Presentation is loading. Please wait.

Presentation is loading. Please wait.

Virtual Memory Prof. Sin-Min Lee Department of Computer Science.

Similar presentations


Presentation on theme: "Virtual Memory Prof. Sin-Min Lee Department of Computer Science."— Presentation transcript:

1 Virtual Memory Prof. Sin-Min Lee Department of Computer Science

2

3

4 Parkinson's law : "Programs expand to fill the memory available to hold them" Idea : Manage the storage available efficiently between the available programs.

5

6

7

8

9

10

11

12

13

14

15

16

17

18 Before VM… Programmers tried to shrink programs to fit tiny memories Result: –Small –Inefficient Algorithms

19 Solution to Memory Constraints Use a secondary memory such as disk Divide disk into pieces that fit memory (RAM) –Called Virtual Memory

20 Implementations of VM Paging –Disk broken up into regular sized pages Segmentation –Disk broken up into variable sized segments

21 Memory Issues Idea: Separate concepts of –address space Disk –memory locations RAM Example: –Address Field = 2 16 = 65536 memory cells –Memory Size = 4096 memory cells How can we fit the Address Space into Main Memory?

22 Paging Break memories into Pages NOTE: normally Main Memory has thousands of pages page 1 page = 4096 bytes New Issue: How to manage addressing?

23 Address Mapping Mapping 2ndary Memory addresses to Main Memory addresses page 1 page = 4096 bytes physical addressvirtual address

24 Address Mapping Mapping 2ndary Memory ( program/virtual ) addresses to Main Memory ( physical ) addresses page 1 page = 4096 bytes physical address used by hardware virtual address used by program 40958191 0 4096 virtualphysical

25 Paging page 40958191 0 4096 virtualphysical 0 4095 / 0 Illusion that Main Memory is Large Contiguous Linear Size(MM) = Size(2ndry M) Transparent to Programmer

26 Paging Implementation Virtual Address Space (Program) & Physical Address Space (MM) –Broken up into equal pages (just like cache & MM!!) Page size  Always a power of 2 Common Size: –512 to 64K bytes

27 Paging Implementation Page Frames Page Tables Programs use Virtual Addresses

28 Memory Mapping Note: 2ndry Mem = 64K; Main Mem = 32K Page Frame: home of VM pages in MM Page Table: home of mappings for VM pages Page #Page Frame #

29 Memory Mapping Memory Management Unit (MMU): Device that performs virtual-to-physical mapping MMU 15-bit Physical Address 32-bit VM Address

30 Memory Management Unit 32-bit Virtual Address Broken into 2 portions 20-bit 12-bit Virtual page # offset in page (since our pages are 4KB) How to determine if page is in MM? Present/Absent Bit in Page Table Entry MMU

31 Demand Paging Possible Mapping of pages Page Fault: Requested page is not in MM Demand Paging: Page is demanded by program Page is loaded into MM

32 Demand Paging Possible Mapping of pages Page Fault: Requested page is not in MM Demand Paging: Page is demanded by program Page is loaded into MM But… What to bring in for a program on start up?

33 Working Set Set of pages used by a process Each process has a unique memory map Importance in regards to a multi-tasked OS At time t, there is a set of all k recently used pages References tend to cluster on a small number of pages Put this set to Work!!! Store & Load it during Process Switching

34 Page Replacement Policy Working Set: –Set of pages used actively & heavily –Kept in memory to reduce Page Faults Set is found/maintained dynamically by OS Replacement: OS tries to predict which page would have least impact on the running program Common Replacement Schemes: Least Recently Used (LRU) First-In-First-Out (FIFO)

35 Replacement Policy Placement Policy –Which page is replaced? –Page removed should be the page least likely to be referenced in the near future –Most policies predict the future behavior on the basis of past behavior

36 Replacement Policy Frame Locking –If frame is locked, it may not be replaced –Kernel of the operating system –Control structures –I/O buffers –Associate a lock bit with each frame

37 Basic Replacement Algorithms Optimal policy –Selects for replacement that page for which the time to the next reference is the longest –Impossible to have perfect knowledge of future events

38 Basic Replacement Algorithms Least Recently Used (LRU) –Replaces the page that has not been referenced for the longest time –By the principle of locality, this should be the page least likely to be referenced in the near future –Each page could be tagged with the time of last reference. This would require a great deal of overhead.

39 Basic Replacement Algorithms First-in, first-out (FIFO) –Treats page frames allocated to a process as a circular buffer –Pages are removed in round-robin style –Simplest replacement policy to implement –Page that has been in memory the longest is replaced –These pages may be needed again very soon

40 Basic Replacement Algorithms Clock Policy –Additional bit called a use bit –When a page is first loaded in memory, the use bit is set to 1 –When the page is referenced, the use bit is set to 1 –When it is time to replace a page, the first frame encountered with the use bit set to 0 is replaced. –During the search for replacement, each use bit set to 1 is changed to 0

41

42 Page Replacement Policies Upon Replacement –Need to know whether to write data back –Add a Dirty-Bit Dirty Bit = 0; Page is clean; No writing Dirty Bit = 1; Page is dirty; Write back

43 Fragmentation Generally…. –Process: Program + Data != Integral # of Pages Wasted space on last page Example: –Program + Data = 26000 byes –Page = 4096 bytes –Result = 2672 bytes wasted on last page Internal Fragmentation: fragments in a page How do we solve this problem?

44 Page Size Smaller page size allows for less wasted space Benefits: –Less internal fragmentation –Less thrashing Drawbacks: – page table – storage – computer cost – time to load – more time spent at disk – miss rate

45 Segmentation Alternative to the 1-D view of Paging Create more than 1 address space Example –Compilation Process 1 segment for each table 0  Large Address Symbol Table Source Text Constants Parse Tree Stack

46 Segmentation Example –Compilation Process 1 segment for each table In a 1-D Address Space Symbol Table Source Text Constants Parse Tree Stack grows continuously grows unpredictably!!

47 Segmentation Example –Compilation Process 1 segment for each table In a Segmented Address Space Symbol Table Source Text Constants Parse Tree Stack Shrink/grow independently

48 Segmentation Virtual Address = Segment # + Offset in Segment Programmer is aware of Segments –May contain procedure, array, stack –Generally not a mixture of types Each Segment is a Logical Entity –It is generally not paged

49 Segmentation Benefits –Eases Linking of Procedures –Facilitates sharing of Procedures & Data –Protection Since user creates –User knows what is stored –Can specify »EXECUTE: procedure code »READ/WRITE: array

50 Implementation of Segmentation Two Ways to Implement –Swapping –Paging (a little bit of both)

51 Implementation of Segmentation Swapping –Like Demand Paging Move out segments to make room for new one Request for 7 moves out 1

52 Implementation of Segmentation Swapping –Like Demand Paging Over a Period of Time Wasted Space!!! External Fragmentation

53 Elimination of External Fragmentation 2 Methods –Compaction –Fit Segments in Existing Holes

54 Elimination of External Fragmentation Compaction –Moving segments closer to zero to eliminate wasted space Compaction

55 Fit Segments in Existing Holes –Maintain a list of Addresses & Hole Size –Algorithms: BEST FIT : choose smallest hole FIRST FIT: scan circularly & choose which fits first Where should I go? Elimination of External Fragmentation

56 Fit Segments in Existing Holes –Maintain a list of Addresses & Hole Size –Algorithms: BEST FIT : choose smallest hole Elimination of External Fragmentation

57 Fit Segments in Existing Holes –Maintain a list of Addresses & Hole Size –Algorithms: FIRST FIT : scan circularly & choose which fits first First Hole Elimination of External Fragmentation Empirically proven to give best results

58 Elimination of External Fragmentation Best of Both Worlds –Hole Coalescing Use Best Fit as your De-Fragmentation Algorithm Upon Removal: –COALESCE ANY NEIGHBORING HOLES

59 Memory Management, Early Systems Single-User Contiguous Scheme Fixed Partitions Dynamic Partitions Deallocation Relocatable Dynamic Partitions Conclusion Single User Configurations Fixed Partitions Dynamic Partitions Relocatable Dynamic Partitions

60 Single-User Contiguous Scheme Each program loaded in its entirety into memory and allocated as much contiguous memory space as needed. If program was too large -- it couldn’t be executed. Minimal amount of work done by Memory Manager. Hardware needed : 1) register to store base address; 2) accumulator to track size of program as it is loaded into memory.

61 Algorithm to Load a Job in a Single-user System 1. Store first memory location of program into base register 2.Set program counter equal to address of first memory location 3.Load instructions of program 4.Increment program counter by number of bytes in instructions 5.Has the last instruction been reached? If yes, then stop loading program If no, then continue with step 6 6.Is program counter greater than memory size? If yes, then stop loading. If no, then continue with step 7 7.Load instruction in memory 8.Go to step 3.

62 Fixed (Static) Partitions Attempt at multiprogramming using fixed partitions –one partition for each job –size of partition designated by reconfiguring the system –partitions can’t be too small or too large. Critical to protect job’s memory space. Entire program stored contiguously in memory during entire execution. Internal fragmentation is a problem.

63 Simplified Fixed Partition Memory Table

64 Original StateAfter Job Entry 100KJob 1 (30K) Partition 1 Partition 2 25KJob 4 (25K) Partition 2 Partition 3 25K Partition 3 Partition 4 50KJob 2 (50K) Partition 4 Job List : J130K J250K J330K J425K Main memory use during fixed partition allocation of Table 2.1. Job 3 must wait.

65 Dynamic Partitions Available memory kept in contiguous blocks and jobs given only as much memory as they request when loaded. Improves memory use over fixed partitions. Performance deteriorates as new jobs enter the system –fragments of free memory are created between blocks of allocated memory (external fragmentation).

66 Dynamic Partitioning of Main Memory & Fragmentation

67 Dynamic Partition Allocation Schemes First-fit: Allocate the first partition that is big enough. –Keep free/busy lists organized by memory location (low- order to high-order). –Faster in making the allocation. Best-fit: Allocate the smallest partition that is big enough – Keep free/busy lists ordered by size (smallest to largest). –Produces the smallest leftover partition. –Makes best use of memory.

68 First-Fit Allocation Example J1 10K J2 20K J3 30K* J4 10K Memory MemoryJob Job Internal locationblock sizenumber sizeStatusfragmentation 10240 30KJ1 10KBusy20K 40960 15KJ4 10KBusy 5K 56320 50KJ2 20KBusy30K 107520 20K Free Total Available:115KTotal Used: 40K Job List

69 Best-Fit Allocation Example J1 10K J2 20K J3 30K J4 10K Memory MemoryJob Job Internal locationblock sizenumber sizeStatusfragmentation 40960 15KJ1 10KBusy 5K 10752020KJ2 20KBusyNone 10240 30KJ3 30KBusyNone 56230 50KJ4 10KBusy40K Total Available:115KTotal Used: 70K Job List

70 First-Fit Memory Request

71 Best-Fit Memory Request

72 Best-Fit vs. First-Fit First-Fit Increases memory use Memory allocation takes less time Increases internal fragmentation Discriminates against large jobs Best-Fit More complex algorithm Searches entire table before allocating memory Results in a smaller “free” space (sliver)

73 Release of Memory Space : Deallocation Deallocation for fixed partitions is simple –Memory Manager resets status of memory block to “free”. Deallocation for dynamic partitions tries to combine free areas of memory whenever possible –Is the block adjacent to another free block? –Is the block between 2 free blocks? –Is the block isolated from other free blocks?

74 Case 1: Joining 2 Free Blocks

75 Case 2: Joining 3 Free Blocks

76 Case 3: Deallocating an Isolated Block

77 Relocatable Dynamic Partitions Memory Manager relocates programs to gather all empty blocks and compact them to make 1 memory block. Memory compaction (garbage collection, defragmentation) performed by OS to reclaim fragmented sections of memory space. Memory Manager optimizes use of memory & improves throughput by compacting & relocating.

78 Compaction Steps Relocate every program in memory so they’re contiguous. Adjust every address, and every reference to an address, within each program to account for program’s new location in memory. Must leave alone all other values within the program (e.g., data values).

79 Program in Memory During Compaction & Relocation Free list & busy list are updated –free list shows partition for new block of free memory –busy list shows new locations for all relocated jobs Bounds register stores highest location in memory accessible by each program. Relocation register contains value that must be added to each address referenced in program so it can access correct memory addresses after relocation.

80 Memory Before & After Compaction (Figure 2.5)

81 Contents of relocation register & close-up of Job 4 memory area (a) before relocation & (b) after relocation and compaction (Figure 2.6)


Download ppt "Virtual Memory Prof. Sin-Min Lee Department of Computer Science."

Similar presentations


Ads by Google