Lecture_11 - Introduction to memory management
Lecture_11 - Introduction to memory management
Systems
COSC 22073/BECS 22233
Lesson 11: Introduction Memory Management
A.M.K.S Aththanayake
Faculty of Science
University of Kelaniya
1
Kenel Data
Process in an OS (PCB)
Stack
Data
(Global &
Static)
Text
(Instructions)
2
How to Allocate Main Memory to a Process?
3
Contiguous Memory Allocation
4
Contiguous Memory Allocation
Kenel Data
• Initial Idea: (PCB)
• Not effiecient.
Data
(Global &
Static)
Text
(Instructions)
5
Types of Contiguous Memory Allocation
Contiguous
Memory
Allocation
Variable or
Fixed or Static
Dynamic
Allocation
Allocation
6
Fixed or Static Partition Contiguous 0
Memory Allocation
1
2
• All sectors of memory are of same size (For eg: 1kb)
3
7
Variable (Dynamic) Partition Contiguous 0
Memory Allocation 1
2
• All sectors of memory are of different sizes.
• Say 1KB, 2 KB, some of 10KB and called as Hole. 3
4
• OS keeps a table which keeps track of which sector of
memory are free. 5
techniques. 8
Dynamic Storage Allocation Techniques
Dynamic
Allocation
9
First Fit
• Stop the search as soon as it finds a free hole that is large enough.
10
Example
Used Hole Used Hole Used Hole Used Hole Used Hole Used Hole
10 KB 10 KB 20 KB 30 KB 10 KB 5 KB 30 KB 20 KB 10 KB 15 KB 20 KB 20 KB
A B C D E F G H I J K L
11
Wrost Fit
• This strategy produces the largest leftover hole which may be more
useful than the smaller leftover hole from a best-fit approach.
12
Example
Used Hole Used Hole Used Hole Used Hole Used Hole Used Hole
10 KB 10 KB 20 KB 30 KB 10 KB 5 KB 30 KB 20 KB 10 KB 15 KB 20 KB 20 KB
A B C D E F G H I J K L
13
Best Fit
• We must search the entire list, unless the list is ordered by size.
14
Example
Used Hole Used Hole Used Hole Used Hole Used Hole Used Hole
10 KB 10 KB 20 KB 30 KB 10 KB 5 KB 30 KB 20 KB 10 KB 15 KB 20 KB 20 KB
A B C D E F G H I J K L
15
Next Fit
• In the first fit, since all the search starts from the beginning of
memory, holes at the beginning of memory are considered for
allocation more frequently.
• In the next fit, it keeps track of which hole was allocated.
• The next search starts at the last hole allocated, not at the beginning
of the memory.
• The next fit is a modified version of the ‘first fit’. It begins as
the first fit to find a free partition but when called next time it
starts searching from where it left off, not from the beginning
16
Example
Used Hole Used Hole Used Hole Used Hole Used Hole Used Hole
10 KB 10 KB 20 KB 30 KB 10 KB 5 KB 30 KB 20 KB 10 KB 15 KB 20 KB 20 KB
A B C D E F G H I J K L
17
Memory Fragmentation
18
Main Types of Fragmentation
Memory
Fragmentation
Internal External
Fragmentation Fragmentation
19
Internal Fragmentation
20
External Fragmentation
• This arises when free memory is separated into small blocks and is
inter separated by allocated memory.
21
Solutions for Fragmentation
Solutions
Non-
Compaction Contiguous
Allocation
22
Compaction
U F U U U F U F U U U U U U U F F F
s r s s s r s r s s s s s s s r r r
e e e e e e e e e e e e e e e e e e
d e d d d e d e d d d d d d d e e e
23
Compaction
• Processes are on one side and free space is on the other side in the
memory.
• During memory compaction:
• Block processes for compaction
• Move them towards lower address space
• Allocate memory for pending processes
• Resumed blocked processes
24
Non-Contiguous Allocation
Non-
Contiguous
Allocation
Paging Segmentation
25
Paging
26
Paging Model
Process Memory Main Memory
27
Paging: Address Translation
Process Memory Main Memory
30
Paging: Address Translation
Process Memory Main Memory
31
Paging: Hardware
30
Page Table
• To make usage more efficient the page table is kept in the main
memory & Page Table Base Register (PBTR)
31
Page Table
• Modified bit: is a flag associated with a memory page that indicates whether
the contents of the page have been modified (written to) since it was last
loaded into memory or since the bit was last cleared.
• Valid bit:is a flag associated with a page table entry that indicates whether the
corresponding page in memory is currently valid or not.
• Read-only bit:is a flag associated with a page table entry or a page in memory 32
that indicates whether the contents of the page are read-only or writable.
Example:
33
Answer
Paging: Advantages
• No external fragmentation.
• Swapping easy
35
Paging: Disadvantages
• Internal fragmentation
36
Solution to Access Time
37
Paging with TLB
38
More about TLB
• So, whenever context switch occurs, the entire content of the TLB is
flushed and deleted.
39
More about TLB
• If the page number is found, then it is TLB hit. Otherwise TLB miss.
40
More about TLB
41
Example:
42
Answer
EMAT=144 nanoseconds
Therefore, the effective memory access time is 144 nanoseconds with a TLB hit ratio of 80%.
Kenel Data
Beyond Paging (PCB)
Stack
Heap
Data
(Global &
Static)
Text
(Instructions)
44
What happened in Paging?
45
What happened in Paging?
46
User’s View
47
Segmentation
• A table store the information about all such segments called the segment
table.
• Segmentation supports user view of the memory.
48
Segmentation
49
Segmentation
50
Segmentation
51
Paged Segmentation
52