1 / 29

Memory Management

Memory Management . COSC513 – Spring 2004 Student Name: Nan Qiao Student ID#: 104454 Professor: Dr. Morteza Anvari. Goals of Memory Management. Relocation Protection Sharing Logical Organization Physical Organization. Relocation.

glen
Télécharger la présentation

Memory Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Management COSC513 – Spring 2004 Student Name: Nan Qiao Student ID#: 104454 Professor: Dr. Morteza Anvari

  2. Goals of Memory Management • Relocation • Protection • Sharing • Logical Organization • Physical Organization

  3. Relocation • Location of a program is not known until the program is executed • Where the program is loaded depends on what other programs are in the system • A program may be swapped out and then back into a different location • Need to be able to execute the program regardless of where it is loaded

  4. Protection • User processes cannot be allowed to read or write outside their boundaries • Protection must be done at runtime • Programs can be loaded anywhere in memory • Programs can dynamically allocate memory • Check for valid addresses during address translation • Verify that address is within its bound

  5. Sharing • Several processes may need access to the same data • Shared operating system library (i.e. DLL) • Shared data buffer • More efficient to allow shared access than to copy the data • Contrary to protection schemes • Cannot just allow all processes access to all memory

  6. Logical Structure • A program is typically constructed based on different types of modules • i.e. code, data, stack, heap • Access to each type may be different (I.e. read/write/shared/execute) • Modules are written and compiled separately • References are resolved at runtime • Typically implemented with segmentation

  7. Physical Structure • Need the ability to swap between main and secondary storage • Secondary storage is large and non-volatile • Main memory is fast • Solution: Divide memory into units that can be moved between disk and memory • Movement is coordinated by user or the OS • OS knows current system resources • OS can perform movement more efficiently

  8. Memory Partitioning • Fixed partitioning • Processes loaded into regions of fixed size • Dynamic partitioning • Processes loaded into variable sized regions • Paging • Fixed sized regions of small size • Segmentation • Large dynamic regions • Virtual memory • Uses pages and/or segments

  9. Fixed Partitioning • Memory is divided into a number of fixed sized partitions • Equal sized: all pages are the same • Unequal sized: small and large pages • The entire process is loaded into contiguous memory • Advantage: Simple to implement • Disadvantage: Can be wasteful • Page sizes are determined by the a-priori

  10. 4M Process 2M Process Too large Cannot be loaded InternalFragmentation 10M Process Equal Sized Partitions • Load processes into first available region • Memory utilization can be low • Large processes may not fit Operating System 8M Process 8M 8M 8M 8M 8M

  11. Unequal Sized Partitions • Load process into: • Smallest free region that fits • Region closest to size (may require waiting) • Advantages • Accommodates large and small processes • May have better utilization than a fixed scheme • Disadvantages • Memory utilization low if processes are small • Limits the number of active processes

  12. Operating System 2M 4M 8M 10M Process 16M InternalFragmentation Unequaled Sized Partitions (example) • Only processes larger than their partitions waste memory • Less internal fragmentation 2M Process 4M Process 8M Process

  13. Dynamic Partitioning • Partitions are of variable size • Allocate only the amount of memory needed • Number of partitions only limited by the amount of free memory • Load entire process into contiguous memory • Advantages: • No internal fragmentation • Disadvantages: • Fragmentation still possible (external) • More complex (requires placement scheme)

  14. 8M 1M 32M ExternalFragmentation 8M Process 2M 3M Process Does notfit 1M Process 11M Process 32M 21M 24M 20M 7M Process 10M Process 9M 2M Dynamic Partitioning (example) • Load processes contiguously in memory • Holes are created when processes release memory Operating System

  15. Placement Algorithm • The OS must decide which hole to fill when allocating memory to a process • Best fit: Find the smallest hole that satisfies the allocation • Worst fit: Use the biggest free region • First fit: Use the first region in memory that satisfies the allocation • Next fit: like first fit, but search starting from the last allocation

  16. Internal Fragmentation • i.e. wasted memory within a partition • Occurs for fixed sized partitions • Occurs when a process does not use the entire partition • Prevent by dynamically partitioning • However this causes external fragmentation

  17. External Fragmentation • i.e. wasted memory outside of the partition • Occurs in variable sized partitions • Occurs when holes are not large enough to hold a new process • Solution: • Compaction

  18. 8M 1M 32M 8M Process 2M 3M Process 1M Process 11M Process 32M 24M 21M 20M 7M Process 9M 1M Compaction • Eliminate holes by moving processes • Copy operation is expensive Operating System Operating System 3M Process 11M Process 7M Process 11M

  19. Disadvantages of Partitioning • Fragmentation is unavoidable • Must load entire process into memory • Limits the number of active processes • Solution: use paging and/or segmentation • Why study partitioning • Historical reasons • Paging and segmentation are based on it • May still be used for dynamic (heap) allocation

  20. Paging Main Memory Frame 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 • Memory is divided into small page frames • Pages of process are loaded into frames (need not be contiguous) • Less external fragmentation • Small pages limit internal fragmentation • Page table translates page addresses to frames

  21. Process A (1) Process D (1) Process A (2) Process D (2) Process A (3) Process D (3) Process A (4) Process D (4) Process B (1) Process B (2) Process B (3) Process B (4) Process B (5) Process B (6) Process C (1) Process C (2) Process C (3) Process D (5) Process C (4) Process D (6) Paging (Example) Main Memory Frame 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 • Load pages into empty frames • Sufficient frames must exist for entire process • No compaction needed (pages can be loaded into non-contiguous frames)

  22. Process D (1) Process D (2) Process D (3) Process D (4) Process B (1) Process B (2) Process B (3) Process B (4) Process D Process B Process C Process B (5) Process B (6) Process C (1) Process C (2) Process C (3) Process D (5) Process C (4) Process D (6) Page Tables Main Memory Frame 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 • One page table for each process • Page table translates logical addresses to physical addresses

  23. 0 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 1 1 1 Frame Number Page Offset Logical to Physical Translation • Translation done in hardware • Upper N bits translated to frame number • Lower M bits contain offset into page Logical Address Page 0 Page Number Page Offset Page 1 Page Table Page 2 Frame Number Physical Address ...

  24. Advantages of Logical Addressing • Logical addressing provides a means for process relocation • Each process uses the same logical addresses • Separate page tables map logical addresses of each process to different physical addresses • Process segments start from a fixed location • Makes compiling/loading easier

  25. Segmentation • Process memory is divided into segments • One segment for each type of data (data,code,stack,heap…) • Each segment can be loaded anywhere • Segments are of variable size • Address translation is required • Address contains segment number and offset • Must ensure that process does not address beyond the segment limit

  26. Comparing Paging and Segmentation • Size of regions • Small and fixed size for paging • Large and variable size for segmentation • Address translation • Simple bit shift for paging • More complicated addition for segmentation • Advantages • Paging: very little fragmentation • Segmentation: logical structure

  27. Combining Paging and Segmentation • Combine to gain advantages of both • A process’s segments consist of a number of fixed sized pages • Requires two address translations • First logical segment to logical page address • Next logical page to physical page address

  28. How Paging/Segmentation Meet Requirements • Relocation • Logical to physical translation allow segments or pages to be loaded anywhere • Protection • Address translation gives hardware an opportunity to check for valid addresses • Sharing • Shared regions can be divided into segments • Two logical segments can be mapped to one physical shared segment

  29. How Paging/Segmentation Meet Requirements • Logical Organization • Program is divided into segments • Physical Organization • Pages are convenient units to swap in and out of memory

More Related