Memory management is an operating system process that efficiently allocates and
deallocates memory to running programs, keeping track of free and used memory to ensure
proper system performance. Key strategies include contiguous allocation (fixed or variable
partitions), non-contiguous allocation (paging, segmentation), and swapping, all designed to
optimize memory use, enable multitasking, and prevent issues like memory leaks and
fragmentation.
Key objectives and strategies
Tracking memory: The OS keeps track of which memory locations are free and which are
allocated to a process. It also tracks how much memory can be allocated to each process.
Efficient allocation/deallocation: Memory is allocated when a process needs it and
deallocated when it's no longer required, making it available for other programs.
Support for multitasking: Memory management enables multiple processes to run
simultaneously by managing memory allocation for each one effectively.
Memory protection: It isolates processes from each other to prevent one from corrupting
another's memory, which enhances system stability and security.
Virtual memory: This allows the system to run programs that require more memory than
physically available by moving data between main and secondary storage.
Fragmentation prevention: Some methods aim to reduce fragmentation, which happens
when memory is broken into small, unusable pieces. Techniques like compaction can help.
Core techniques
Contiguous Allocation: Memory is allocated in one continuous block for each process.
Fixed Partition: Memory is divided into fixed-size partitions, which can lead to internal
fragmentation.
Variable Partition: Partitions are created dynamically based on a process's size, avoiding
internal fragmentation but leading to external fragmentation.
Non-Contiguous Allocation: Memory is not required to be in a single continuous block.
Paging: Memory is divided into fixed-size blocks called frames (physical memory) and pages
(logical memory), which can be non-adjacent.
Segmentation: Memory is divided into logical units called segments, such as code and data
sections.
Swapping: This involves moving a process from main memory to secondary storage to free
up space and then bringing it back when needed.
………………………
Contiguous memory allocation is an operating system memory management technique
where a process is allocated a single, unbroken block of memory. This method is simple and
provides fast access because all of a process's memory is in one place. However, it can lead
to fragmentation, where free memory is divided into small, non-contiguous blocks, making it
difficult to allocate space for new processes even if the total free memory is sufficient.
How it works
When a process needs memory, the operating system assigns it a single, contiguous block
of memory.
The memory is often divided into partitions, and a process is placed in a partition that fits its
needs.
This approach simplifies memory management and can ensure fast access speeds.
Advantages
Simplicity: It is a straightforward and easy-to-implement memory management technique.
Fast Access: Because the memory is continuous, the CPU can easily and quickly calculate
the address of any location within the block.
Disadvantages
Fragmentation: This is the main drawback, where memory becomes fragmented into small,
non-contiguous pieces.
External fragmentation: Occurs when there is enough total free memory, but it is broken into
small, non-contiguous chunks, so a large process cannot be loaded.
Internal fragmentation: Occurs when a process is allocated a fixed-size block that is larger
than it needs, wasting the unused space within that block.
Process Size Limitation: A process cannot be loaded if its size is larger than the largest
available partition.
……………………….
Paging is an operating system memory management scheme that divides a process's logical
memory and physical memory into fixed-size blocks called pages and frames, respectively. It
allows pages of a process to be stored in non-contiguous frames, enabling the operating
system to manage memory more efficiently and reduce external fragmentation. A page table
maps the virtual addresses (page numbers) to physical addresses (frame numbers), and the
Memory Management Unit (MMU) performs this address translation to access data.
How Paging Works
Segmentation: Each process is divided into fixed-size logical blocks called pages. The main
memory (RAM) is also divided into fixed-size blocks called frames. The page size is equal to
the frame size.
Address Translation: When the CPU generates a virtual address, the MMU uses this
address to look up the corresponding physical address.
Page Table: The operating system maintains a page table for each process, which stores the
mapping between virtual page numbers and their physical frame locations.
Frame Allocation: Pages can be loaded into any available frame in physical memory, not
necessarily contiguous ones.
Demand Paging: In some systems, only the necessary pages of a process are loaded into
memory when they are required, a technique known as demand paging or a "lazy swapper".
Benefits of Paging
Virtual Memory Implementation: Paging is a key component of virtual memory, allowing
processes to use more memory than physically available by storing parts of them in
secondary storage (disk).
No External Fragmentation: Because pages can be placed in any available frame, paging
eliminates external fragmentation, which occurs when free memory is broken into small,
unusable pieces.
Efficient Memory Utilization: Paging maximizes the utilization of main memory by allowing
fragmented memory to be used effectively.
Key Components
Pages: Fixed-size blocks of a process's logical memory.
Frames: Fixed-size blocks of physical memory (RAM).
Page Table: A data structure used by the operating system to map virtual page numbers to
physical frame numbers.
Translation Lookaside Buffer (TLB): A special hardware cache that stores frequently used
page table entries to speed up the address translation process and reduce overhead.
Page Fault: An event that occurs when a requested page is not found in physical memory,
prompting the operating system to fetch it from secondary storage.
……………………….
The structure of an operating system page table can vary, with common designs including
hierarchical (multi-level) paging, hashed page tables, and inverted page tables. Each entry in
a page table maps a virtual page to a physical frame and includes status bits like a
valid/invalid bit, protection bits, and a dirty bit.
1. Hierarchical paging (multi-level paging)
This structure breaks up the logical address space into multiple tables, creating a tree-like
structure.
For example, a two-level table has a first-level table whose entries point to second-level
tables. The final level contains the actual page information.
This method is effective for large address spaces, as it can reduce the memory required for
page tables compared to a single-level table.
2. Hashed page tables
This structure uses a hash function on the virtual page number to find its corresponding
entry in a hash table.
To handle collisions (multiple virtual pages hashing to the same location), each entry in the
hash table contains a linked list of elements.
Each element in the list includes the virtual page number, the physical frame number, and a
pointer to the next element.
This is particularly useful for very large address spaces, such as those beyond 32 bits.
3. Inverted page tables
This structure combines a page table and a frame table into a single structure.
There is a single table with an entry for each physical memory frame, rather than an entry
per virtual page.
Each entry contains the virtual page number, the process ID (PID) of the process owning the
page, and other information.
While it reduces the memory needed for the table, it can increase lookup time because the
OS must search the table to find the correct entry.
Page table entry components
Regardless of the overall structure, a page table entry typically contains the following
information:
Page frame number (PFN): The physical address of the page in RAM.
Valid/invalid bit: Indicates whether the page is currently in physical memory.
Protection bits: Define access permissions, such as read-only, read-write, or execute-only.
Dirty bit: Shows if the page has been modified since it was loaded into memory.
Referenced bit: Indicates if the page has been recently accessed, often used in page
replacement algorithms.
……………………..
Swapping is an operating system memory management technique that moves processes
between main memory (RAM) and secondary storage (like a hard disk) to make the most of
limited physical memory. When RAM is full, the OS "swaps out" an idle process to the
secondary storage (swap space) to make room for a new process. The swapped-out
process can later be "swapped in" and resumed from where it left off.
This video explains the concept of swapping in operating systems:
40s
EZCSE
YouTube · 30 Oct 2022
How it works
Swap-out: When RAM is full and a new process needs to be loaded, the OS selects an
inactive or low-priority process and moves it from main memory to secondary storage (swap
space).
Swap-in: When the swapped-out process is needed again, the OS moves it back from the
swap space into RAM, potentially replacing another process that is no longer active.
Priority-based swapping: If a higher-priority process arrives, the OS can swap out a
lower-priority process to make space for it, ensuring the more important task runs first.
Purpose and benefits
Increased multiprogramming: Allows the system to run more processes than can fit into the
physical RAM at one time.
Improved resource utilization: Maximizes CPU usage by ensuring there's always a process
ready to run, even when memory is a bottleneck.
Enables larger applications: Makes it possible to run larger programs by moving less critical
data out of the way.
Key terms
Swap Space: The area on the secondary storage where processes are temporarily stored
during swapping.
Swap-out: The act of moving a process from RAM to swap space.
Swap-in: The act of moving a process from swap space back to RAM.
……………………