Unit 4
Unit 4
Memory Management
1.Introduction –Swapping:
Swapping has been subdivided into two concepts: swap-in and swap-out.
• Swap-out is a technique for moving a process from RAM to the hard disc.
• Swap-in is a method of transferring a program from a hard disc to main memory, or RAM.
Advantages
If there is low main memory so some processes may has to wait for much long but by using
swapping process do not have to wait long for execution on CPU.
Using only single main memory, multiple process can be run by CPU using swap partition.
The concept of virtual memory start from here and it utilize it in better way.This concept can
be useful in priority based scheduling to optimize the swapping process.
Disadvantages
• If there is low main memory resource and user is executing too many processes and suddenly
the power of system goes off there might be a scenario where data get erase of the processes
which are took parts in swapping.
• Chances of number of page faults occur
• Low processing performance
• Only one process occupies the user program area of memory in a single tasking operating
system and remains in memory until the process is completed.
When all of the active processes in a multitasking operating system cannot coordinate in
main memory, a process is swapped out of main memory so that other processes can enter
it.
2.Contiguous Memory Allocation :
Allocating space to software applications is referred to as memory allocation. Memory is a sizable
collection of bytes. Contiguous and non-contiguous memory allocation are the two basic types of
memory allocation. Contiguous memory allocation enables the tasks to be finished in a single memory
region. Contrarily, non-contiguous memory allocation distributes the procedure throughout many
memory locations in various memory sections.
According to the contiguous memory allocation technique, if a process needs to be given space in the
memory, we must give it a continuous empty block of space to reside in. There are two ways to allocate
this:
Each process in this method of contiguous memory allocation is given a fixed size continuous
block in the main memory. This means that the entire memory will be partitioned into
continuous blocks of fixed size, and each time a process enters the system, it will be given one
of the available blocks. Because each process receives a block of memory space that is the same
size, regardless of the size of the process. Static partitioning is another name for this approach.
Three processes in the input queue in the figure above require memory space allocation. The
memory has fixed-sized chunks because we are using the fixed size partition technique. In
addition to the 4MB process, the first process, which is 3MB in size, is given a 5MB block. The
second process, which is 1MB in size, is also given a 5MB block. So, it doesn't matter how big
the process is. The same fixed-size memory block is assigned to each.
It is obvious that under this system, the number of continuous blocks into which the memory
will be partitioned will be determined by the amount of space each block covers, and this, in
turn, will determine how many processes can remain in the main memory at once.
The degree of multiprogramming refers to the number of processes that can run concurrently
in memory. Therefore, the number of blocks formed in the RAM determines the system's level
of multiprogramming.
Advantages
o This strategy is easy to employ because each block is the same size. Now all that is left to
do is allocate processes to the fixed memory blocks that have been divided up.
o It is simple to keep track of how many memory blocks are still available, which determines
how many further processes can be allocated memory.
o This approach can be used in a system that requires multiprogramming since numerous
processes can be maintained in memory at once.
Disadvantages
Although the fixed-size partitioning strategy offers numerous benefits, there are a few
drawbacks as well:
o We won't be able to allocate space to a process whose size exceeds the block since the
size of the blocks is fixed.
o The amount of multiprogramming is determined by block size, and only as many
processes can run simultaneously in memory as there are available blocks.
o We must assign the process to this block if the block's size is more than that of the
process; nevertheless, this will leave a lot of free space in the block. This open area might
have been used to facilitate another procedure.
Flexible Partitioning Method
No fixed blocks or memory partitions are created while using this style of contiguous memory
allocation technique. Instead, according on its needs, each process is given a variable-sized
block. This indicates that if space is available, this amount of RAM is allocated to a new process
whenever it requests it. As a result, each block's size is determined by the needs and
specifications of the process that uses it.
There are no partitions with set sizes in the aforementioned diagram. Instead, the first process is given
just 3MB of RAM because it requires that much. The remaining 3 processes are similarly given only the
amount of space that is necessary for them.
This method is also known as dynamic partitioning because the blocks' sizes are flexible and
determined as new processes start.
Advantages
o There is no internal fragmentation because the processes are given blocks of space according to
their needs. Therefore, this technique does not waste RAM.
o How many processes are in the memory at once and how much space they take up will determine
how many processes can be running simultaneously. As a result, it will vary depending on the
situation and be dynamic.
o Even a large process can be given space because there are no blocks with set sizes.
Disadvantages
Despite the variable-size partition scheme's many benefits, there are a few drawbacks as well:
So far, we've examined two different contiguous memory allocation strategies. But what
transpires when a fresh process needs to be given a location in the main memory? How is the
block or segment that it will receive chosen?
Continuous blocks of memory assigned to processes cause the main memory to always be full.
A procedure, however, leaves behind an empty block termed as a hole after it is finished. A new
procedure could potentially be implemented in this area. As a result, there are processes and
holes in the main memory, and each one of these holes might be assigned to a new process
that comes in.
First-Fit
This is a fairly straightforward technique where we start at the beginning and assign the first
hole, which is large enough to meet the needs of the process. The first-fit technique can also be
applied so that we can pick up where we left off in our previous search for the first-fit hole.
Best-Fit
The goal of this greedy method, which allocates the smallest hole that meets the needs of the
process, is to minimise any memory that would otherwise be lost due to internal fragmentation
in the event of static partitioning. Therefore, in order to select the greatest match for the
procedure without wasting memory, we must first sort the holes according to their diameters.
Worst-Fit
The Best-Fit strategy is in opposition to this one. The largest hole is chosen to be assigned to
the incoming process once the holes are sorted based on size. The theory behind this allocation
is that because the process is given a sizable hole, it will have a lot of internal fragmentation left
over. As a result, a hole will be left behind that can house a few additional processes.
Contiguous Memory Allocation's advantages and disadvantages
Contiguous memory allocation has a range of benefits and drawbacks. The following are a few
benefits and drawbacks:
Advantages
o The number of memory blocks remaining, which affects how many further processes can be given
memory space, is easy to keep track of.
o Contiguous memory allocation has good read performance since the entire file can be read from
the disc in a single process.
o The contiguous allocation works well and is easy to set up.
Disadvantages
o Fragmentation is not an issue because each new file can be written to the disk's end after the
preceding one.
o In order to choose the proper hole size while creating a new file, it needs know its final size.
o The extra space in the holes would need to be compressed or used once the diskis full.
Conclusion
o When a process is brought into the main memory to be executed, contiguous memory allocation
allocates contiguous blocks of memory to that process.
o There are two methods for allocating contiguous memory:
o Each process is given access to a fixed size continuous block of main memory through fixed size
partitioning.
o Variable Size Partitioning: Depending on the needs of each process, space is allocated. No specific
fixed-size block is present.
o There are three ways to give an entering process a hole:
o First-Fit: Assign the procedure to the first, adequate hole.
o The smallest hole that fulfils the procedure' requirements should be assigned.
o Worst-Fit: Give the entering process the hole with the biggest size among all.
3.Paging:
In Operating Systems, Paging is a storage mechanism used to retrieve processes from the
secondary storage into the main memory in the form of pages.
The main idea behind the paging is to divide each process in the form of pages. The main
memory will also be divided in the form of frames.
One page of the process is to be stored in one of the frames of the memory. The pages can be
stored at the different locations of the memory but the priority is always to find the contiguous
frames or holes.
Pages of the process are brought into the main memory only when they are required otherwise
they reside in the secondary storage.
Different operating system defines different frame sizes. The sizes of each frame must be equal.
Considering the fact that the pages are mapped to the frames in Paging, page size needs to be as same
as frame size.
Example
Let us consider the main memory size 16 Kb and Frame size is 1 KB therefore the main memory
will be divided into the collection of 16 frames of 1 KB each.
There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each. Each process is divided
into pages of 1 KB each so that one page can be stored in one frame.
Initially, all the frames are empty therefore pages of the processes will get stored in the
contiguous way.
Frames, pages and the mapping between the two is shown in the image below.
Let us consider that, P2 and P4 are moved to waiting state after some time. Now, 8 frames
become empty and therefore other pages can be loaded in that empty place. The process P5 of
size 8 KB (8 pages) is waiting inside the ready queue.
Given the fact that, we have 8 non contiguous frames available in the memory and paging
provides the flexibility of storing the process at the different places. Therefore, we can load the
pages of process P5 in the place of P2 and P4.
When a page is to be accessed by the CPU by using the logical address, the operating system needs to
obtain the physical address to access that page physically.
1. Page Number
2. Offset
Memory management unit of OS needs to convert the page number to the frame number.
Example
Considering the above image, let's say that the CPU demands 10th word of 4th page of process P3.
Since the page number 4 of process P1 gets stored at frame number 9 therefore the 10th word of 9th
frame will be returned as the physical address.
4.Segmentation:
In Operating Systems, Segmentation is a memory management technique in which the memory is divided
into the variable size parts. Each part is known as a segment which can be allocated to a process.
The details about each segment are stored in a table called a segment table. Segment table is stored in
one (or many) of the segments.
Till now, we were using Paging as our main memory management technique. Paging is more close to the
Operating system rather than the User. It divides all the processes into the form of pages regardless of
the fact that a process can have some relative parts of functions which need to be loaded in the same
page.
Operating system doesn't care about the User's view of the process. It may divide the same function into
different pages and those pages may or may not be loaded at the same time into the memory. It
decreases the efficiency of the system.
It is better to have segmentation which divides the process into the segments. Each segment contains
the same type of functions such as the main function can be included in one segment and the library
functions can be included in the other segment.
With the help of segment map tables and hardware assistance, the operating system can easily translate
a logical address into physical address on execution of a program.
The Segment number is mapped to the segment table. The limit of the respective segment is compared
with the offset. If the offset 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 offset to get the physical
address of the actual word in the main memory.
The above figure shows how address translation is done in case of segmentation.
Advantages of Segmentation
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
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.
Difference between Paging and Segmentation
Sr Paging Segmentation
No.
2 Paging divides program into fixed size pages. Segmentation divides program into variable size
segments.
8 Logical address is divided into page number and Logical address is divided into segment number and
page offset segment offset
9 Page table is used to maintain the page Segment Table maintains the segment information
information.
10 Page table entry has the frame number and Segment table entry has the base address of the
some flag bits to represent details about pages. segment and some protection bits for the segments.
In this tutorial, we will cover some of the most common techniques used for structuring the Page table.
The data structure that is used by the virtual memory system in the operating system of a computer in
order to store the mapping between physical and logical addresses is commonly known as Page Table.
As we had already told you that the logical address that is generated by the CPU is translated into the
physical address with the help of the page table.
Thus page table mainly provides the corresponding frame number (base address of the frame) where
that page is stored in the main memory.
The above diagram shows the paging model of Physical and logical memory.
Generally; the Number of entries in the page table = the Number of Pages in which the process is divided.
PTBR means page table base register and it is basically used to hold the base address for the page table
of the current process.
Some of the common techniques that are used for structuring the Page table are as follows:
1. Hierarchical Paging
2. Hashed Page Tables
3. Inverted Page Tables
• There might be a case where the page table is too big to fit in a contiguous space, so we may
have a hierarchy with several levels.
• In this type of Paging the logical address space is broke up into Multiple page tables.
• Hierarchical Paging is one of the simplest techniques and for this purpose, a two-level page
table and three-level page table can be used.
Consider a system having 32-bit logical address space and a page size of 1 KB and it is further divided
into:
As we page the Page table, the page number is further divided into :
P2 indicates the displacement within the page of the Inner page Table.
As address translation works from outer page table inward so is known as forward-mapped Page Table.
Below given figure below shows the Address Translation scheme for a two-level page table
Three Level Page Table
For a system with 64-bit logical address space, a two-level paging scheme is not appropriate. Let us
suppose that the page size, in this case, is 4KB.If in this case, we will use the two-page level scheme
then the addresses will look like this:
Thus in order to avoid such a large table, there is a solution and that is to divide the outer page table,
and then it will result in a Three-level page table:
• The Virtual Page numbers are compared in this chain searching for a match; if the match is
found then the corresponding physical frame is extracted.
• In this scheme, a variation for 64-bit address space commonly uses clustered page tables.
• These are similar to hashed tables but here each entry refers to several pages (that is 16) rather
than 1.
• Mainly used for sparse address spaces where memory references are non-contiguous and
scattered
Given below figure shows the address translation scheme of the Inverted Page Table:
In this, we need to keep the track of process id of each entry, because many processes may
have the same logical addresses.
Also, many entries can map into the same index in the page table after going through the hash
function. Thus chaining is used in order to handle this.
6. Virtual Memory:
A computer can address more memory than the amount physically installed on the system. This
extra memory is actually called virtual memory and it is a section of a hard disk that's set up to
emulate the computer's RAM.
The main visible advantage of this scheme is that programs can be larger than physical
memory. Virtual memory serves two purposes. First, it allows us to extend the use of physical
memory by using disk. Second, it allows us to have memory protection, because each virtual
address is translated to a physical address.
Following are the situations, when entire program is not required to be loaded fully in main
memory.
User written error handling routines are used only when an error occurred in the data or
computation.
Many tables are assigned a fixed amount of address space even though only a small amount of
the table is actually used.
The ability to execute a program that is only partially in memory would counter many benefits.
Less number of I/O would be needed to load or swap each user program into memory.
A program would no longer be constrained by the amount of physical memory that is available.
Each user program could take less physical memory, more programs could be run the same
time, with a corresponding increase in CPU utilization and throughput.
Advantages
Following are the advantages of Demand Paging −
Large virtual memory.
More efficient use of memory.
There is no limit on degree of multiprogramming.
Disadvantages
Number of tables and the amount of processor overhead for handling page interrupts are greater than
in the case of the simple paged management techniques.
Reference String
The string of memory references is called reference string. Reference strings are generated artificially or
by tracing a given system and recording the address of each memory reference. The latter choice
produces a large number of data, where we note two things.
For a given page size, we need to consider only the page number, not the entire address.
If we have a reference to a page p, then any immediately following references to page p will never cause
a page fault. Page p will be in memory after the first reference; the immediately following references will
not fault.
For example, consider the following sequence of addresses − 123,215,600,1234,76,96
If page size is 100, then the reference string is 1,2,6,12,0,0
Individual programs face extra latency when they access a page for the first time.
Low-cost, low-power embedded systems may not have a memory management unit that supports
page replacement.
Memory management with page replacement algorithms becomes slightly more complex.
Possible security risks, including vulnerability to timing attacks; see Percival, Colin (2005-05-
Thrashing which may occur due to repeated page faults.
8. Copy on Writer:
Copy on Write or simply COW is a resource management technique. One of its main use is in the
implementation of the fork system call in which it shares the virtual memory(pages) of the OS.
In UNIX like OS, fork() system call creates a duplicate process of the parent process which is called as
the child process.
The idea behind a copy-on-write is that when a parent process creates a child process then both of
these processes initially will share the same pages in memory and these shared pages will be marked as
copy-on-write which means that if any of these processes will try to modify the shared pages then only
a copy of these pages will be created and the modifications will be done on the copy of pages by that
process and thus not affecting the other process.
Suppose, there is a process P that creates a new process Q and then process P modifies page 3.
The below figures shows what happens before and after process P modifies page 3.
9. Page Replacement:
While a user process is executing, a page fault occurs. The operating system determines where
the desired page is residing on the disk but then finds that there are no free frames on the free-frame
list: All memory is in use Figure below:
The operating system has several options at this point. It could terminate the user process.
However, demand paging is the operating system's attempt to improve the computer system's
utilization and throughput. Users should not be aware that their processes are running on a paged
system—paging should be logically transparent to the user. So this option is not the best choice.
The operating system could swap out a process, freeing all its frames and reducing the level of
multiprogramming. This option is a good one in certain circumstances; Here, we discuss the most
common solution: page replacement.
page-replacement algorithm
1. FIFO Page Replacement
The simplest page-replacement algorithm is a first-in, first-out (FIFO) algorithm. A FIFO replacement
algorithm associates with each page the time when that page was brought into memory. When a page
must be replaced, the oldest page is chosen. Notice that it is not strictly necessary to record the time
when a page is brought in. We can create a FIFO queue to hold all pages in memory. We replace the
page at the head of the queue. When a page is brought into memory, we insert it at the tail of the
queue.
For our example reference string, our three frames are initially empty. The first three references (7,
0, 1) cause page faults and are brought into these empty frames. The next reference (2) replaces page
7, because page 7 was brought in first. Since 0 is the next reference and 0 is already in memory, we
have no fault for this reference. The first reference to 3 results in replacement of page 0, since it is now
first in line. Because of this replacement, the next reference, to 0, will fault. Page 1 is then replaced by
page 0. This process continues as shown in Figure below. Every time a fault occurs, we show which
pages are in our three frames. There are fifteen faults altogether.
2. FIFO page-replacement algorithm.
The FIFO page-replacement algorithm is easy to understand and program. However, its performance
is not always good. On the one hand, the page replaced may be an initialization module that was
used a long time ago and is no longer needed. On the other hand, it could contain a heavily used
variable that was initialized early and is in constant use.
The key distinction between the FIFO and OPT algorithms is that the FIFO algorithm uses the time
when a page was brought into memory, whereas the OPT algorithm uses the time when a page is to
be used. If we use the recent past as an approximation of the near future, then we can replace the
page that has not been used for the longest period of time. This approach is the
LRU replacement associates with each page the time of that page’s last use. When a page must be
replaced, LRU chooses the page that has not been used for the longest period of time. We can think
of this strategy as the optimal page-replacement algorithm looking backward in time, rather than
forward. (Strangely, if we let SR be the reverse of a reference string S, then the page-fault
rate for the OPT algorithm on S is the same as the page-fault rate for the OPT algorithm on SR.
Similarly, the page-fault rate for the LRU algorithm on S is the same as the page-fault rate for the LRU
algorithm on SR.) The result of applying LRU replacement to our example reference string is shown in
Figure below:
The LRU algorithm produces twelve faults. Notice that the first five faults are the same as those for
optimal replacement. When the reference to page 4 occurs, however, LRU replacement sees that, of
the three frames in memory, page 2 was used least recently. Thus, the LRU algorithm replaces page 2,
not knowing that page 2 is about to be used. When it then faults for page 2, the LRU algorithm
replaces page 3, since it is now the least recently used of the three pages in memory. Despite these
problems, LRU replacement with twelve faults is much better than FIFO replacement with fifteen.
The LRU policy is often used as a page-replacement algorithm and is considered to be good. The
major problem is how to implement LRU replacement. An LRU page-replacement algorithm may
require substantial hardware assistance.
10.Allocation of Frames:
The OS has to allocate a sufficient number of frames for each process and to do so, the OS uses various
algorithms.
Disadvantage: A low priority process may hinder a high priority process by not making its frames
available to the high priority process.
This is a straightforward model. We must act depending on the frequency/rate of page faults and
assign frames to each process accordingly. For this page-fault rate, we specified an upper bound (UB)
and a lower bound (LB). We compare the page fault rate(R) of each process to the stated upper
bound(UB) and lower bound(LB).
If R > UB, we can deduce that a process requires additional frames to keep this rate under control. To
avoid thrashing, we'll need to dedicate extra frames to it. If there aren't any frames available, the
process can be paused until a sufficient number of frames becomes available. The allotted frames
should be assigned to another high-paging process once this process has been suspended.
We have more than enough frames for a process if R < LB, and some of them can be given to other
processes. We can maintain a balance between frame needs and frame allocation by using the R, UB,
and LB.
Locality Model
The locality model in thrashing is based on the observation that when a system is thrashing, a large
proportion of memory accesses are likely to be concentrated in a few pages of memory that are in high
demand. By identifying and prioritizing these pages, it may be possible to reduce the number of page
faults and alleviate the thrashing condition. This is because if the system can keep these high-demand
pages in physical memory, it will need to page them in and out less frequently, reducing the number of
disk I/O operations and improving system performance. The locality model can be applied using
techniques such as page replacement algorithms that aim to keep the most frequently accessed pages
in physical memory.
Read about Batch Operating System here.
Symptoms of Thrashing in OS and How to Detect it?
Thrashing might occur in OS when the system is overloaded with excessive paging, which results in
decreased performance of the OS. Here are the different ways to detect Thrashing in OS.
• Maximum cores of CPU being used, and little or no activity is done.
• Thrashing leads to the swapping of pages between the main memory and disk, so disk activity
increases rapidly.
• Frequent page faults can also be a reason for thrashing in OS.
Effects on System Performance and User Experience
• Thrashing in OS has various impacts on system performance and user experience.
• It decreases the overall performance of the system due to excessive cores of the CPU being used.
• Due to the swapping of pages between memory and disk, the response time for user interaction
gets increased, which reduces the user experience.
• Thrashing increases application loading time, which reduces the efficiency of the system
• Disk activity is increased rapidly, which reduces the performance of systems. Due to slow
performance and increased loading time of the system and applications, the user might get
frustrated, which depicts a bad user experience.