5chapter Five - Memory Management
5chapter Five - Memory Management
Memory 5
Management
Operating System
1
Memory Management
• Introduction
• Logical versus Physical Address Space
• Swapping
• Memory Management Schemes
• Memory Partitioning
• Contiguous Allocation
• Paging
• Segmentation
2
Introduction
3
Binding of Instructions and Data
to Memory
Address binding of instructions and data to memory addresses can
happen at three different stages.
Compile-time Address Binding :
If the compiler is responsible for performing address binding then it is
called compile-time address binding.
It will be done before loading the program into memory.
The compiler requires interacts with an OS memory manager to
perform compile time address binding.
Load time Address Binding :
It will be done after loading the program into memory.
This type of address binding will be done by the OS memory manager
i.e loader.
Execution time or dynamic Address Binding :
The dynamic type of address binding done by the processor at the time
of program execution.
4
Memory-Management Unit
(MMU)
• Part of the operating system that manages memory is called the
Memory Manager (MM).
Keeping track of which part of memory is in use
Allocating and de-allocating memory to processes.
Managing swapping between memory and disk
• A good memory manager has the following attributes
It allows all processes to run.
Its memory (space used for the management activity) overhead
must be reasonable.
Its time overhead (time required for the management activity) is
reasonable.
5
Logical vs. Physical Address
Space
Logical address – generated by the CPU; also referred to as
virtual address.
Physical address – address seen by the memory unit.
• Logical and physical addresses are the same in compile-time and
load-time address-binding schemes; logical (virtual) and physical
addresses differ in execution-time address-binding scheme.
6
Swapping
7
Schematic View of Swapping
8
Memory Management Schemes
9
Address Spaces
10
Memory Partitioning
11
Fixed partitioning
13
Memory Partitioning (Cont.)
14
Memory Partitioning (Cont.)
15
Managing Free Memory
16
Memory Management with
Bitmaps
• The memory is divided into fixed size allocation units.
• Each allocation unit is represented with a bit in the bit map. If the bit is 0, it
means the allocation unit is free and if it is 1, it means it is occupied.
• The following figure shows part of memory and the corresponding bitmap.
17
Memory Management with
Linked Lists
• A linked list of allocated and free memory segments is used.
• Each segment is either a process or a hole between two processes
and contains a number of allocation units.
• Each entry in the list consists of
Segment type: P/H (1bit)
The address at which it starts
The length of the segment
A pointer to the next entry
18
How Do You Allocate Memory to
New Processes?
How to satisfy a request of size n from a list of free holes?
• First-fit: Allocate the first hole that is big enough.
• Best-fit: Allocate the smallest hole that is big enough; must search
entire list, unless ordered by size. Produces the smallest leftover
hole.
• Worst-fit: Allocate the largest hole; must also search entire list.
Produces the largest leftover hole.
• First-fit and best-fit better than worst-fit in terms of speed and
storage utilization.
19
VIRTUAL MEMORY
20
VIRTUAL MEMORY (Cont.)
21
Paging
22
Paging Example
23
Address Translation Scheme
24
Address Translation
Architecture
25
Demand Paging
26
Page Replacement Algorithms
• When a page fault occurs, the operating system has to choose a page
to evict (remove from memory) to make room for the incoming page.
• While it would be possible to pick a random page to evict at each
page fault, system performance is much better if a page that is not
heavily used is chosen.
• If a heavily used page is removed, it will probably have to be brought
back in quickly, resulting in extra overhead.
27
First-In-First-Out (FIFO)
Algorithm
• FIFO page replacement algorithm selects the page that has been in
memory the longest.
• Example: FIFO
28
The Optimal Page Replacement
Algorithm
• Replace page that will not be used for longest period of time.
• 4 frames example
29
Least Recently Used (LRU)
Algorithm
• It replaces the page in memory that has not been referenced for the
longest time.
• Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
30
LRU Algorithm (Cont.)
• Counter implementation
Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter.
When a page needs to be changed, look at the counters to
determine which are to change.
• Stack implementation – keep a stack of page numbers in a double link
form:
Page referenced:
move it to the top
requires 6 pointers to be changed
No search for replacement
31
Thrashing
32
Segmentation
33
Logical View of Segmentation
34
Segmentation Architecture
35
Logical View of Segmentation
36
Paging Vs. Segmentation
Segmentation Paging
Program is divided into variable size Program is divided into fixed size pages
segments
User or compiler is responsible for Division into pages is performed by the
dividing the program into segments Operating System
Segmentation is slower than paging Paging is faster than segmentation
Segmentation is visible to the user Paging is invisible to the user
Segmentation eliminates internal Paging suffers from internal
fragmentation fragmentation
Segmentation suffers from external There is no external fragmentation
fragmentation
37
Thank you
38