Chapter 3 Memory Managment
Chapter 3 Memory Managment
Memory Management
The base register holds the smallest legal physical memory address
and the limit register specifies the size of the range.
For example, if the base register holds 300000 and the limit
register is 120,000, then the program can legally access all
addresses from 300000 through 420,000.
5
Memory Management
6
Why Memory Management is required
Allocate and de-allocate memory before and after
process execution.
To keep track of used memory space by processes.
7
Logical versus Physical Address Space
Logical Address space: An address generated by the CPU is
known as “Logical Address”.
8
Logical versus Physical Address Space
11
The OS has a part called Memory manager that manipulates
and perform an operation on memory hierarchy.
12
Memory Management
Memory management systems are divided in to two classes.
13
Monoprogramming without Swapping or Paging
The simplest possible memory management scheme is to run just one
program at a time, sharing the memory between that program and the
operating system.
0X FFF Devices drivers in
OS in ROM ROM
User Program
User Program
User program
OS in RAM OS in RAM
0
15
Fragmentation
16
Fragmentation
Internal fragmentation:
Internal fragmentation occurs when memory blocks are
allocated to the process more than their requested size.
Due to this some unused space is leftover and creates
an internal fragmentation problem.
External fragmentation:
In external fragmentation, we have a free memory block, but
we can not assign it to process because blocks are not
contiguous.
To overcome the external fragmentation problem
Compaction is used. In the compaction technique, all free
memory space combines and makes one large block. So, this
space can be used by other processes effectively.
17
Swapping
• Swapping is a mechanism in which a process can be swapped
temporarily out of main memory to a backing store , and then
brought back into memory for continued execution.
19
Swapping
• Major time consuming part of swapping is transfer time. Total
transfer time is directly proportional to the amount of memory
swapped.
• Let us assume that the user process is of size 100KB and the
backing store is a standard hard disk with transfer rate of 1 MB
per second. The actual transfer of the 100K process to or from
memory will take
= 1/10 second
20
Swapping
21
Swapping(1)
24
MM using Bitmaps
1) Bitmap
The free space list is implemented as a bitmap or bit vector.
25
MM using Bitmaps
The Size of the
bitmap
depends on the
size of memory
and size of the
allocation unit.
a) A part of memory with five processes and three holes. The tick marks show the
memory allocation units. The shaded regions (0 in the bitmap) are free.
(b) The corresponding bitmap.
(c) The same information as a list.
26
MM using Linked List
There is a head pointer which points the first free block of
the list which is kept in a special location on the disk.
Advantage
The operating system simply allocate the first block in
free space list and move the head pointer to the next free
block in the list.
Disadvantage
Searching the free space will be very time consuming
each block will have to be read from the disk.
27
MM using Linked List
28
Paging
30
Paging
† Address generated by CPU is divided into
31
Paging
32
Paging…
Pages and page frames are
always equal. Here 4KB
◦ How much pages and page
frames can we get from 64
KB of Virtual address
space and 32 KB of
physical memory?
33
Paging Model of Logical and Physical
Memory
34
Paging Hardware
35
Paging Example
† Paging example for a 32-byte memory with 4-byte pages
36
Goals of Paging
37
What happens if there is no free frame?
• Page replacement happens when a requested page is not
present in the main memory and the available space is not
sufficient for allocation to the requested page.
B. 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
First-In-First-Out (FIFO) Page replacement
Replace the pages that has been in memory the longest time.
If the needed/demanding page is not available in the main memory it is
called page fault.
If the needed/demanding page is available in the main memory it is called
page Hit.
What happens when we have 3 frames in the above example A?
45
Optimal Page Replacement
46
Least Recently Used (LRU) Algorithm
† Replace the page that has not been used for the longest period of
time.
† The optimal algorithm uses the time when a page is to be used
next
† FIFO - uses the time when a page was brought into memory
† LRU – replace the page which is least recently used.
† LRU– use the recent past as an approximation of the near future.
47
LRU Page Replacement
† 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 one is the smallest value and use that frame .
48
Global vs. Local Allocation
Global replacement – process selects a replacement frame from the
set of all frames; one process can take a frame from another.
◦ A process is no longer in control of its page-fault rate
◦ Usually results in greater system throughput and its therefore
more commonly used
Local replacement – each process selects from only its own set of
allocated frames.
◦ Does not take advantage of less used pages in other processes
which could improve overall system throughput
49
Working sets and Thrashing
† If a single process is too large for memory, there is nothing the
operating system (OS) can do. That process will simply
thrash.
† If the problem arises because of the sum of several processes:
figure out how much memory each process needs to prevent
thrashing. This is called its working set.
50
Caching
51
!!!
E E
H R
R T
T E
A P
C H
O F
N D
E
52