Unit-2 Memory Management
Unit-2 Memory Management
Unit-2 Memory Management
Memory is the important part of the computer that is used to store the data. Its management is critical
to the computer system because the amount of main memory available in a computer system is very
limited. At any time, many processes are competing for it. Moreover, to increase performance, several
processes are executed simultaneously. For this, we must keep several processes in the main memory,
so it is even more important to manage them effectively.
Advantages of Segmentation
1. The size of the segment table is less compared to the size of the page table.
2. There is no internal fragmentation.
Disadvantages of Segmentation
1. When the processes are loaded and removed ( during swapping ) from the main memory then free
memory spaces are broken into smaller pieces and this causes external fragmentation.
2. Here also the time to access the data increases as due to segmentation the main memory has to be now
accessed two times. First, we need to access the segment table which is also stored in the main memory
and second, combine the base address of the segment with the segment offset and then get the physical
address which is again stored in the main memory.
Topic: Page Replacement Algorithms in Operating Systems
In an operating system that uses paging for memory management, a page replacement algorithm is
needed to decide which page needs to be replaced when a new page comes in. Page replacement
becomes necessary when a page fault occurs and there are no free page frames in memory. However,
another page fault would arise if the replaced page is referenced again. Hence it is important to replace
a page that is not likely to be referenced in the immediate future. y. If no page frame is free, the virtual
memory manager performs a page replacement operation to replace one of the pages existing in
memory with the page whose reference caused the page fault. It is performed as follows: The virtual
memory manager uses a page replacement algorithm to select one of the pages currently in memory
for replacement, accesses the page table entry of the selected page to mark it as “not present” in
memory, and initiates a page-out operation for it if the modified bit of its page table entry indicates
that it is a dirty page.
Page Fault: A page fault happens when a running program accesses a memory page that is mapped
into the virtual address space but not loaded in physical memory. Since actual physical memory is
much smaller than virtual memory, page faults happen. In case of a page fault, Operating System might
have to replace one of the existing pages with the newly needed page. Different page replacement
algorithms suggest different ways to decide which page to replace. The target for all algorithms is to
reduce the number of page faults.
Page Replacement Algorithms:
1. First In First Out (FIFO): This is the simplest page replacement algorithm. In this algorithm, the
operating system keeps track of all pages in the memory in a queue, the oldest page is in the front of
the queue. When a page needs to be replaced page in the front of the queue is selected for removal.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames.Find the number of
page faults.
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page
Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is not available in
memory so it replaces the oldest page slot i.e 1. —>1 Page Fault. 6 comes, it is also not available in
memory so it replaces the oldest page slot i.e 3 —>1 Page Fault. Finally, when 3 come it is not
available so it replaces 0 1 page fault.
Belady’s anomaly proves that it is possible to have more page faults when increasing the number of
page frames while using the First in First Out (FIFO) page replacement algorithm. For example, if we
consider reference strings 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we get 9 total page faults, but if
we increase slots to 4, we get 10-page faults.
2. Optimal Page replacement: In this algorithm, pages are replaced which would not be used for the
longest duration of time in the future.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page frame. Find
number of page fault.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7 because it is not used
for the longest duration of time in the future.—>1 Page fault. 0 is already there so —> 0 Page
fault. 4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already available in the
memory.
Optimal page replacement is perfect, but not possible in practice as the operating system cannot know
future requests. The use of Optimal Page replacement is to set up a benchmark so that other
replacement algorithms can be analyzed against it.
3. Least Recently Used: In this algorithm, page will be replaced which is least recently used.
Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page frames.
Find number of page faults.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault. when 3 came it will take the place of 7 because it is least
recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in the
memory.
4. Most Recently Used (MRU): In this algorithm, page will be replaced which has been used recently.
Belady’s anomaly can occur in this algorithm.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so–> 0 page fault
when 3 comes it will take place of 0 because it is most recently used —>1 Page fault
when 0 comes it will take place of 3 —>1 Page fault
when 4 comes it will take place of 0 —>1 Page fault
2 is already in memory so —> 0 Page fault
when 3 comes it will take place of 2 —>1 Page fault
when 0 comes it will take place of 3 —>1 Page fault
when 3 comes it will take place of 0 —>1 Page fault
when 2 comes it will take place of 3 —>1 Page fault
when 3 comes it will take place of 2 —>1 Page fault
-------------------------------------------------------------------------------------------------------------------------
Topic: Virtual Memory in Operating System
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as
though it were part of the main memory. The addresses a program may use to reference memory are
distinguished from the addresses the memory system uses to identify physical storage sites and
program-generated addresses are translated automatically to the corresponding machine addresses.
A memory hierarchy, consisting of a computer system’s memory and a disk, that enables a process
to operate with only some portions of its address space in memory. A virtual memory is what its
name indicates- it is an illusion of a memory that is larger than the real memory. We refer to the
software component of virtual memory as a virtual memory manager. The basis of virtual memory
is the noncontiguous memory allocation model. The virtual memory manager removes some
components from memory to make room for other components.
The size of virtual storage is limited by the addressing scheme of the computer system and the
amount of secondary memory available not by the actual number of main storage locations.
It is a technique that is implemented using both hardware and software. It maps memory addresses
used by a program, called virtual addresses, into physical addresses in computer memory.
1. All memory references within a process are logical addresses that are dynamically translated
into physical addresses at run time. This means that a process can be swapped in and out of the main
memory such that it occupies different places in the main memory at different times during the course
of execution.
2. A process may be broken into a number of pieces and these pieces need not be continuously
located in the main memory during execution. The combination of dynamic run-time address
translation and the use of a page or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or segments are present
in the main memory during execution. This means that the required pages need to be loaded into
memory whenever required. Virtual memory is implemented using Demand Paging or
Demand Segmentation.
Demand Paging
The process of loading the page into memory on demand (whenever a page fault occurs) is known
as demand paging. The process includes the following steps are as follows:
1. If the CPU tries to refer to a page that is currently not available in the main memory, it generates
an interrupt indicating a memory access fault.
2. The OS puts the interrupted process in a blocking state. For the execution to proceed the OS must
bring the required page into the memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical address space. The page
replacement algorithms are used for the decision-making of replacing the page in physical address
space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution and it will place the process
back into the ready state.
Hence whenever a page fault occurs these steps are followed by the operating system and the required
page is brought into memory.
Advantages of Virtual Memory
• More processes may be maintained in the main memory: Because we are going to load only
some of the pages of any particular process, there is room for more processes. This leads to more
efficient utilization of the processor because it is more likely that at least one of the more numerous
processes will be in the ready state at any particular time.
• A process may be larger than all of the main memory: One of the most fundamental restrictions
in programming is lifted. A process larger than the main memory can be executed because of demand
paging. The OS itself loads pages of a process in the main memory as required.
• It allows greater multiprogramming levels by using less of the available (primary) memory for
each process.
• It has twice the capacity for addresses as main memory.
• It makes it possible to run more applications at once.
• Users are spared from having to add memory modules when RAM space runs out, and applications
are liberated from shared memory management.
• When only a portion of a program is required for execution, speed has increased.
• Memory isolation has increased security.
• It makes it possible for several larger applications to run at once.
• Memory allocation is comparatively cheap.
• It doesn’t require outside fragmentation.
• It is efficient to manage logical partition workloads using the CPU.
• Automatic data movement is possible.
Disadvantages of Virtual Memory
• It can slow down the system performance, as data needs to be constantly transferred between the
physical memory and the hard disk.
• It can increase the risk of data loss or corruption, as data can be lost if the hard disk fails or if there
is a power outage while data is being transferred to or from the hard disk.
• It can increase the complexity of the memory management system, as the operating system needs
to manage both physical and virtual memory.
Page Fault Service Time: The time taken to service the page fault is called page fault service time.
The page fault service time includes the time taken to perform all the above six steps.
Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
Swapping
Swapping is a process out means removing all of its pages from memory, or marking them so that
they will be removed by the normal page replacement process. Suspending a process ensures that it
is not runnable while it is swapped out. At some later time, the system swaps back the process from
the secondary storage to the main memory. When a process is busy swapping pages in and out then
this situation is called thrashing.