0% found this document useful (0 votes)
13 views55 pages

Chapter 3 - Memory Management

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views55 pages

Chapter 3 - Memory Management

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 55

Chapter 3: Memory Management

By: Adisu A. (MSc.)


College of Science and Technology

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 3: Memory Management

 Introduction
 Overview of Physical Memory and Memory Management
 Hardware Overlays
 Swapping
 Partitioning
 Paging and Segmentation
 Page Replacement and Replacement Policies
 Working Sets and Thrashing
 Caching

Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
Introduction to Memory Management
 Memory management refers to the process of controlling and
coordinating computer memory, ensuring that the available memory
resources are efficiently utilized by the operating system and
applications.

 It involves tasks such as allocation, tracking, and deallocation of


memory space to processes and data.

Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
Importance of Efficient Memory Utilization
 crucial for the overall performance and stability of a computer
system.

 ensures that processes have access to the required memory


resources without causing conflicts or resource exhaustion.

 Inefficient memory management can lead to issues such as memory


leaks, fragmentation, and performance degradation.

Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Overview of Memory Management Tasks
 Memory Allocation: assigning memory space to processes.
 Memory Deallocation: Reclaiming memory space that is no longer
needed by processes.
 Memory Protection: Enforcing access control mechanisms to
protect critical system data and prevent processes from interfering
with each other's memory space.
 Memory Organization: Structuring memory into logical units.
Techniques such as partitioning, paging, and segmentation.
 Memory Optimization: improves memory utilization. Techniques
such as caching, swapping, and compaction.

Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Physical Memory and Memory Management
 Physical memory (main memory or RAM), is the primary storage
location in a computer system where data and instructions are stored
temporarily for processing.
 It consists of a collection of memory cells, each uniquely identified by
an address, allowing the CPU to read from and write to specific
locations.
 Memory management is essential for effectively utilizing physical
memory resources to meet the demands of running processes.

Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
Key Components of Memory Management

Memory Hierarchy:
Consists of multiple levels of memory with varying speeds &

capacities, i.e., registers, cache, RAM, and disk storage.


Each level of the hierarchy serves a specific purpose and has unique

characteristics, such as speed, cost, and volatility.


Memory Addressing:
Includes physical addressing and logical addressing.
Physical addressing - accessing memory directly using physical

memory addresses
Logical addressing uses virtual memory addresses that are
translated to physical addresses by memory management unit (MMU).

Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Key Components of Memory Management

Memory Protection and Access Control:


Memory protection mechanisms - prevent unauthorized access to

memory regions and ensure data integrity and system security.


Access control mechanisms enforce permissions and privileges for

accessing specific memory locations, preventing processes from


accessing memory outside their allocated space.

Memory Organization Techniques:


Techniques such as partitioning, paging, and segmentation.

These techniques divide physical memory into manageable units to

facilitate memory allocation, and address translation.

Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Challenges in Memory Management

Fragmentation:
Occurs when memory becomes divided into small, unusable chunks

due to allocation and deallocation of memory blocks.


It leads to inefficient memory utilization and performance degradation if

not managed properly.

Resource Contention:
Multiple processes compete for limited memory resources, leading to

contention and potential deadlock situations.


Effective memory management strategies are required to mitigate it.

Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
Hardware Overlays
 A memory management technique used to allow a computer system
to address more memory than it actually has physically available.
 The system divides the available physical memory into multiple
fixed-size partitions or overlays.
 Each partition can hold a different program or data segment, and
only the portion of memory needed for the currently executing
program is loaded into the available partition.

Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Functionality of Hardware Overlays
 When a program switches, the operating system loads the required
program segments into the appropriate overlay partition, replacing
the previous contents.
 This allows the system to execute programs larger than the physical
memory capacity by swapping segments in and out of memory as
needed.
 It enables efficient memory usage by allowing different programs to
share the same physical memory space without conflicting with each
other.

Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Advantages of Hardware Overlays
 Efficient Memory Utilization: enable efficient use of physical
memory by allowing multiple programs to share memory space.
 Increased Program Size: Programs larger than the available
physical memory can be executed using overlays, as only the
required segments are loaded into memory at any given time.
 Reduced Memory Fragmentation: By dynamically loading program
segments as needed, hardware overlays help reduce memory
fragmentation compared to static partitioning schemes.

Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Implementation of Hardware Overlays
 Hardware support for overlays typically involves specialized
memory management hardware, such as memory controllers and
address translation units.
 The OS coordinates the loading and unloading of program segments
into overlay partitions.
 Overlays may be implemented using techniques such as dynamic
loading, where program segments are loaded into memory on-
demand, or swapping, where segments are swapped in and out of
memory as needed.

Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Limitations of Hardware Overlays
 Overhead: due to the need for additional memory management
hardware and loading and swapping program segments.
 Complexity: Implementing and managing overlays can be complex,
requiring careful coordination between the operating system and
hardware components.
 Limited Scalability: It may not be suitable for systems with rapidly
changing memory requirements or large-scale multiprocessing
environments.

Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Swapping
 Swapping is a memory management technique used by operating
systems to temporarily transfer inactive processes or parts of
processes from main memory (RAM) to secondary storage (disk).
 It allows the operating system to free up memory space for other
processes or data when physical memory becomes full or when a
process is idle for an extended period.

Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Functionality of Swapping
 When a process is swapped out, its entire address space, including
code, data, and stack, is transferred from RAM to a designated area
on the disk called the swap space or swap file.
 OS maintains a data structure called swap table to track the location
of swapped-out processes and manage the swapping process.
 Swapped-out processes remain in a suspended state until they are
brought back into main memory either
 a request from the process itself
 when the OS decides to prioritize their execution.

Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
Implementation of Swapping
 Swapping is implemented by the OS kernel using system calls and
kernel-level memory management routines.
 OS monitors memory usage and initiates swapping when available
memory becomes low.
 Swapping involves several steps:
 selecting processes for swapping
 transferring process data to disk
 updating process control blocks
 managing the swap space efficiently.

Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Advantages of Swapping
 Increased Memory Capacity: utilization of secondary storage as
virtual memory, increasing system memory capacity.
 Improved Process Management: OS manage processes more
efficiently by prioritizing active processes in main memory and
swapping out inactive or low-priority processes.
 Enhanced System Stability: prevent system crashes and out-of-
memory errors by dynamically managing memory resources.

Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
Limitations of Swapping
 Performance Overhead: data transfer time required between main
memory and secondary storages  delay in process execution.
 Disk I/O Bottleneck: Excessive swapping activity creates a disk I/O
bottleneck, particularly on systems with slow disk access times.
 Fragmentation: disk fragmentation over time as processes are
swapped in and out of disk space, potentially impacting disk
performance and efficiency.

Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
Partitioning
 Partitioning is a memory management technique used to divide the
available physical memory (RAM) into multiple fixed-size or
variable-size partitions, each serving as a separate memory region.
 Each partition can be assigned to a specific process, allowing
multiple processes to execute concurrently without interfering with
each other's memory space.

Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Types of Partitioning
Fixed Partitioning:
physical memory is divided into a predetermined number of partitions

of fixed size.
Each partition accommodate a single process, and the size of each

partition remains constant throughout the system's operation.


Simple to implement but may lead to inefficient memory utilization and

fragmentation, especially if processes vary widely in size.

Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Types of Partitioning

Variable (Dynamic) Partitioning (Dynamic):


partitions created and resized dynamically

based on memory needs of processes.


When a process requests memory, OS

allocates a suitable-sized partition from the


available free memory space.
flexible and efficient than fixed partitioning
complex memory management algorithms

required.

Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Allocation Methods in Partitioning

First Fit Allocation:


allocates the first available
partition that is large enough to
accommodate the process's
memory requirements.
simple and efficient but may

lead to fragmentation over time if


smaller partitions are left unused
between allocated partitions.

Operating System Concepts – 9th Edition 4.23 Silberschatz, Galvin and Gagne ©2013
Allocation Methods in Partitioning

Best Fit Allocation:


selects the smallest
available partition that is large
enough to accommodate the
process's memory
requirements.
minimizes fragmentation but

may lead to inefficient


memory utilization due to
small leftover partitions
(internal fragmentation).

Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
Allocation Methods in Partitioning

Worst Fit Allocation:


Selects the largest available

partition, regardless of the


process's memory
requirements.
creates larger leftover
partitions (external
fragmentation) but may be
suitable for processes with
varying memory demands.

Operating System Concepts – 9th Edition 4.25 Silberschatz, Galvin and Gagne ©2013
Fragmentation in Partitioning

Internal Fragmentation:
occurs when allocated memory
blocks are larger than necessary,
resulting in wasted memory space
within partitions.
Fixed-size partitions are particularly

susceptible to internal fragmentation.

Operating System Concepts – 9th Edition 4.26 Silberschatz, Galvin and Gagne ©2013
Fragmentation in Partitioning

External Fragmentation:
occurs when free memory blocks are scattered throughout the memory

space, making it challenging to allocate contiguous memory blocks to


processes.
Variable partitioning are susceptible to external fragmentation.

Operating System Concepts – 9th Edition 4.27 Silberschatz, Galvin and Gagne ©2013
Solutions to Fragmentation

Compaction:
rearranging memory contents to consolidate fragmented memory

blocks and create larger contiguous free memory blocks.


effective but additional overhead in systems with large memory sizes.

Memory Paging and Segmentation:


mitigate fragmentation by dividing memory into smaller, more
manageable units and managing memory allocation at a finer level.

Operating System Concepts – 9th Edition 4.28 Silberschatz, Galvin and Gagne ©2013
Paging and Segmentation
 memory management techniques used to overcome the limitations
of fixed-size partitioning and provide more flexible memory allocation
mechanisms.
 Both techniques divide the logical address space of a process into
smaller, manageable units, allowing the operating system to allocate
memory dynamically as needed.

Operating System Concepts – 9th Edition 4.29 Silberschatz, Galvin and Gagne ©2013
Paging
 divides logical address space of
a process into fixed-size blocks
called pages, typically ranging
from 4 KB to 64 KB in size.
 Physical memory is divided into
fixed-size blocks called frames,
which are same size as pages.
 OS maintains a data structure
called page table to map logical
pages to physical frames,
enabling efficient address
translation.

Operating System Concepts – 9th Edition 4.30 Silberschatz, Galvin and Gagne ©2013
Benefits of Paging

Efficient Memory Utilization:


reduces internal fragmentation than fixed-size partitioning since

processes use only the memory they require, not entire partitions.

Simplified Memory Management:


OS manages memory allocation and deallocation at page level, rather

than at the partition level.

Memory Protection:
assigns each page a protection attribute, such as read-only or read-

write, preventing unauthorized access to memory regions.

Operating System Concepts – 9th Edition 4.31 Silberschatz, Galvin and Gagne ©2013
Paging Mechanism

Address Translation:
When a process accesses a memory location, the logical address is

divided into a page number and an offset within the page.


The page number is used as an index into the page table to retrieve

the corresponding physical frame number, and the offset is added to the
frame number to calculate the physical address.

Operating System Concepts – 9th Edition 4.32 Silberschatz, Galvin and Gagne ©2013
Paging Mechanism

Page Replacement:
Page replacement algorithms, such as FIFO (First-In-First-Out), LRU

(Least Recently Used), and Optimal, are used to select which page to
evict from memory when a page fault occurs and a new page needs to
be brought in.

Operating System Concepts – 9th Edition 4.33 Silberschatz, Galvin and Gagne ©2013
Segmentation
 divides the logical address space of a process into variable-size
segments, each representing a logical unit of the program, such as
code, data, stack, and heap.
 Segments can vary in size and do not need to be contiguous in
physical memory, allowing for more flexible memory allocation.

Operating System Concepts – 9th Edition 4.34 Silberschatz, Galvin and Gagne ©2013
Benefits of Segmentation

Logical Organization:
provides a logical organization of memory by dividing a program into

meaningful segments, such as code, data, and stack.

Flexibility:
allows processes to allocate memory dynamically as needed

Sharing and Protection:


enables memory sharing and protection by assigning different access

permissions to each segment

Operating System Concepts – 9th Edition 4.35 Silberschatz, Galvin and Gagne ©2013
Segmentation Mechanism

Address Translation:
requires two levels of address

translation: a segment table to


map logical segment numbers to
base addresses and limits, and a
page table within each segment
to map logical pages to physical
frames.

Operating System Concepts – 9th Edition 4.36 Silberschatz, Galvin and Gagne ©2013
Segmentation Mechanism

Protection and Access Control:


enables memory protection and access control by assigning access

permissions (e.g., read-only, read-write) to each segment and enforcing


these permissions during address translation.

Operating System Concepts – 9th Edition 4.37 Silberschatz, Galvin and Gagne ©2013
Page Replacement & Replacement Policies
 Page replacement is a critical aspect of virtual memory
management, where the operating system selects a page in main
memory for eviction when additional memory is needed.
 When a process accesses a page that is not currently in main
memory (Page fault), OS decides which page to remove from
memory to make room for the new page.

Operating System Concepts – 9th Edition 4.38 Silberschatz, Galvin and Gagne ©2013
Need for Page Replacement

Limited Physical Memory:


when physical memory is insufficient to hold all the pages required by

active processes simultaneously.

Demand Paging:
A virtual memory technique where OS loads pages into memory only

when they are needed.

Operating System Concepts – 9th Edition 4.39 Silberschatz, Galvin and Gagne ©2013
Replacement Policies
 determine which page to evict from memory when a page fault
occurs.
 Various replacement policies prioritize different criteria, such as
minimizing page faults, maximizing resource utilization, or optimizing
system performance.

Operating System Concepts – 9th Edition 4.40 Silberschatz, Galvin and Gagne ©2013
Popular Replacement Policies

First-In, First-Out (FIFO)


FIFO replaces the page that has been in memory the longest (oldest)

without regard to how frequently or infrequently it is used.


Advantages:

 Simple to implement and understand.


 Guarantees fairness, as each page has an equal chance of
being evicted.

Operating System Concepts – 9th Edition 4.41 Silberschatz, Galvin and Gagne ©2013
Popular Replacement Policies

Least Recently Used (LRU)


LRU replaces the page that has not been accessed for the longest

time.
Advantages:

 Generally effective in reducing the number of page faults by


prioritizing pages that are less likely to be accessed again soon.

Operating System Concepts – 9th Edition 4.42 Silberschatz, Galvin and Gagne ©2013
Popular Replacement Policies

Optimal Page Replacement (OPR)


OPR replaces the page that will not be used for the longest time in the

future.
Advantages:

 Minimizes the number of page faults.


Disadvantages:

 Impractical for real-world systems, as it requires knowledge of


future memory access patterns, which is generally not feasible.

Operating System Concepts – 9th Edition 4.43 Silberschatz, Galvin and Gagne ©2013
Comparison of Replacement Policies

Page Fault Rate:


Each replacement policy may result in a different page fault rate

depending on the workload and memory access patterns.

Overhead:
Different policies incur varying overhead in terms of computational

complexity and memory usage.

Optimality:
Optimal page replacement provides the best theoretical performance

but is impractical for real-world systems due to its high implementation


complexity.

Operating System Concepts – 9th Edition 4.44 Silberschatz, Galvin and Gagne ©2013
Working Sets and Thrashing

Working Set
Working set of a process is the set of pages that the process is actively

using or referencing during a particular period of time.


It represents process's memory access patterns, indicating the pages

that are currently needed for efficient execution.

Working Set
Model

Importance of Working Sets


ensuring working set of a process remains in main memory, OS can

minimize page faults and maintain optimal performance.

Operating System Concepts – 9th Edition 4.45 Silberschatz, Galvin and Gagne ©2013
Thrashing
 occurs when a computer system spends a significant amount of time
swapping pages between main memory and secondary storage,
resulting in excessive disk I/O and degraded system performance.
 It typically occurs when the system is overloaded with too many
processes competing for limited physical memory, causing frequent
page faults and excessive swapping activity.

Operating System Concepts – 9th Edition 4.46 Silberschatz, Galvin and Gagne ©2013
Causes of Thrashing

Insufficient Physical Memory:


occurs when physical memory is insufficient to hold the working sets of

all active processes simultaneously.

Over-commitment of Memory:
the system allocates more memory to processes than is physically

available which intensify thrashing.

Poor Memory Management:


poorly designed page replacement strategies contribute to thrashing

by failing to prioritize critical memory pages.

Operating System Concepts – 9th Edition 4.47 Silberschatz, Galvin and Gagne ©2013
Mitigation of Thrashing

Increase Physical Memory:


Adding more physical memory to the system reduces thrashing

providing more space to the working sets of active processes.

Adjust Memory Allocation Policies:


adjusting page replacement strategies or limiting overcommitment of

memory to mitigate thrashing.

Identify and Prioritize Critical Processes:


prevent thrashing by ensuring that essential system components

remain in main memory.

Operating System Concepts – 9th Edition 4.48 Silberschatz, Galvin and Gagne ©2013
Working Sets vs. Thrashing
 closely related concepts in memory management
 working sets – the active memory needs of processes
 thrashing – the detrimental effects of insufficient memory resources.
 managing working sets of processes effectively can prevent
thrashing and maintain optimal system performance.

Operating System Concepts – 9th Edition 4.49 Silberschatz, Galvin and Gagne ©2013
Caching
 Caching is a memory management technique used to store
frequently accessed data or instructions in a fast, temporary storage
location known as a cache.
 The cache serves as a buffer between the CPU and slower, larger
main memory (RAM) or secondary storage (e.g., disk), speeding up
access to frequently used data and improving overall system
performance.

Operating System Concepts – 9th Edition 4.50 Silberschatz, Galvin and Gagne ©2013
Types of Caches

CPU Cache
CPU cache, also known as processor cache, is a small, high-speed

memory directly integrated into the CPU chip.


It consists of multiple levels, including L1 cache (level 1), L2 cache

(level 2), and sometimes L3 cache (level 3), with each level offering
progressively larger capacity but slower access speed.

Operating System Concepts – 9th Edition 4.51 Silberschatz, Galvin and Gagne ©2013
Types of Caches

Disk Cache
A buffer memory used to temporarily store data read from or written to

a disk drive.
It helps reduce disk I/O latency by prefetching data blocks into memory

and buffering write operations.

Web Browser Cache


stores copies of web pages, images, and other web resources locally

on the user's device.


Enables browser to load web content quickly by retrieving cached

resources from local storage than the internet.

Operating System Concepts – 9th Edition 4.52 Silberschatz, Galvin and Gagne ©2013
Cache Management

Cache Replacement Policies


determine how the cache decides which data to evict when the cache

becomes full and needs to make room for new data.


Popular policies include LRU, FIFO, and Random Replacement.

Cache Coherency
ensures multiple cache copies of the same data remain consistent and

synchronized with each other and with the main memory.

Cache Prefetching
proactively loads data into the cache before it is requested by the

CPU.

Operating System Concepts – 9th Edition 4.53 Silberschatz, Galvin and Gagne ©2013
Challenges and Considerations

Cache Pollution
occurs when cache is filled with unnecessary data, reducing the

effectiveness of caching and increasing cache miss rates.

Cache Consistency
Maintaining cache consistency across multiple cache levels can be

challenging, particularly in distributed or multi-core systems.

Cache Size and Configuration


Balancing cache size with power consumption, and performance goals

is essential for maximizing the benefits of caching.

Operating System Concepts – 9th Edition 4.54 Silberschatz, Galvin and Gagne ©2013
End of Chapter 3

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013

You might also like