Memory Management and Virtual Memory
Memory Management and Virtual Memory
Memory Management
/>
Group 5
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
</Background: What is Memory Management
In a multiprogramming computer, the operating system resides in part of memory, leaving the rest
for multiple processes.
2. Efficient Utilization
Memory management ensures efficient use of memory by allocating and deallocating memory
before and after process execution.
3. Fragmentation
It minimizes fragmentation issues (both internal and external) to maintain optimal memory
utilization.
4. Data Integrity
- Various methods exist for memory management, each with its own
algorithmic approach.
- It prevents memory wastage and allows multiple processes to share memory resources
effectively.
2. Dynamic Allocation
- Processes can request memory as required, leading to flexibility and efficient resource
utilization.
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
</Advantages cont’d
3. Fragmentation Handling
- Internal fragmentation occurs when allocated memory is larger than needed, but
memory management can mitigate this.
- It ensures that each process gets its fair share of memory, allowing efficient
multitasking.
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
</Disadvantages
1. Complexity
2. Overhead
3. Fragmentation
- Internal fragmentation arises when memory blocks are not fully utilized.
- However, it can slow down the system due to constant data transfers
between RAM and the hard disk.
- There's a risk of data loss or corruption if the hard disk fails during these
transfers.
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
</Contiguous Memory Allocation
-The Segment number is mapped to the segment table. The limit of the respective
segment is compared with the displacement. If the displacement is less than the
limit then the address is valid otherwise it throws an error as the address is invalid.
-In the case of valid addresses, the base address of the segment is added to the
displacement to get the physical address of the actual word in the main memory.
ADVANTAGES
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compared to the page table in paging.
DISADVANTAGES
6. It can have external fragmentation.
7. it is difficult to allocate contiguous memory to variable sized partition.
8. Costly memory management algorithms.
DIFFERENCE BETWEEN
PAGING AND SEGMENTATION
PAGING SEGMENTATION
● Closer to Operating System. ● Closer to User.
● Suffers from internal
fragmentation. ● Suffers from external
● It is faster. fragmentation.
● Logical address is divided into ● It is slower.
page number and page offset.
● Page table is used to maintain ● Logical address is
the page information. divided into segment
number and segment
offset.
● Segment table is used to
maintain the segment
information
</Paging vs Segmentation
Paging Segmentation
•Closer to Operating System. •Closer to User.
•Logical address is divided into page •Logical address is divided into segment number
number and page offset. and segment offset.
</ Inverted Paging
Principle: Instead of each process having its page table, a global page table is
maintained.
Data Structure: Inverted Page Table (IPT) is used to map each page to the
corresponding frame in physical memory.
Mapping: Each entry in the IPT contains the page number, process ID, and
additional control information.ystems with large address spaces.
Search Overhead:
○ Searching the IPT for page mappings can introduce overhead,
especially in systems with a large number of frames.
Concurrency Control:
○ Ensuring concurrent access to the IPT is properly managed to prevent
data corruption and race conditions.
● Address spaces larger than 32 bits pose a challenge for traditional page table
structures. Hashed page tables offer a solution by employing hash functions
to map virtual page numbers to physical page frames efficiently.
● Hashed page tables consist of a hash table, where each entry contains a
linked list of elements that hash to the same location.
● Each element typically includes the virtual page number, the mapped page
frame value, and a pointer to the next element in the list.
● Address translation involves hashing the virtual page number to locate the
corresponding physical page frame, handling collisions efficiently.
● Clustered page tables, a variation of hashed page tables, store mappings for
multiple physical page frames within a single page-table entry. This
approach is particularly useful for sparse address spaces, where memory
references are noncontiguous.
● The fundamental idea behind overlays is to load only the necessary parts of
a program into memory at any given time, freeing up memory for other
tasks. When a specific part of the program is required for execution, it is
loaded into memory, and once it's no longer needed, it is unloaded, making
space for new parts as necessary. This process is akin to swapping memory
blocks between disk storage and RAM.
● Introduction to Multilevel Paging - Multilevel paging is a hierarchical paging scheme with two or more levels of
page tables. - Each level's page table entry points to the next level's page table, with the last level storing actual
frame information. - Level 1 page table is stored in the Page Table Base Register (PTBR)
● Why Multilevel Paging is Required - Multilevel paging is used to address the large memory requirements of page
tables. - With a 32-bit physical address space and a large number of page table entries, storing all page tables in
physical memory leads to memory wastage. - Multile
Efficient memory utilization: Only the outermost page table needs to be in main memory, while other page tables are
brought in as required. - Memory savings: Storing only the outermost page table in a single frame reduces memory
overhead. - Flexibility: Multilevel paging allows varying the size and number of page tables based on system
requirements.
● In multilevel paging whatever may be levels of paging, all the page tables will be stored in the main memory. So it
requires more than one memory access to get the physical address of the page frame. One access for each level is
needed. Each page table entry except the last level page table entry contains the base address of the next level page
table
Levels in Paging
● - Multilevel paging is a hierarchical paging scheme used to address the large memory requirements of page tables. -
By bringing page tables into memory as needed, it saves memory space and improves memory utilization. - It
provides flexibility and efficient management of page tables in operating systems.
● Page table entries (PTEs) are data structures used in virtual memory systems
to map virtual addresses to physical addresses. Each PTE contains
information about a specific page or frame in memory, enabling the
operating system to manage memory efficiently.
● Page table entries (PTEs) are data structures used in virtual memory systems
to map virtual addresses to physical addresses. Each PTE contains
information about a specific page or frame in memory, enabling the
operating system to manage memory efficiently.
The operating system and hardware use physical addresses to access and
manipulate data stored in memory modules. From the perspective of the
hardware, the physical address space represents the entire range of memory
available in the system.
It's not accurate to say that one address space is inherently "better" than the
other because they serve different purposes and exist for different reasons
Paging: In paging, the logical address space and physical address space are
divided into fixed-size blocks called pages and page frames, respectively.
The MMU uses page tables to map logical pages to physical page frames.
Segmentation: Segmentation divides the logical address space and physical
address space into variable-sized segments. Each segment is mapped to a
segment descriptor that specifies its base address and size.
● Dynamic resizing: Systems can dynamically adjust the size of memory blocks
based on the actual requirements of the processes or data being stored,
minimizing wasted space.
● Memory pooling: Pooling smaller memory requests together can help utilize the
remaining space within blocks more effectively.
● External fragmentation occurs when there is enough total free memory to satisfy
a memory request, but the available memory is fragmented into small, non-
contiguous blocks, making it impossible to allocate a contiguous block of
memory.
● Causes:
● Dynamic memory allocation: Allocating and deallocating memory over
time can lead to small gaps or "holes" between allocated memory blocks.
Variable-size allocation: In systems where memory blocks are of varying sizes (e.g.,
dynamic partitioning), fragmentation can occur as blocks are allocated and
deallocated unevenly.
1011 011 01 1011001 10 11011 011 01 110110 110111 1101
</ Example
● Suppose a system has several small free memory blocks scattered throughout
memory. Even though the total free memory might be sufficient to fulfill a
request, if these blocks are not contiguous, the request cannot be satisfied,
resulting in external fragmentation.
H G A C
0 1 2 3 4 5 6 7 8 9
VIRTUAL MEMORY KEY WORDS
● Pages and Page Tables: VM divides the virtual ● Page Replacement: When physical memory
address space and physical memory into fixed- becomes full, the operating system must choose
which pages to evict from memory to make room
size blocks called pages. Page tables are for new pages. Page replacement algorithms,
structures used by the operating system to track such as the least recently used (LRU) algorithm,
the mapping between virtual pages and physical are used to determine which pages to evict based
memory pages. on their recent usage patterns.
DEMAND PAGING
When the CPU calls out for process
3(I-R) to the MM, there will be a
page fault(P3 not found in MM). So
the CPU transfers a control signal to
the OS which checks if there is
space in MM to swap in P3. If
there’s space P3 is swapped in but in
this case, P1 & P2 will be both
swapped out & then the swapping in
of P3 is carried out
PERFORMANCE OF DEMAND PAGING
● Page fault rate exists in this range: 0 <= P <= 1
If P = 0, there’s no page fault
If P = 1, there exists a page fault
● The performance of demand paging is often measured in terms of the effective access time.
● Effective access time is the amount of time it takes to access memory, if the cost of page faults are
amortized over all memory accesses.
● In some sense it is an average or expected access time.
EAT = (1 - P) * MAT + P*PFT
EAT = effective access time
MAT = physical memory (core) access time
PFT = page fault time
P = probability of a page fault occurring
(1-P) = the probability of accessing memory in an available frame
Paging fault time can be broken down also as
shown above.
EXAMPLE OF DEMAND PAGING
PERFORMANCE
</ Page Replacement Algorithms
The Ratio of Page Hit to the Page Fault = 8 : 12 - - - > 2 : 3 - - - > 0.66
The Page Hit Percentage = 8 *100 / 20 = 40%
The Page Fault Percentage = 100 - Page Hit Percentage = 100 - 40 = 60%
Explanation:
First, fill the frames with the initial pages. Then, after the frames are filled we need to create a
space in the frames for the new page to occupy. So, with the help of First in First Out Page
Replacement Algorithm we remove the frame which contains the page is older among the
pages. By removing the older page we give access for the new frame to occupy the empty
space created by the First in First out Page Replacement Algorithm.
From the sub sequence 0, 3, 4, 6, 0, 2, 1 we can say that 1 is the last occurring
page number. So we can say that 0 can be placed in the frame body by removing
1 from the frame.
Let us understand this OPTIMAL Page Replacement Algorithm working with
the help of an example.
First, fill the frames with the initial pages. Then, after the frames are filled we need to create a
space in the frames for the new page to occupy.
Here, we would fill the empty spaces with the pages we and the empty frames we have. The
problem occurs when there is no space for occupying of pages. We have already known that we
would replace the Page which is not used in the Longest Dimension of time in future.
There comes a question what if there is absence of page which is in the frame.
Suppose the Reference String is:
0, 2, 4, 6, 0, 2, 1, 2, 1, 2, 0, 3, 2, 1, 2, 0
6, 1, 5 are in the frames occupying the frames.
Here, we can see that page number 5 is not present in the Reference String. But the number 5 is
present in the Frame. So, as the page number 5 is absent we remove it when required and other
page can occupy that position.
Explanation
First, fill the frames with the initial pages. Then, after the frames are filled we need to
create a space in the frames for the new page to occupy.
Here, we would fill the empty spaces with the pages we and the empty frames we have.
The problem occurs when there is no space for occupying of pages. We have already
known that we would replace the Page which is not used in the Longest Dimension of
time in past or can be said as the Page which is very far away in the past.
● One of the main causes of it is overcommitting memory, which means that the
system allocates more memory than it has available
● Another cause is inadequate memory allocation, where the system has too little
memory to hold all the necessary data and programs
Effects
● reducing system performance
● slow down the system
● longer boot times
● slower application load times
● high disk usage
Prevention
● One technique is to increase the amount of memory available,
either by adding more physical memory or by using virtual
memory.
● Another technique is to reduce the number of processes running
concurrently, thus reducing the demand for memory.
● Load balancing can also help prevent Thrashing in OS by
distributing the workload evenly across multiple processors.
● Overall, the key to preventing Thrashing in OS is to ensure that
the system has enough memory, allocate memory efficiently, and
use effective memory management techniques to optimize
memory usage
CONCLUSION
● In operating systems, “Thrashing in OS” refers to a state where
the system spends excessive time swapping pages between main
memory and secondary storage, resulting in poor performance.
This can occur when the system has insufficient physical
memory to hold all the required pages in memory, and as a
result, the system spends too much time swapping pages in and
out of memory. To avoid Thrashing in the OS, the system can use
various techniques, such as increasing the amount of physical
memory available, adjusting the paging algorithm, or optimizing
the application’s memory usage. In conclusion,
● Thrashing in OS is a significant performance problem that can
occur in operating systems when the system’s physical memory
is insufficient to hold all the required pages in memory.
● To prevent Thrashing in the OS, it is essential to optimize
memory usage and consider increasing physical memory or
adjusting the paging algorithm.