1 / 22

Memory Management Basics

Memory Management Basics. Program P. But where do addresses come from?. MOV r0, @0xfffa620e. Basic Memory Management Concepts Address spaces. MAX sys. Physical address space — The address space supported by the hardware Starting at address 0, going to address MAX sys

nalani
Download Presentation

Memory Management Basics

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 Basics

  2. Program P But where do addresses come from? MOV r0, @0xfffa620e Basic Memory Management ConceptsAddress spaces MAXsys • Physical address space— The address space supported by the hardware • Starting at address 0, going to address MAXsys • Logical/virtual address space— A process’s view of its own memory • Starting at address 0, going to address MAXprog MAXprog 0 0

  3. Which is bigger, physical or virtual address space? • A. Physical address space • B. Virtual address space • C. It depends on the system.

  4. Basic ConceptsAddress generation • The compilation pipeline 0 1000 Library Routines Library Routines 0 100 1100 prog P : : foo() : : end P P: : push ... inc SP, x jmp _foo : foo: ... : push ... inc SP, 4 jmp 75 : ... : : : jmp 175 : ... : : : jmp 1175 : ... 75 175 1175 Compilation Assembly Linking Loading

  5. Program Relocation • Program issues virtual addresses • Machine has physical addresses. • If virtual == physical, then how can we have multiple programs resident concurrently? • Instead, relocate virtual addresses to physical at run time. • While we are relocating, also bounds check addresses for safety. • I can relocate that program (safely) in two registers…

  6. 500 1000 Limit Register Base Register Basic Concepts (Cont’d.)Address Translation MAXsys MEMORY EXCEPTION Logical Addresses Physical Addresses no Program 1500 CPU P’s ≤ + yes physicaladdressspace 1000 Instructions MAXprog Program P’s logicaladdressspace 0 0

  7. With base and bounds registers, the OS needs a hole in physical memory at least as big as the process. • A. True • B. False

  8. Execution Stack Program Code(“text”) Data Execution Stack Evaluating Dynamic Allocation TechniquesThe fragmentation problem MAX • External fragmentation • Unused memory between units of allocation • E.g, two fixed tables for 2, but a party of 4 • Internal fragmentation • Unused memory within a unit of allocation • E.g., a party of 3 at a table for 4 Program Q’s PAS Program R’s PAS 0

  9. Simple Memory Management SchemesDynamic allocation of partitions MAX Program P1 • Simple approach: • Allocate a partition when a process is admitted into the system • Allocate a contiguous memory partition to the process Program P2 • OS keeps track of... • Full-blocks • Empty-blocks (“holes”) • Allocation strategies • First-fit • Best-fit • Worst-fit P5 Program P3 Program P4 0

  10. To allocate n bytes, use the first available free block such that the block size is larger than n. First Fit Allocation 1K bytes 2K bytes 2K bytes To allocate 400 bytes, we use the 1st free block available 500 bytes 500 bytes

  11. Advantages • Simple • Tends to produce larger free blocks toward the end of the address space Disadvantages • Slow allocation • External fragmentation Rationale & Implementation • Simplicity of implementation • Requires: • Free block list sorted by address • Allocation requires a search for a suitable partition • De-allocation requires a check to see if the freed partition could be merged with adjacent free partitions (if any)

  12. To allocate n bytes, use the smallest available free block such that the block size is larger than n. Best Fit Allocation 1K bytes 1K bytes 2K bytes 2K bytes To allocate 400 bytes, we use the 3rd free block available (smallest) 500 bytes

  13. Disadvantages • External fragmentation • Slow de-allocation • Tends to produce many useless tiny fragments (not really great) Advantages • Works well when most allocations are of small size • Relatively simple Rationale & Implementation • To avoid fragmenting big free blocks • To minimize the size of external fragments produced • Requires: • Free block list sorted by size • Allocation requires search for a suitable partition • De-allocation requires search + merge with adjacent free partitions, if any Doug Lea’s malloc “In most ways this malloc is a best-fit allocator”

  14. To allocate n bytes, use the largest available free block such that the block size is larger than n. Worst Fit Allocation 1K bytes 1K bytes 2K bytes To allocate 400 bytes, we use the 2nd free block available (largest) 500 bytes

  15. Advantages • Works best if allocations are of medium sizes Disadvantages • Slow de-allocation • External fragmentation • Tends to break large free blocks such that large partitions cannot be allocated Rationale & Implementation • To avoid having too many tiny fragments • Requires: • Free block list sorted by size • Allocation is fast (get the largest partition) • De-allocation requires merge with adjacent free partitions, if any, and then adjusting the free block list

  16. Allocation strategies • First fit, best fit and worst fit all suffer from external fragmentation. • A. True • B. False

  17. Ready Running Waiting ready queue Suspended suspended queue semaphore/condition queues Dynamic Allocation of PartitionsEliminating Fragmentation MAX Program P1 • Compaction • Relocate programs to coalesce holes Program P2 • Swapping • Preempt processes & reclaim their memory Program P3 ? Program P4 0

  18. 2n-1 Heap Run-Time Stack ProgramP’s VAS Program Data Program Text 0 Program P’s VAS Memory ManagementSharing Between Processes • Schemes so far have considered only a single address space per process • A single name space per process • No sharing • How can one share code and data between programs without paging?

  19. Program Data Program Text Multiple Name SpacesExample — Protection/Fault isolation & sharing 2n-1 2n4-1 Heap Heap 2n3-1 0 Run-Time Stack Run-Time Stack 2n2-1 0 2n6-1 Program Data Libraries 0 2n1-1 0 Program Text 2n5-1 User Code 0 0 0

  20. Supporting Multiple Name SpacesSegmentation • New concept: A segment — a memory “object” • A virtual address space • A process now addresses objects —a pair (s, addr) • s— segment number • addr — an offset within an object • Don’t know size of object, so 32 bits for offset? n 0 n2 0 n1 0 s addr s addr Segment + Address register scheme Single address scheme

  21. Implementing SegmentationBase + Limit register scheme Physical Memory Program P • Add a segment table containing base & limit register values CPU 1500 MEMORY EXCEPTION Program s o P’s no Segment n 0 32 0 1000 ≤ + yes Logical Addresses Limit Register Base Register 500 1000 base limit s 0 STBR Segment Table

  22. Memory Management BasicsAre We Done? • Segmentation allows sharing • … but leads to poor memory utilization • We might not use much of a large segment, but we must keep the whole thing in memory (bad memory utilization). • Suffers from external fragmentation • Allocation/deallocation of arbitrary size segments is complex • How can we improve memory management? • Paging

More Related