Basics

1

Basic Memory Management Concepts Address spaces

Physical address space — The address space MAXsys supported by the hardware

¾ Starting at address 0, going to address MAXsys

MAXprog Logical/virtual address space — A ’s view of its own memory Program

¾ Starting at address 0, going to address MAXprog P

0

But where do addresses come from? MOV r0, @0xfffa620e 0

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

3

Basic Concepts Address generation

The compilation pipeline

0 1000 Library Library Routines Routines prog P P: 0100 1100 : : : : : push ... : : : push ... inc SP, 4 : : foo() inc SP, x jmp 75 jmp 175 jmp 1175 : jmp _foo : : : : : ...... end P foo: ... 75175 1175

Compilation Assembly Linking Loading

4 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…

5

Basic Concepts (Cont’d.) Address Translation

MAXsys MEMORY EXCEPTION

Logical no Physical Addresses Addresses 1500 Program CPU ≤ + P’s yes physical address 1000 space Instructions 500 1000 MAX prog Limit Base Program Register Register P’s logical address space 0 0

6 With base and bounds registers, the OS needs a hole in physical memory at least as big as the process. ¾ ATA. True ¾ B. False

7

Evaluating Dynamic Allocation Techniques The fragmentation problem

External fragmentation ¾ Unused memory between units of MAX 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 Program a table for 4 Q’s Program Code PAS (“text”)

ExecutionData Stack

Execution Stack Program 0 R’s PAS

8 Simple Memory Management Schemes Dynamic allocation of partitions

MAX Simple approach: Program ¾ Allocate a partition when a process is admitted P1 into the system ¾ Allocate a contiguous memory partition to the process Program P2 OS keeps track of... Full-blocks P5 Empty-blocks (“holes”)

Allocation strategies Program First-fit P3 Best-fit Worst-fit Program P4 0

9

First Fit Allocation

To allocate n bytes, use the first available free block such 1K bytes that the block size is larger than n.

2K bytes 2K bytes

To allocate 400 bytes, we use the 1st free block 500 bytes 500 bytes available

10 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)

Advantages Disadvantages ‹ Simple ‹ Slow allocation ‹ Tends to produce larger ‹ External fragmentation free blocks toward the end of the address space

11

Best Fit Allocation

To allocate n bytes, use the smallest available free block 1K bytes 1K bytes such that the block size is larger than n.

2K bytes 2K bytes

To allocate 400 bytes, we use the 3rd free block 500 bytes available (smallest)

12 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

Advantages Disadvantages ‹ Works well when most ‹ External fragmentation allocations are of small size ‹ Slow de-allocation ‹ Relatively simple ‹ Tends to produce many useless tiny fragments (not really great) Doug Lea’s malloc “In most ways this malloc is a best-fit allocator”

13

Worst Fit Allocation

To allocate n bytes, use the largest available free block 1K bytes 1K bytes such that the block size is larger than n.

2K bytes

To allocate 400 bytes, we use the 2nd free block 500 bytes available (largest)

14 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

Advantages Disadvantages ‹ WWkorks bbtest if alllltiocations ‹ Slow de-allocati on are of medium sizes ‹ External fragmentation ‹ Tends to break large free blocks such that large partitions cannot be allocated

15

Allocation strategies

First fit, best fit and worst fit all suffer from external fragmentation. ¾ A. True ¾ B. False

16 Dynamic Allocation of Partitions Eliminating Fragmentation

MAX Compaction Program P ¾ Relocate programs to coalesce holes 1

Swapping Program ¾ Preempt processes & reclaim their memory P2

Program Ready Running P3

ready ? Waiting queue Program Suspended P4 suspended queue semaphore/condition queues 0 17

Memory Management Sharing Between Processes

2n-1 Heap Schemes so far have considered only a single address space per process ¾ A single name space per process Run-TimeProgram ¾ No sharing StackP’s VA S

Program How can one share code and data between Data programs without paging? Program Text 0 Program P’s VA S 18 Multiple Name Spaces Example — Protection/Fault isolation & sharing

n n 2 -1 2 4-1 Heap Heap n 2 3-1 0 Run-Time Run-Time Stack Stack n 2 2-1 0 n Program Program 2 6-1 Data Data Libraries n 0 2 1-1 0 n Program Program 2 5-1 Text Text User 00 Code 0

19

Supporting Multiple Name Spaces Segmentation

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 00n1 s addr s addr

Segment + Address register scheme Single address scheme

20 Implementing Segmentation Base + Limit register scheme Physical Memory

Program Add a segment table containing base & P limit register values

CPU MEMORY 1500 EXCEPTION Program so P’s no Segment n320 0 1000 Logical ≤ yes + Addresses Limit Base Register 500 1000 Register base limit s STBR 0 Segment Table 21

Memory Management Basics Are We Done?

Segmentation allows sharing

… but leads to ppyoor 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

22