Virtual Memory
Virtual memory is a memory
management technique used by modern operating systems to extend the capacity of
main memory (RAM). It creates the illusion of a memory space that is much
larger than the actual physical memory, thereby allowing programs to run even
if they don't entirely fit into main memory at once.
Virtual memory works by using a
portion of the computer's disk space as a temporary storage for information
that is not currently being used by the CPU. The data that is most frequently
accessed is kept in physical memory while other data is moved to the virtual
memory space.
Here are some key aspects of
virtual memory:
- Paging: This is a core function of virtual
memory that divides memory into fixed-size blocks called pages. When a
program needs to access data that is not in main memory, the operating
system will swap out a page from main memory to disk and then load the
required page into memory.
- Swapping: This is the process where an
entire process is moved from main memory to disk and back.
- Page Fault: A page fault occurs when a
program tries to access data that was mapped into virtual memory but is
not currently located in the physical memory. This prompts the operating
system to intervene and move the data from the virtual memory into the
physical memory.
- Page Replacement Algorithms: These are used
to decide which memory pages to swap out of main memory to disk when a
page of memory needs to be allocated. Examples include the Least Recently
Used (LRU) and Most Recently Used (MRU) algorithms.
- Memory Protection: Virtual memory provides a
degree of isolation between processes, thereby providing memory
protection. Each process runs in its own virtual memory space, and it
cannot access the memory of other processes without explicit mechanisms to
do so.
The main benefits of virtual
memory include the ability to run larger programs than would otherwise fit into
main memory, increased system utilization, and enhanced memory protection and
isolation. However, it also introduces complexity to the operating system and
can lead to degraded performance if not managed correctly, such as excessive
page swapping, known as thrashing.
Introduction to Paging
Paging is a method of managing
computer memory. It is one of the key mechanisms that allows virtual memory
systems to function.
In a paging system, the operating
system divides an application's virtual memory into blocks of contiguous
addresses known as "pages". The corresponding blocks in physical
memory, where these pages are loaded, are called "frames".
Key points about paging include:
- Page and Frame Size: The size of a page (and
corresponding frame) is typically determined by the hardware. Common page
sizes range from 4KB to 64KB, though some systems support larger page
sizes.
- Page Table: The operating system maintains a
data structure, known as a page table, for each process. The page table
maps the virtual page numbers of a process to the corresponding physical
frames in memory.
- Translation Lookaside Buffer (TLB): Because
accessing the page table can be time-consuming, many systems use a special
cache known as a TLB to store recent translations from virtual to physical
addresses.
- Swapping and Page Faults: If a process tries
to access a page that is not currently in physical memory (known as a
"page fault"), the operating system will choose a page to swap
out from memory to disk, load the needed page into the now-free frame, and
then resume the process.
- Memory Protection: Paging also helps with
memory protection. Because each process has its own page table, one
process cannot access the memory of another process unless the operating
system explicitly allows it.
- Internal Fragmentation: Since memory is
allocated in terms of fixed-size pages, there can be internal
fragmentation, where the last page of a process might not be fully
utilized.
Paging allows the physical memory
to be used more efficiently by multiple processes and is a core concept in
modern operating systems that use virtual memory.
Introduction to Segmentation
Segmentation is another memory
management technique used by operating systems, particularly for systems that
utilize virtual memory. Unlike paging, which divides memory into fixed-sized
blocks, segmentation divides memory into variable-sized blocks based on logical
divisions in a program. These blocks are known as "segments."
Key points about segmentation
include:
- Segments: Each program is divided into
segments based on logical units such as functions, data structures, and
arrays. Each segment represents a separate portion of a program's address
space, such as the code, stack, and heap segments.
- Segment Table: The operating system
maintains a segment table for each process. This table stores the starting
address and length of each segment. When a process needs to access a memory
location, it specifies the segment number and the offset within the
segment.
- Advantages: Segmentation can be more
efficient than paging because it reduces internal fragmentation. It can
also enhance security and simplicity by isolating different segments of a
program from one another.
- Disadvantages: Segmentation can lead to
external fragmentation, where the total free memory is enough to satisfy a
request, but it's not contiguous and thus can't be used. Compaction, a
costly process, is needed to reduce external fragmentation.
- Memory Protection: Like paging, segmentation
also aids in memory protection. Different segments can have different
levels of protection, such as read-only or execute-only.
- Segmentation with Paging: Some systems
combine segmentation and paging to gain the benefits of both, dividing
memory into segments for the logical benefits and then dividing those
segments into pages for better physical memory management. This is called
paged segmentation.
Segmentation offers several
benefits over simple paging, especially for complex programs that benefit from
being broken down into logical units. However, it can also be more complex to
implement and manage.
Introduction to Fragmentation
Fragmentation is a common issue
that occurs in computer systems, particularly in memory management and storage.
It refers to the inefficient use of memory or storage space, resulting in
unused or partially used space that cannot be effectively utilized.
Fragmentation can occur in two main forms: internal fragmentation and external
fragmentation.
- Internal Fragmentation: Internal
fragmentation occurs when a portion of allocated memory or storage is
unused but cannot be utilized by other processes or programs. This happens
because memory is allocated in fixed-size blocks, and if the requested
memory size is slightly smaller than the block size, the remaining space
within the block remains unused. This unused space is called internal
fragmentation.
For example, if a program requests
a memory block of 16KB but is only able to use 14KB, there will be 2KB of
internal fragmentation. In this case, the remaining 2KB within the allocated
block is wasted and cannot be used by other processes.
- External Fragmentation: External
fragmentation occurs when there is enough total free memory or storage
space to satisfy a request, but the available space is divided into small
non-contiguous chunks. As a result, the requested memory or storage cannot
be allocated because it is not available in a single contiguous block.
External fragmentation can be more
problematic than internal fragmentation as it leads to memory or storage being
underutilized even though there is technically enough free space to fulfill a
request. Over time, external fragmentation can reduce the overall efficiency of
memory or storage utilization.
Both internal and external
fragmentation can impact system performance and efficiency. Memory management
techniques such as compaction, which rearranges memory to consolidate free
space, and memory allocation strategies like paging and segmentation, aim to
reduce the effects of fragmentation and optimize memory utilization in computer
systems. Similarly, defragmentation is used in storage systems to consolidate
fragmented files and improve disk performance.
Introduction to Page fault
A page fault is an interrupt or
exception that occurs when a program tries to access a portion of memory (a
page) that is not currently stored in the physical memory (RAM), but rather in
the virtual memory (typically a hard drive or SSD). This happens when the
operating system uses a technique called paging to manage memory.
Here are the key steps that occur
during a page fault:
- When a program needs to access data that isn't in
the physical memory, the system triggers a page fault interrupt.
- The operating system then checks if the request for
the memory access is valid and if the page is in the virtual memory. If
it's not valid (i.e., the process requested a non-existent address or
violated protection policies), the OS typically terminates the process.
This is known as a segmentation fault or access violation.
- If the request is valid, the OS will look for a
free frame in the physical memory. If no such frames exist, the OS will
choose a frame to be swapped out based on a page replacement algorithm
(such as Least Recently Used (LRU), First-In, First-Out (FIFO), etc.).
- The required page is then loaded from the virtual
memory into the freed frame in the physical memory.
- Finally, the page table is updated to reflect the
new location of the page, and the interrupted process resumes.
Page faults are a normal part of
operating system operation when using virtual memory, but if they occur too
frequently (a situation known as thrashing), they can significantly degrade
system performance, as reading from disk is much slower than reading from RAM.
Techniques like improving the page replacement algorithm, increasing the amount
of RAM, or modifying programs to access memory in a more sequential manner can
help reduce the frequency of page faults.