Memory Management
• Memory management is a critical aspect of
operating systems that ensures efficient use of
the computer's memory resources. It controls
how memory is allocated and deallocated to
processes, which is key to both performance and
stability. Below is a detailed overview of the
various components and techniques involved in
memory management.
3 Criteria to Choose Memory
1.Size 2.Access Time 3.Cost
• Register:It is a temporary storage are built in a cpu
Access time of register is below.
• Cache Memory:It is a high speed memory.the
purpose of cache memory is to stored those program
that are seperately used
• Secondary Memory:It allows user to store data that
can be easily retrived .This Memory cannot be
direcly used.It can be accessed only by the main
memory.
For e.g Magnetic tape,Magnetic disk,Hard disk
Memory Management Techniques
Memory management techniques are methods used by an operating system to efficiently allocate, utilize,
and manage memory resources for processes. Various techniques help the operating system manage
memory effectively
Contiguous Memory Allocation
Contiguous memory allocation is a memory management method where each process is given a single,
continuous block of memory. This means all the data for a process is stored in adjacent memory locations.
Non-Contiguous Memory Allocation
This method allows processes to be broken into smaller parts, which are placed in different, non-adjacent
memory locations. Techniques for non-contiguous memory allocation include:
Paging: The process is divided into fixed-size blocks called "pages," and the memory is divided into blocks of
the same size called "frames." The operating system keeps a page table to map logical pages to physical
frames.
Segmentation: The process is divided into segments of varying sizes, such as code, data, stack, etc. The
operating system maintains a segment table to map logical segments to physical memory.
Memory allocation
strategies
Contiguous Memory Allocation
Fixed Partition Scheme
Variable Partition Scheme
Allocation Algorithms (First-Fit, Best-Fit, Worst-Fit):
These algorithms manage free space in variable partition schemes.
First Fit: The algorithm allocates a process to the first available memory
block that is large enough.
Best Fit: The algorithm allocates a process to the smallest available
memory block that fits the process.
Worst Fit: The algorithm allocates a process to the largest available
memory block, aiming to leave a large, more usable free block.
Memory partitioning
• Memory partitioning is an operating system technique for dividing a
computer's main memory into several independent sections, or
"partitions," to store resident programs and processes. Common
methods include fixed partitioning, which uses predefined, fixed-size
partitions, and dynamic/variable partitioning, which creates variable-
sized partitions to fit process needs more efficiently but risks external
fragmentation
Fixed and dynamic partitions
Paging
Paging is an operating system (OS) memory management
technique that divides a process's logical memory into fixed-size
blocks called pages and physical memory into fixed-size blocks
called frames. Pages are loaded from secondary storage into
available frames in main memory as needed, even if they are not
contiguous, which eliminates external fragmentation and allows
more processes to run concurrently. This mechanism, often
combined with virtual memory, enables programs larger than
physical RAM to run by allowing data to be swapped between RAM
and disk
Structure of paging table
A page table's structure consists of an array of Page Table
Entries (PTEs) that map a process's virtual pages to physical
frames in memory, with each PTE containing a frame number,
present/valid bit, and other status bits (like dirty or accessed).
Common page table structures that address scalability include
hierarchical (multi-level) page tables, which divide the address
space across multiple tables, hashed page tables that use
hashing to find entries, and inverted page tables, which are
indexed by frame number instead of page number.
Segmentation
Segmentation is a memory management technique in operating
systems that divides a program into logical, variable-sized units called
segments, such as code, data, and stack. Each segment is managed
independently by the operating system, which maintains a segment
table containing the base address and size for each segment to map
logical addresses to physical memory. This logical view of memory
improves organization, allows for better protection, and can lead to
more efficient memory utilization by matching segment sizes to
program component needs
Virtual memory
Virtual memory is an operating system technique that
creates the illusion of a larger main memory (RAM) than
physically available by using a portion of disk storage. It
allows for more, and larger, programs to run by only
loading the necessary parts of a process into RAM,
moving unused data to the disk, and bringing it back
when needed. This is implemented using mechanisms
like demand paging, which creates a mapping of virtual
addresses to physical addresses using a
Memory Management Unit (MMU) and a page table
Virtual memory provides the illusion of a larger main memory than physically
available, and Demand Paging is a technique that implements this illusion by only
loading pages of a program into physical memory when they are actually needed by
the CPU, triggered by a "page fault.
Background: Virtual Memory
Logical vs. Physical Address Space:
Virtual memory allows programs to use a larger, continuous address space (logical)
than the physical RAM available.
Enhanced Multitasking:
It enables more applications to run simultaneously or allows single applications to be
larger than the physical memory, as only the required parts of the programs are
loaded into RAM.
Address Translation:
The operating system and hardware work together to map the program's logical
addresses to physical addresses in RAM, using page tables to manage this translation
Page replacement
Page replacement algorithms like FIFO and LRU manage
memory by deciding which page to remove when new data
is needed, with FIFO removing the oldest page and LRU
removing the least recently used page. The Optimal
algorithm, which replaces the page not used for the
longest future time, serves as a theoretical ideal. Thrashing
is a system state of excessive swapping between memory
and disk due to frequent page faults, leading to severe
performance degradation.
FIFO (First-In, First-Out): Replaces the page that has been in memory for
the longest time, similar to a queue.
•LRU (Least Recently Used): Replaces the page that was least recently
accessed.
•Optimal (or MIN): Replaces the page that will not be used for the longest
time in the future. This algorithm is impossible to implement in practice but
serves as a benchmark.
•Random: Replaces any page randomly.
•NRU (Not Recently Used): A practical approximation of LRU, it selects a
page that hasn't been used recently, often using a "used bit" to track recent
usage.
•LIFO (Last-In, First-Out): Replaces the page that was most recently
loaded into memory.