Module 4 Memory Management
Module 4 Memory Management
Management
Curriculum
• Basic Concepts of Memory Management: Swapping, Memory
Allocation strategy, Paging, Structure of Page Table, Segmentation,
TLB.
• Basic Concepts of Virtual Memory, Demand Paging, Copy-on Write,
Page Replacement Algorithms, Thrashing.
• While registers and cache memory are closer to the processor that a
processor can directly access, they are very expensive and thus of
very small capacity.
• They cannot accommodate either the operating system or other
programs that are executed by the processor.
• Main memory is the furthest memory unit from a processor that it
can directly access which can accommodate the OS as well as user
applications during their execution.
• Processors make all memory references with respect to this memory.
• Main memory is volatile in nature - it keeps the code and data (both
for system and application programs) as long as the computer is on.
• Both the OS and other programs need to be loaded on the main
memory from the secondary or tertiary memory after each start-up
and/or execution.
Memory Management
It is the process of controlling and organising a computer’s memory by
allocating portions, called blocks, to different executing programmes to
improve the overall system performance. It performs following
functions
• To keep track of all memory locations-free or allocated and if
allocated, to which process and how much.
• To decide the memory allocation policy i.e. which process should get
how much memory, when and where.
• To use various techniques and algorithms to allocate and de-allocate
memory locations.
Memory Image
• A memory image (RAM image or memory dump) is a file that
contains the exact state of system memory.
• It includes:
➢Running processes and their data
➢Operating system kernel data
➢Open network connections
➢Passwords or encryption keys temporarily stored in RAM
➢Stacks, heaps, and program instructions
Swapping
• Swapping in the operating system is a memory management scheme
that temporarily swaps out an idle or blocked process from the main
memory to secondary memory which ensures proper memory
utilization and memory availability for those processes which are
ready to be executed.
• Swapping is a memory management technique in which a process is
temporarily moved from main memory (RAM) to secondary storage
(disk) and vice versa. This allows the operating system to manage
limited RAM effectively and run multiple processes concurrently in a
multiprogramming environment.
Swapping
Roll Out
If a higher priority process arrives and wants service, the memory
manager can swap out the lower priority process so that it can load
higher priority process and execute it.
Roll in
When the higher priority process finishes, the lower priority process
can be swapped back in and continued.
Swapping
Swap In:
• The method of removing a process from secondary memory (Hard Drive)
and restoring it to the main memory (RAM ) for execution is known as
the Swap In method.
Swap Out:
• It is a method of bringing out a process from the main memory(RAM) and
sending it to the secondary memory(hard drive) so that the processes with
higher priority or more memory consumption will be executed known as
the Swap Out method.
• The secondary disc also known as backing store is a fast disc. It must be
large enough to accommodate copies of all memory images.
• The system maintains a ready queue of all the processes whose memory
images are on the backing store or in memory and are ready to run.
Swapping
Advantages
• Helps in achieving the goal of Maximum CPU Utilization.
• Ensures proper memory availability for every process that needs to be executed.
• Helps avoid the problem of process starvation means a process should not take
much time for execution so that the next process should be executed.
• CPU can perform various tasks simultaneously with the help of swapping so that
processes do not have to wait much longer before execution.
• Ensures proper RAM(main memory) utilization.
• Creates a dedicated disk partition in the hard drive for swapped processes which
is called swap space.
• Is an economical process.
• Can be applied on priority-based process scheduling where a high
priority process is swapped in and a low priority process is swapped out which
improves the performance.
Disadvantages
• If the system deals with power-cut during bulky swapping activity
then the user may lose all the information which is related to the
program.
• If the swapping method uses an algorithm that is not up to the mark
then the number of page faults can be increased and therefore this
decreases the complete performance.
• There may be inefficiency in a case when there is some common
resource used by the processes that are participating in the swapping
process.
Techniques in Memory Allocation
Contiguous Memory Allocation
• It is a memory management technique that involves allocating a
process to the entire contiguous block of the main memory it requires
to execute.
• The process is loaded into a single continuous chunk of memory.
• All instructions and data of a process are stored in adjacent memory
locations.
Single Contiguous Memory Allocation
• The main memory is divided into two parts:
➢One part is reserved for the Operating System
➢The remaining part is allocated to a single user processes
• Characteristics
• Only one user process can reside in memory at a time
• The operating system occupies a fixed portion of memory
• No multiprogramming is possible
• Simple to implement and manage
Single Contiguous Memory Allocation
Advantages
• Simple memory management
• No fragmentation issues
Disadvantages
• Poor memory utilization
• No support for multitasking or multiprogramming
Partitioned Memory Allocation
• Main memory is divided into multiple contiguous partitions, and each partition
can hold one process.
• This technique supports multiprogramming.
• It is further partitioned into:
Fixed Partition Allocation
• Memory is divided into a fixed number of partitions
• Each partition has a fixed size
• Each partition can store only one process
• Leads to internal fragmentation
• Once partitions are defined operating system keeps track of the status
of memory partitions it is done through a data structure called a partition
table.
Fixed Partition Allocation
Advantages:
• Simple to implement as all blocks are of same size. Implemented
by dividing the memory into fixed blocks and assign processes to
them.
• Easy to keep track of how many blocks of memory are left, which
in turn decides how many more processes can be given space in
the memory.
• As at a time multiple processes can be kept in the memory, this
scheme can be implemented in a system that needs
multiprogramming.
Disadvantages:
• As the size of the blocks is fixed, a process that has a greater size
than the block cannot be allocated space.
• The size of the blocks decides the degree of multiprogramming,
and only that many processes can remain in the memory at once
as the number of blocks.
• If the size of the block is greater than the size of the process, we
have no other choice but to assign the process to this block, but
this will lead to much empty space left behind in the block. This
empty space could've been used to accommodate a different
process. This is called internal fragmentation. Hence, this
technique may lead to space wastage.
Variable Partition Allocation (Dynamic Partioning)
• Memory is divided into partitions dynamically based on process size
• Reduces internal fragmentation
• Suffers from external fragmentation
• Advantages
• Supports multiprogramming
• Better memory utilization compared to single contiguous allocation
• Disadvantages
• Fragmentation issues
• Complex memory management compared to single contiguous
allocation
Solution
• Allocate P1 (212KB) => B1 Too Small => B2 is large enough => Allocate P1 to B2.
Remaining 500 − 212 = 288 KB
• Allocate P2 (417KB) => B3 , B4 Too Small => B5 is large enough => Allocate P2 to B5.
Remaining 600 − 417 = 183 KB
• Allocate P3 (112 KB) => B1 Too Small => B2 288KB remaining => Allocate P3 to B2.
Best Fit
• The OS searches through the list of free blocks of memory to find the block
that is closest in size to the memory request from the process.
• Once a suitable block is found, the operating system splits the block into
two parts: the portion that will be allocated to the process, and the
remaining free block.
Advantages:
• Reduced External Fragmentation − By allocating the smallest possible
block to a process, the Best Fit Algorithm helps in reducing external
fragmentation.
• Efficient Memory Utilization − The Best Fit Algorithm ensures that memory
is utilized efficiently by minimizing the amount of wasted space after
allocation.
• Best for Small Processes − The Best Fit Algorithm is considered to be more
suitable for small processes.
Best Fit
Disadvantages:
• High Search Time − The Best Fit Algorithm requires searching the
entire memory for the smallest suitable block, which can increase
allocation time.
• Fragmentation − Even though it reduces external fragmentation, it
can cause internal fragmentation. Because processes may not fully
utilize the allocated blocks.
• Not Suitable for Large Processes − The Best Fit Algorithm may
struggle to find suitable blocks for larger processes. This leads to
more "Not Allocated" statuses.
e.g. Available Memory Blocks Process Requesting Memory
Block Size Block Size
B1 100 KB P1 212 KB
B2 500 KB P2 417 KB
B3 200 KB P3 112 KB
B4 300 KB
B5 600 KB
• Allocate P1 (212KB) => Smallest Suitable Block B4=> Allocate P1 to B4. Remaining space =
300 − 212 = 88KB
• Allocate P2 (417KB) => Smallest Suitable Block B2=> Allocate P2 to B2. Remaining space =
500 - 417 = 83KB
• Allocate P3 (112 KB) => Smallest Suitable Block B3=> Allocate P3 to B3. Remaining space =
200 - 112 = 88KB
Worst Fit
• The worst-fit algorithm searches for the largest free partition and
allocates the process to it. This algorithm is designed to leave the
largest possible free partition for future use.
Advantages:
• Reduces Chances of Small Fragments: By allocating the largest
available block, worst fit leaves larger leftover fragments, which are
more likely to be useful for future allocations.
• Simple to Implement: The logic is straightforward: find the largest
block that can fit the process and allocate it.
• Efficient for Large Processes: It always looks for the biggest space,
large processes may find better fits compared to other strategies like
Best Fit or First Fit.
Worst Fit
Disadvantages:
• External Fragmentation: Worst-Fit Allocation can lead to
significant external fragmentation. By allocating the largest available
block, it often leaves behind many smaller, unusable fragments of
memory.
• Inefficient Memory Usage: Allocating the largest block to a process,
even if it's much larger than required, can lead to inefficient memory
usage.
• Lower Allocation Time: Worst-Fit requires the operating system to
search for the largest available block, which can be slower than
methods like First-Fit or Next-Fit, especially if there are many free
blocks.
e.g. Available Memory Blocks Process Requesting Memory
Block Size Block Size
B1 100 KB P1 212 KB
B2 500 KB P2 417 KB
B3 200 KB P3 112 KB
B4 300 KB
B5 600 KB
• Allocate P1 (212KB) => Largest Block B5=> Allocate P1 to B5. Remaining space = 600 − 212
= 388KB
• Allocate P2 (417KB) => Largest Block B2=> Allocate P2 to B2. Remaining space = 500 - 417
= 83KB
• Allocate P3 (112 KB) => Largest Block (388KB) => Allocate P3. Remaining space = 388 - 112
= 276KB
Next Fit
• The Next Fit algorithm is a modified version of the First Fit memory
allocation technique. While the First Fit algorithm always starts searching
from the beginning of the memory block list for each new process, the
Next Fit algorithm optimizes this behavior by continuing the search from
where it last left off.
Advantages over First Fit:
• Reduces fragmentation near the beginning: Unlike First Fit, which
continuously fills the initial part of memory, Next Fit distributes allocations
more evenly across the memory space.
• Improved performance: Since it doesn’t restart the search every time, the
average time to find a free block is reduced, making it faster than First Fit
and Best Fit in many scenarios.
• Better load balancing: Memory usage gets spread more uniformly,
reducing the chances of creating too many small fragments in a specific
region.
Worst Fit
Disadvantages:
• External Fragmentation: Still susceptible to external fragmentation
• Poor Memory Utilization: Because it continues searching from the
last position, it may skip suitable free blocks earlier in memory.
• Slower Than First Fit (In Some Cases): If the last allocation point is
near the end of memory, the algorithm may need to scan a large
portion before finding a suitable block.
• Not Optimal for Large Requests: Large memory requests may fail
even when enough total memory is available — because free space is
scattered into small pieces.
e.g. Available Memory Blocks Process Requesting Memory
Block Size Block Size
B1 100 KB P1 212 KB
B2 500 KB P2 417 KB
B3 200 KB P3 112 KB
B4 300 KB
B5 600 KB
• Allocate P1 (212KB) => B1 not suitable Block B2 is suitable => Allocate P1 to B2.
Remaining space = 500 − 212 = 288KB
• Allocate P2 (417KB) B2, B3, B4 not suitable=> Allocate P2 to B5.
Remaining space = 600 - 417 = 183KB
• Allocate P3 (112 KB) => Block B5 183KB still available Allocate P3 to remaining memory
space in B5 . Remaining space = 183 - 112 = 71KB
Non-Contiguous Memory Allocation
• In this technique a process is divided into smaller parts and these parts are
stored in different, non-adjacent locations in main memory.
• Features
➢A process can be stored in multiple memory locations
➢Improves utilization of available memory
➢Reduces external fragmentation
➢Requires address translation using hardware support (MMU)
Primary techniques
1. Paging
2. Segmentation
3. Segmentation with Paging (Paged Segmentation)
Advantages
• Better memory utilization
• Supports large programs
• Eliminates the need for contiguous free memory
Disadvantages
• More complex than contiguous allocation
• Additional overhead for address translation
• Requires extra memory for tables (page table / segment
table)
Paging
• Paging is a memory management scheme that permits the physical
address space of a process to be non contiguous.
• Physical memory is broken into fixed size blocks called frames.
• Logical memory is broken into blocks of the same size called pages.
• Paging is the process of moving parts of a program, called pages, from
secondary storage into the main memory (RAM).
• When a process is to be executed its pages are loaded into any
available memory frames from the backing store.
Paging Hardware
Paging Hardware
• Every address generated by the CPU is divided into two parts: a page
number (p) and a page offset (d).
• The page number is used as an index into a per-process page table.
• The page table contains the base address of each page in physical
memory.
• The offset is the location within the frame being referenced.
• The base address of the frame is combined with the page offset to
define the physical memory address that is sent to the memory unit.
Paging Model of logical and physical memory
Hardware Support
• Each OS has its own methods for storing page tables.
• Hardware implementation of the page table can be done in several
ways.
• 1. The page table is implemented as a set of dedicated registers.
• These registers should be built with very high-speed logic to make the
paging-address translation efficient.
• The use of registers for the page table is satisfactory if the page table
is reasonably small
Hardware Support
• 2. The page table is kept in the main memory and the Page Table Base
Register (PTBR) points to the page table.
• Disadvantage:
• Two memory accesses are needed to access a byte. One for page
table entry and other for physical memory calculation
• Solution: Use a special small fast- lookup hardware cache called
Translation Look-aside Buffer (TLB)
Translation Look-aside Buffer (TLB)
• The TLB is associative, high-speed memory. Each entry in the TLB consists of
two parts:
• a key (or tag) and a value.
• The TLB contains only a few of the page table entries.
• When a logical address is generated by CPU, its page number is presented to
the TLB.
• If the page number is found, its frame number is immediately available and is
used to access memory. (TLB Hit)
• TLB Miss: If the page number is not in TLB, a memory reference to the page
table must be made.
• HIT Ratio: The percentage of times that the page number of interest is found
in the TLB is called the HIT ratio.
Paging Hardware with TLB
Valid /Invalid bit
Translation Look-aside Buffer in Paging
• For each process page table will be created which contains page table
entries.
• It contains information like frame number, valid / invalid bit,
protection bit etc.
• Valid/Invalid bit:
• When this bit is set to valid, the associated page is in the process’s
logical address space and is thus a legal (or valid) page.
• When the bit is set to invalid, the page is not in the process’s logical
address space. Illegal addresses are trapped by use of the valid–
invalid bit.
Translation Lookaside Buffer in Paging
• Protection bit: Memory protection in a paged environment is
accomplished by protection bits associated with each frame. One bit
can define a page to be read–write or read-only.
• Every reference to memory goes through the page table to find the
correct frame number.
• At the same time that the physical address is being computed, the
protection bits can be checked to verify that no writes are being
made to a read-only page.
• An attempt to write to a read-only page causes a hardware trap to the
operating system
Translation Lookaside Buffer in Paging
• Given a logical address, the CPU examines the TLB. If the page table
entry is present (TLB Hit), the frame number is retrieved and the real
address is formed.
• If a page table entry is not found in the TLB (TLB Miss), the page
number is used as index and matched to page table residing in the
main memory.
• If the entry is not found in the main memory also then Page Fault is
issued.
• The TLB is updated with new page table entry. If space is not
available, one of the page replacement techniques(LRU, FIFO,….) is
used
Effective Memory Access Time
• Effective Memory Access Time = h *(c + m) + (1-h) *(c +2m)
Where
h: Hit ratio of TLB
m: Memory Access Time
c: TLB Access Time
Structure of Page Table
Three most common techniques for structuring the page tables
1. Hierarchical Paging
2. Hashed Page Tables
3. Inverted Page Tables
Hierarchical Paging
• Modern computer systems support a large logical address space
• (232 to 264). In such an environment, the page table itself becomes
excessively large.
• It becomes difficult to allocate page table contiguously in main
memory.
• Solution: Divide the page table into smaller pieces.
• One way is to use a two-level paging algorithm, in which the page
table itself is also paged
Two-way Page Table
Logical Address
• Let p be the probability of a page fault (0< p < 1). Then the effective
access time is
• Effective access time = (1 - p) x memory access time + p x page fault
time
• Three major components of the page-fault service time:
• 1. Service the page-fault interrupt.
• 2. Read in the page.
• 3. Restart the process.
Process Creation
Page Fault= 9
page reference string: 0, 1, 2, 3, 0, 1, 4, 0, 1,
2, 3, 4 with no of frame 3,
3 3 3 3 3 3 2 2 2
2 2 2 2 2 2 1 1 1 1
1 1 1 1 1 1 0 0 0 0 4
0 0 0 0 0 0 4 4 4 4 3 3
H H
Page Fault= 10
Advantages of FIFO
• Simplicity: Simple and easy-to-implement algorithm. It does not require
complex data structures or sophisticated calculations.
• Fairness: All pages have an equal chance of being replaced. The oldest
page is replaced first, regardless of its usage frequency or importance.
• No starvation: Does not suffer from starvation, which means that a page
will eventually be replaced if it has been in memory for a long time, even if
it is frequently used.
• Predictability: Predictable in the sense that the order in which pages are
replaced is deterministic and does not depend on the page usage patterns
or history.
• Low overhead: Minimal overhead because it only needs to maintain a
simple queue of pages in memory, making it a good choice for systems with
limited resources.
Disadvantages of FIFO
• Poor performance: May not provide optimal performance because it does not take into account the
usage patterns or importance of the pages. It may result in frequent page faults and unnecessary disk
I/O operations, especially if the workload is complex or memory demands are high.
• Inefficient use of memory: Can lead to inefficient use of memory because it replaces the oldest page,
regardless of its usage frequency or importance. As a result, some pages that are rarely used or not
important may occupy memory for a long time, while other more critical pages may be swapped out
frequently.
• Susceptibility to thrashing: May be susceptible to thrashing, which occurs when the system spends a
significant amount of time swapping pages in and out of memory without making progress on the
actual workload. This happens when the number of pages needed by the workload exceeds the
available physical memory, and the FIFO algorithm does not efficiently manage the page swapping.
• Unfairness: May not be fair from the perspective of the workload or the user. Some pages may be
more important than others, and replacing them first can lead to degraded performance or even
application crashes.
• No consideration of future usage: Does not consider the future usage of pages, which means that it
may replace a page that will be needed again soon, leading to increased page faults and decreased
performance.
Optimal Page Replacement Algorithm
• This algorithm states that: Replace that page which will not be used
for longest period of time i.e. future knowledge of reference string is
required.
page reference string:
5, 0, 1, 0, 2, 3, 0, 2, 4, 3, 3, 2, 0, 2, 1, 2, 7, 0, 1, 1, 0
Number of Page frames = 3
1 1 1 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1
0 0 0 0 0 0 0 4 4 4 4 0 0 0 0 0 0 0 0 0
5 5 5 5 2 2 2 2 2 2 2 2 2 2 2 2 7 7 7 7 7
H H H H H H H H H H H H