Chapter 3 - Memory Management
Chapter 3 - Memory Management
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.
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.
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 &
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
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
Resource Contention:
Multiple processes compete for limited memory resources, leading to
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
Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Types of Partitioning
required.
Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Allocation Methods in Partitioning
Operating System Concepts – 9th Edition 4.23 Silberschatz, Galvin and Gagne ©2013
Allocation Methods in Partitioning
Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
Allocation Methods in Partitioning
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
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
Operating System Concepts – 9th Edition 4.27 Silberschatz, Galvin and Gagne ©2013
Solutions to Fragmentation
Compaction:
rearranging memory contents to consolidate fragmented memory
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
processes use only the memory they require, not entire partitions.
Memory Protection:
assigns each page a protection attribute, such as read-only or read-
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
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
Flexibility:
allows processes to allocate memory dynamically as needed
Operating System Concepts – 9th Edition 4.35 Silberschatz, Galvin and Gagne ©2013
Segmentation Mechanism
Address Translation:
requires two levels of address
Operating System Concepts – 9th Edition 4.36 Silberschatz, Galvin and Gagne ©2013
Segmentation Mechanism
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
Demand Paging:
A virtual memory technique where OS loads pages into memory only
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
Operating System Concepts – 9th Edition 4.41 Silberschatz, Galvin and Gagne ©2013
Popular Replacement Policies
time.
Advantages:
Operating System Concepts – 9th Edition 4.42 Silberschatz, Galvin and Gagne ©2013
Popular Replacement Policies
future.
Advantages:
Operating System Concepts – 9th Edition 4.43 Silberschatz, Galvin and Gagne ©2013
Comparison of Replacement Policies
Overhead:
Different policies incur varying overhead in terms of computational
Optimality:
Optimal page replacement provides the best theoretical performance
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
Working Set
Model
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
Over-commitment of Memory:
the system allocates more memory to processes than is physically
Operating System Concepts – 9th Edition 4.47 Silberschatz, Galvin and Gagne ©2013
Mitigation of Thrashing
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
(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
Operating System Concepts – 9th Edition 4.52 Silberschatz, Galvin and Gagne ©2013
Cache Management
Cache Coherency
ensures multiple cache copies of the same data remain consistent and
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
Cache Consistency
Maintaining cache consistency across multiple cache levels can be
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