Unit-5: Memory Management
Unit-5: Memory Management
Unit-5: Memory Management
GTU # 3140702
Unit-5
Memory
Management
Looping
Outline
• Concept of Memory
• Memory abstraction
• Logical and Physical address map
• Memory allocation
• Virtual Memory: Basics of Virtual Memory
• Paging
• Page Replacement Algorithms
• Segmentation
Concept of Memory
Section - 1
What is Memory?
🞂 Computer memory is any physical device capable of storing
information temporarily or permanently.
🞂 Types of memory
1. Random Access Memory (RAM), is a volatile memory that loses its
contents when the computer or hardware device loses power.
▪ Computer uses special ROM called BIOS (Basic Input Output System)
which permanently stores the software needed to access computer
hardware such as hard disk and then load an operating system into
RAM and start to execute it.
What is Memory?
🞂 Computer memory is any physical device capable of storing
information temporarily or permanently.
🞂 Types of memory
3. Programmable Read-Only Memory (PROM), is a memory chip on
which you can store a program. But once the PROM has been used,
you cannot wipe it clean and use it to store something else. Like
ROMs, PROMs are non-volatile. E.g CD-R
• Faster
Register • Expensive
• Less Capacity
L1 Cache
L2 Cache
Main Memory
User Program
User Program
User Program
OS in RAM 0 0 OS in RAM 0
Partition 5 Partition 5
Partition 4 Partition 4
Partition 3 Partition 3
Partition 2 Partition 2
Partition 1 Partition 1
OS OS
Multiprogramming with dynamic partitions
🞂 Here, memory is shared among operating system and various
simultaneously running processes.
🞂 Initially, the entire available memory is treated as a single free partition.
🞂 Process is allocated exactly as much memory as it requires.
🞂 Whenever any process enters in a system, a chunk of free memory big
enough to fit the process is found and allocated. The remaining User
Program
unoccupied space is treated as another free partition.
🞂 If enough free memory is not available to fit the process, process needs to
wait until required memory becomes available.
🞂 Whenever any process gets terminate, it releases the space occupied. If
the released free space is contiguous to another free partition, both the
free partitions are merged together in to single free partition. OS
🞂 Better utilization of memory than fixed sized size partition.
Multiprogramming with dynamic partitions
C C C C C
B B B B
A
A A A
D D D
OS OS OS OS OS OS OS
Memory compaction
🞂 When swapping creates multiple holes in memory, it is possible to combine them all in one big
hole by moving all the processes downward as far as possible. This techniques is known as
memory compaction.
🞂 It requires lot of CPU time.
Process - B
Process - B
Process - A Process - A
OS OS
Multiprogramming without memory abstraction
0 32764
.
.
We can use static Using absolute CMP 16412
Program 2
relocation at program address is wrong 16408
load time here 16404
16400
Program 1 Program 2 16392
JMP 28
16388
16384
0 16380 0 16380 0 16380
. . .
. . .
ADD 28 CMP 28 ADD 28
Program 1
MOV 24 24 MOV 24
20 20 20
16 16 16
JMP 24 8 8 8
JMP 28 JMP 24
4 4 4
0 0 0
Static relocation
When program was loaded at address 16384, the 0 32764
constant 16384 was added to every program .
address during the load process. .
• Slow CMP 16412
Program 2
16408
• Required extra information from program
16404
16400
Program 1 Program 2 16392
JMP 16412
16388
16384
0 16380 0 16380 0 16380
. . .
. . .
ADD 28 CMP 28 ADD 28
Program 1
MOV 24 24 MOV 24
20 20 20
16 16 16
JMP 24 8 8 8
JMP 28 JMP 24 4
4 4
0 0 0
Logical and Physical address
map
Section - 3
Base and Limit register
🞂 An address space is set of addresses that a process can use to address 0 32764
.
memory. .
CMP 16412
🞂 An address space is a range of valid addresses in memory that are 16408
available for a program or process. 16404
16400
🞂 Two registers: Base and Limit 16392
⮩ Base register: Start address of a program in physical memory. JMP 16412
16388
⮩ Limit register: Length of the program. 16384
0 16380
🞂 For every memory access .
⮩ Base is added to the address .
ADD 28
⮩ Result compared to Limit MOV 24
🞂 Only OS can modify Base and Limit register. 20
16
8
JMP 24 4
0
Dynamic relocation
🞂 Dynamic relocation refers to address transformations being done during execution of a
program.
🞂 Steps in dynamic relocation
1. Hardware adds relocation register (base) to virtual address to get a physical address
2. Hardware compares address with limit register; address must be less than or equal limit
3. If test fails, the processor takes an address trap and ignores the physical address.
Base Limit
Register Register
H 18 2 P 20 6 P 26 3 H 29 3 X
🞂 Another way to keep track of memory is to maintain a linked list of allocated and free memory
segments, where segment either contains a process or is an empty hole between two
processes.
🞂 Each entry in the list specifies a hole (H) or process (P), the address at which it starts the
length and a pointer to the next entry.
🞂 The segment list is kept sorted by address.
🞂 Sorting this way has the advantage that when a process terminates or is swapped out,
updating the list is straightforward.
🞂 A terminating process normally has two neighbors (except when it is at the very top or bottom
of memory).
Memory management with Linked List
P 0 5 H 5 3 P 8 6 P 14 4
H 18 2 P 20 6 P 26 3 H 29 3 X
Neighbors
Before P terminate After P terminate
A P B P is replaced by H A B
P is replaced by H
A P A
and two H are merged
P is replaced by H
P B B
and two H are merged
P is replaced by H
P
and three H are merged
Memory allocation
Section - 4
Memory allocation
🞂 Four memory allocation algorithms are as follow
1. First fit
2. Next fit
3. Best fit
4. Worst fit
First fit
🞂 Search starts from the starting location of the memory.
🞂 First available hole that is large enough to hold the process is selected for allocation.
🞂 The hole is then broken up into two pieces, one for process and another for unused memory.
🞂 Example: Processes of 212K, 417K, 112K and 426K arrives in order.
100k 500k 200k 300k 600k
🞂 Here process of size 426k will not get any partition for allocation.
🞂 Fastest algorithm because it searches as little as possible.
🞂 Memory loss is higher, as very large hole may be selected for small process.
Next fit
🞂 It works in the same way as first fit, except that it keeps the track of where it is whenever it
finds a suitable hole.
🞂 The next time when it is called to find a hole, it starts searching the list from the place where
it left off last time.
🞂 Example: Processes of 212K, 417K, 112K and 426K arrives in order.
100k 500k 200k 300k 600k
🞂 Here process of size 426k will not get any partition for allocation.
🞂 Search time is smaller.
🞂 Memory manager must have to keep track of last allotted hole to process.
🞂 It gives slightly worse performance than first fit.
Best fit
🞂 Entire memory is searched here.
🞂 The smallest hole, which is large enough to hold the process, is selected for allocation.
🞂 Example: Processes of 212K, 417K, 112K and 426K arrives in order.
100k 500k 200k 300k 600k
🞂 Here process of size 426k will not get any partition for allocation.
🞂 Search time is high, as it searches entire memory every time.
🞂 This algorithm can be used only with dynamic partitioning.
Virtual Memory: Basics of Virtual
Memory
Section - 5
Virtual Memory
🞂 Memory is hardware that your computer uses to load the operating system and run programs.
🞂 Computer consists of one or more RAM chips that each have several memory modules.
🞂 The amount of real memory in a computer is limited to the amount of RAM installed. Common
memory sizes are 1GB, 2GB, and 4GB.
🞂 Because your computer has a finite amount of RAM, it is possible to run out of memory when
too many programs are running at one time.
🞂 This is where virtual memory comes into the picture.
🞂 Virtual memory increases the available memory of your computer by enlarging the "address
space," or places in memory where data can be stored.
🞂 It does this by using hard disk space for additional memory allocation.
🞂 However, since the hard drive is much slower than the RAM, data stored in virtual memory
must be mapped back to real memory in order to be used.
Virtual Memory
🞂 Each program has its own address space, which is broken up into
pages. RAM
🞂 These pages are mapped onto the physical memory but, to run the Another
program, all pages are not required to be present in the physical HDD Process’
memory. s
Memory
🞂 The operating system keeps those parts of the program currently in
RAM
use in main memory, and the rest on the disk.
🞂 In a system using virtual memory, the physical memory is divided
into page frames and the virtual address space is divided in into
HDD
equally-sized partitions called pages. RAM
🞂 Virtual memory works fine in a multiprogramming system, with bits
and pieces of many programs in memory at once. Virtual
Address space HDD
Non-Contiguous
Non-contiguous Memory Allocation
🞂 In this type of allocation, separate blocks of memory are allocated to a file/process.
🞂 It executes slowly in comparison to contiguous memory.
🞂 It is difficult to be controlled by the operating system.
🞂 More overhead since it needs to translate multiple addresses.
🞂 It includes paging.
🞂 It includes segmentation.
🞂 No memory is wasted.
🞂 External fragmentation occurs in this type of allocation.
🞂 The swapped-in processes can be arranged in any location in the memory.
Non-contiguous Memory Allocation
🞂 Paging and Segmentation are the two ways which allow a process’s physical address space to
be non-contiguous.
🞂 It has advantage of reducing memory wastage but it increases the overheads due to address
translation.
🞂 It slows the execution of the memory because time is consumed in address translation..
🞂 In non-contiguous allocation, Operating system needs to maintain the table which is called
Page Table for each process which contains the base address of the each block which is
acquired by the process in memory space.
🞂 In non-contiguous memory allocation, different parts of a process is allocated different places
in Main Memory. Spanning is allowed which is not possible in other techniques like Dynamic or
Static Contiguous memory allocation.
🞂 That’s why paging is needed to ensure effective memory allocation and to find optimal way to
load process in the partition.
🞂 Paging is done to remove External Fragmentation.
Paging
Section - 6
Paging
Paging
Paging
Paging
Paging
🞂 Paging is a storage mechanism used to retrieve processes from the secondary storage (Hard
disk) into the main memory (RAM) in the form of pages.
40K – 44K Page • The main idea behind the paging is to divide each process in the
11 form of pages. The main memory will also be divided in the
36K – 40K Virtual page
Page form of frames.
Virtual 32K – 36K
10 • One page of the process is to be stored in one of the frames of
Address
28K – 32K Page 9 the memory.
Space
24K – 28K Page 8
Physical
20K – 24K Page 7 Page frame Frame 20K – Memory
16K – 20K Page 6 6 24K Address
Frame 16K –
12K – 16K Page 5
5 20K
8K – 12K Page 4 Frame 12K –
4K – 8K Page 3 4 16K
0K – 4K Page 2 Frame 8K – 12K
3
4K – 8K
Page 1 Frame
0K – 4K
2
Frame
Paging
🞂 The pages can be stored at the different locations of the memory but the priority is always to
find the contiguous frames or holes.
🞂 Pages of the process are brought into the main memory only when they are required otherwise
they reside in the secondary storage.
🞂 The sizes of each frame must be equal. Considering the fact that the pages are mapped to the
frames in Paging, page size needs to be as same as frame size.
🞂 Different operating system defines different frame sizes.
Paging
40K – 44K
• Only required pages are loaded in the physical
36K – 40K Virtual page
memory.
Virtual 32K – 36K 5
• Transfers between RAM and disk are always in
Address
28K – 32K
units of a page.
Space
24K – 28K
Physical
20K – 24K 3 Page frame 5 20K – Memory
16K – 20K 4 4 24K Address
16K –
12K – 16K 0 3
20K
8K – 12K 2 12K –
4K – 8K 1 1 16K
0K – 4K 2 0 8K – 12K
4K – 8K
Internal operation of the MMU
Outgoing physical address (24580)
1 1 0 0 0 0 0 0 0 0 0 0 1 0 0
15 000 0
14 000 0
13 000 0
12 000 0
11 111 1
10 000 0
9 101 1
12 bit offset
Page 8 000 0
7 000 0 copied directly from
Table
6 000 0 input to output
Virtual page=2 5 011 1
is used as an 4 100 1
3 000 1
index into the
2 110 1 110
page table 1 001 1
0 010 1
Present/
Absent bit
0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0
Logical Address
Physical Address
P D F3 D
P2
Performance is
reduced by half
CPU
For every instruction
Memory reference Memory
occur two time
Use a hardware
Page Table
TLB (Translation
Lookaside Buffer) Pag Fram
e e
P1 F2
P2 F3
P3 F1
Mapping from virtual address to physical address must be fast
Logical Address
Physical Address
P D F3
F1 D
P3 P2
TLB
Pag Fram
CPU e e
Hardware
P1 F2 Memory
P2 F3
Page Table
Pag Fram
e e Data Structure
P1 F2 Inside kernel
P2 F3
P3 F1
Mapping from virtual address to physical address using TLB
🞂 Steps in TLB hit:
⮩ CPU generates virtual address.
⮩ It is checked in TLB (present).
⮩ Corresponding frame number is retrieved, which now tells where in the main memory page lies.
Logical Address
PT1 PT2 Offset Physical Address
F202 D
10 bit 10 bit 12 bits
2 P2
Pag Fram
e e
Page Pointe P1 F101
CPU
r P2 F102
1 Memory
Pag Fram
2 e e
3 P1 F201
P2 F202
Pag Fram
e e
P1 F301
P2 F302
Inverted Page Table
Page 7 0 1 2 0 3 0 4 2 3 0 3 2 0 2 0 1 7 0 1
Request
Frame – 1 7 7 7 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1
Frame – 2 0 0 0 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0
Frame – 3 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 7 7 7
Page Faults F F F F F F F F F
(9)
FIFO Page Replacement Algorithm
🞂 The first in first out page replacement algorithm is the simplest page replacement algorithm.
🞂 The operating system maintains a list of all pages currently in memory, with the most recently
arrived page at the tail and least recent at the head.
🞂 On a page fault, the page at head is removed and the new page is added to the tail.
🞂 When a page replacement is required the oldest page in memory needs to be replaced.
🞂 The performance of the FIFO algorithm is not always good because it may happen that the
page which is the oldest is frequently referred by OS.
🞂 Hence removing the oldest page may create page fault again.
FIFO Page Replacement Algorithm
🞂 Page Reference String:
⮩ 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
⮩ Three frames
Page 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Request
Frame – 1 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
Frame – 2 0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
Frame – 3 1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
Page Faults F F F F F F F F F F F F F F F
(15)
Second Chance Page Replacement Algorithm
🞂 It is modified form of the FIFO page replacement algorithm.
🞂 It looks at the front of the queue as FIFO does, but instead of immediately paging out that page,
it checks to see if its referenced bit is set.
⮩ If it is not set (zero), the page is swapped out.
⮩ Otherwise, the referenced bit is cleared, the page is inserted at the back of the queue (as if it were a new
page) and this process is repeated.
0 1 0 1 0
Remove B C D E A Add at end
1 0 1 0 1
C D E A F
Second Chance Page Replacement Algorithm
🞂 If all the pages have their referenced bit set, on the second encounter of the first page in the
list, that page will be swapped out, as it now has its referenced bit cleared.
🞂 If all the pages have their reference bit cleared, then second chance algorithm degenerates into
pure FIFO.
1 1 1 1 1
A B C D E
0 0 0 0 0
A B C D E
0 0 0 0 1
B C D E F
Clock Page Replacement Algorithm
🞂 In second chance algorithm pages are constantly
moving around on its list. So it is not working 1
efficiently. A
🞂 A better approach is to keep all the page frames on a 1 10
circular list in the form of a clock. F G
B G
Page 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Request
Frame – 1 7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
Frame – 2 0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
Frame – 3 1 1 1 3 3 3 2 2 2 2 2 2 2 2 2 7 7 7
Page Faults F F F F F F F F F F F F
(12)
NRU (Not Recently Used) Page Replacement Algorithm
🞂 NRU makes approximation to replace the page based on R (referenced) and M (modified) bits.
🞂 When a process is started up, both page bits for all pages are set to 0 by operating system.
🞂 Periodically, the R bit is cleared, to distinguish pages that have not been referenced recently
from those that have been.
🞂 When page fault occurs, the operating system inspects all the pages and divide them into 4
categories based on current values of their R and M bits
⮩ Case 0 : not referenced, not modified
⮩ Case 1 : not referenced, modified
⮩ Case 2 : referenced, not modified
⮩ Case 3 : referenced, modified
🞂 The NRU (Not Recently Used) algorithm removes a page at random from the lowest numbered
nonempty class.
NRU (Not Recently Used) Page Replacement Algorithm
🞂 For example if,
⮩ Page-0 is of class-2 (referenced, not modified)
⮩ Page-1 is of class-1 (not referenced, modified)
⮩ Page-2 is of class-0 (not referenced, not modified)
⮩ Page-3 is of class-3 (referenced, modified)
🞂 So lowest class page-2 needs to be replaced by NRU.
Belady’s Anomaly (in FIFO Page Replacement Algorithm)
🞂 Page Reference String: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Page Faults of 4 Frame > Page Faults of 3 Frame
Page 1 2 3 4 1 2 5 1 2 3 4 5
Request
Frame – 1 1 1 1 4 4 4 5 5 5 5 5 5
Frame – 2 2 2 2 1 1 1 1 1 3 3 3
Frames
Three
Frame – 3 3 3 3 2 2 2 2 2 4 4
Page Faults F F F F F F F F F
(9)
Page 1 2 3 4 1 2 5 1 2 3 4 5
Request
Frame – 1 1 1 1 1 1 1 5 5 5 5 4 4
Four Frames
Frame – 2 2 2 2 2 2 2 1 1 1 1 5
Frame – 3 3 3 3 3 3 3 2 2 2 2
Frame – 4 4 4 4 4 4 4 3 3 3
Page Faults F F F F F F F F F F
(10)
Belady’s Anomaly (in FIFO Page Replacement Algorithm)
🞂 Belady’s anomaly is the phenomenon in which increasing the number of page frames results in
an increase in the number of page faults for certain memory access patterns.
🞂 This phenomenon is commonly experienced when using the First-In First-Out (FIFO) page
replacement algorithm.
🞂 In FIFO, the page fault may or may not increase as the page frames increase, but in Optimal and
stack-based algorithms like LRU, as the page frames increase the page fault decreases.
Sum
🞂 A computer has four page frames. The time of loading, Pag Loaded Last used R M
time of last access and the R and M bit for each page e
given below. Which page FIFO, LRU and NRU will replace. 0 126 280 1 0
1 230 265 0 1
🞂 FIFO:- Page which is arrived first needs to be removed
first, so page-3 needs to replace. 2 140 270 0 0
3 110 285 1 1
🞂 LRU:- When page fault occurs, throw out the page that has been used for the longest time.
🞂 Page-1 is not used for the long time from all four, so LRU suggest replacing page-1.
🞂 NRU:-
⮩ Page-0 is of class-2 (referenced, not modified)
⮩ Page-1 is of class-1 (not referenced, modified)
⮩ Page-2 is of class-0 ( not referenced, not modified)
⮩ Page-3 is of class-3 (referenced, modified)
⮩ So lowest class page-2 needs to be replaced by NRU
Segmentation
Section - 8
Segmentation
Process P Segment Map Table
🞂 Segmentation is a memory management Physical
Sr Size Memor
technique in which each job is divided Segment 1 y Memory
into several segments of different sizes, Addres
one for each module that contains s
100
pieces that perform related functions. 1 300 100
200
🞂 Each segment is actually a different 2 500 400
300
logical address space of the program. Segment 2 3 100 900
400
N X NM
🞂 When a process is to be executed, its 500
corresponding segmentation are loaded 600
into non-contiguous memory though 700
every segment is loaded into a Segment 3
800
contiguous block of available memory.
900
🞂 Segmentation memory management Segment N 1000
works very similar to paging but here 1100
segments are of variable-length where
as in paging pages are of fixed size.
Segmentation
Process P Segment Map Table
🞂 A program segment contains the Physical
Sr Size Memor
program's main function, utility Segment 1 y Memory
functions, data structures, and so on. Addres
s
🞂 The operating system maintains a 100
1 300 100
segment map table for every process. 200
2 500 400
🞂 Segment map table contains list of free Segment 2 3 100 900
300
memory blocks along with segment 400
N X NM
numbers, their size and corresponding 500
memory locations in main memory. 600
🞂 For each segment, the table stores the 700
Segment 3
starting address of the segment and the 800
length of the segment. 900
🞂 A reference to a memory location Segment N 1000
includes a value that identifies a 1100
segment and an offset.
Paging VS Segmentation
Paging Segmentation
Paging was invented to get large address Segmentation was invented to allow programs
space without having to buy more physical and data to be broken up into logically
memory. independent address space and to add sharing
and protection.
The programmer does not aware that paging is The programmer is aware that segmentation is
used. used.
Procedure and data cannot be distinguished Procedure and data can be distinguished and
and protected separately. protected separately.
Change in data or procedure requires compiling Change in data or procedure requires compiling
entire program. only affected segment not entire program.
Sharing of different procedures not available. Sharing of different procedures available.
Questions
1. Explain multiprogramming with fixed partition.
2. Explain link list method for dynamic memory management.
3. How free space can be managed by OS.
4. Explain Swapping and Fragmentation in detail.
5. What is Paging? Explain paging mechanism in MMU with example.
6. Explain TLB and Virtual Memory.
7. Discuss demand paging.
8. Define following Terms: Thrashing
9. List different Page Replacement Algorithms? Discuss it in terms of page faults.
10. What is Belady’s anomaly? Explain with suitable example.
11. Consider (70120304230321201701) page reference string: How many page fault would occur
for following page replacement algorithm. Consider 3 frames and 4 frames.
1. FIFO 2. LRU 3. Optimal
Questions
12. Given six Partition of 300KB, 600KB, 350KB, 200KB, 750KB and 125KB(in order), how would
the first-fit, best-fit and worst-fit algorithms places processes of size 115 KB, 500KB, 358KB,
200KB and 375KB(in order)? Which algorithm is efficient for the use of memory?
13. Calculate the page fault rates for below reference string in case of FIFO and Optimal page
replacement algorithm. Assume the memory size is 4 page frames and all frames are initially
empty. 0,2,1,6,4,0,1,0,3,1,2,1
14. Consider the following reference string. Calculate the page fault rates for FIFO and OPTIMAL
page replacement algorithm. Assume the memory size is 4 page frame.
1,2,3,4,5,3,4,1,6,7,8,7,8,9,7,8,9,5,4,5,4,2
15. Consider the page reference string: 1,2,3,4,5,3,4,1,6,7,8,7,8,9,7,8,9,5,4,5,4,2 With four Frames
How many page faults would occur for the FIFO, Optimal page replacement algorithms? which
algorithm is efficient? (assume all frames are initially empty)