Unit-III (Memory Management)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Unit-3
Memory Management
Background
Memory management is the functionality of an operating system which handles or manages primary
memory and moves processes back and forth between main memory and disk during execution. Memory
management keeps track of each and every memory location, regardless of either it is allocated to some
process or it is free. It checks how much memory is to be allocated to processes. It decides which
process will get memory at what time. It tracks whenever some memory gets free or unallocated and
correspondingly it updates the status.

Swapping in Operating System

Swapping is a memory management scheme in which any process can be temporarily swapped from main
memory to secondary memory so that the main memory can be made available for other processes. It is
used to improve main memory utilization. In secondary memory, the place where the swapped-out
process is stored is called swap space.

The purpose of the swapping in operating system is to access the data present in the hard disk and bring it
to RAM so that the application programs can use it. The thing to remember is that swapping is used only
when data is not present in RAM.

Although the process of swapping affects the performance of the system, it helps to run larger and more
than one process. This is the reason why swapping is also referred to as memory compaction.

The concept of swapping has divided into two more concepts: Swap-in and Swap-out.

 Swap-out is a method of removing a process from RAM and adding it to the hard disk.
 Swap-in is a method of removing a program from a hard disk and putting it back into the main
memory or RAM.

BY: - S. K. Yadav Page 1


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Example: Suppose the user process's size is 2048KB and is a standard hard disk where swapping has a
data transfer rate of 1Mbps. Now we will calculate how long it will take to transfer from main memory to
secondary memory.

soln.:

1. User process size is 2048Kb


2. Data transfer rate is 1Mbps = 1024 kbps
3. Time = process size / transfer rate
4. = 2048 / 1024
5. = 2 seconds
6. = 2000 milliseconds
7. Now taking swap-in and swap-out time, the process will take 4000 milliseconds.

Advantages of Swapping

1. It helps the CPU to manage multiple processes within a single main memory.
2. It helps to create and use virtual memory.
3. Swapping allows the CPU to perform multiple tasks simultaneously. Therefore, processes do not
have to wait very long before they are executed.
4. It improves the main memory utilization.

BY: - S. K. Yadav Page 2


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Disadvantages of Swapping

1. If the computer system loses power, the user may lose all information related to the program in
case of substantial swapping activity.
2. If the swapping algorithm is not good, the composite method can increase the number of Page
Fault and decrease the overall processing performance.

Fragmentation:

As processes are loaded and removed from memory, the free memory space is broken into little pieces. It
happens after sometimes that processes cannot be allocated to memory blocks considering their small
size and memory blocks remains unused. This problem is known as Fragmentation.
Fragmentation is of two types −

S.N. Fragmentation & Description

1
External fragmentation
Total memory space is enough to satisfy a request or to reside a process in it, but it is not contiguous,
so it cannot be used.

2
Internal fragmentation
Memory block assigned to process is bigger. Some portion of memory is left unused, as it cannot be
used by another process.

The following diagram shows how fragmentation can cause waste of memory and a compaction
technique can be used to create more free memory out of fragmented memory −

BY: - S. K. Yadav Page 3


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

External fragmentation can be reduced by compaction or shuffle memory contents to place all free
memory together in one large block. To make compaction feasible, relocation should be dynamic.
The internal fragmentation can be reduced by effectively assigning the smallest partition but large
enough for the process.

Memory Management Techniques:

There are two Memory Management Techniques: Contiguous, and Non-Contiguous. In


Contiguous Technique, executing process must be loaded entirely in main-memory.
Contiguous memory allocation

The Contiguous memory allocation is one of the methods of memory allocation. In contiguous
memory allocation, when a process requests for the memory, a single contiguous section of memory
blocks is assigned to the process according to its requirement.
 All the available memory space remains together in one place. It means freely available
memory partitions are not expanded here and there across the whole memory space.
 Both the operating system and the user must reside in the main memory. The main memory
is divided into two portions one portion is for the operating and other is for the user program.
 When any user process request for the memory a single section of the contiguous memory
block is given to that process according to its need. We can achieve contiguous memory
allocation by dividing memory into the fixed-sized partition.
A single process is allocated in that fixed sized single partition. But this will increase the degree of
multiprogramming means more than one process in the main memory that bounds the number of
fixed partition done in memory. Internal fragmentation increases because of the contiguous memory
allocation.

Contiguous Technique can be divided into:


1. Fixed (or static) partitioning
2. Variable (or dynamic) partitioning

BY: - S. K. Yadav Page 4


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

1. Fixed (or static) Partitioning:

This is the oldest and simplest technique used to put more than one processes in the main memory. In
this partitioning, number of partitions (non-overlapping) in RAM is fixed but size of each partition
may or may not be same. As it is contiguous allocation, hence no spanning is allowed. Here partition
are made before execution or during system configure.

As illustrated in above figure, first process is only consuming 1MB out of 4MB in the main memory.
Hence, Internal Fragmentation in first block is (4-1) = 3MB.
Sum of Internal Fragmentation in every block = (4-1)+(8-7)+(8-7)+(16-14)= 3+1+1+2 = 7MB.

Suppose process P5 of size 7MB comes. But this process cannot be accommodated in spite of available
free space because of contiguous allocation (as spanning is not allowed). Hence, 7MB becomes part of
External Fragmentation.
There are some advantages and disadvantages of fixed partitioning.
Advantages of Fixed Partitioning :–

1. Easy to implement:
Algorithms needed to implement Fixed Partitioning are easy to implement. It simply requires
putting a process into certain partition without focusing on the emergence of Internal and External
Fragmentation.
2. Little OS overhead:
Processing of Fixed Partitioning require lesser excess and indirect computational power.

BY: - S. K. Yadav Page 5


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Disadvantages of Fixed Partitioning: –


1. Internal Fragmentation:
Main memory use is inefficient. Any program, no matter how small, occupies an entire partition.
This can cause internal fragmentation.
2. External Fragmentation:
The total unused space (as stated above) of various partitions cannot be used to load the processes
even though there is space available but not in the contiguous form (as spanning is not allowed).
3. Limit process size:
Process of size greater than size of partition in Main Memory cannot be accommodated. Partition
size cannot be varied according to the size of incoming process’s size. Hence, process size of
32MB in above stated example is invalid.
4. Limitation on Degree of Multiprogramming:
Partition in Main Memory are made before execution or during system configure. Main Memory is
divided into fixed number of partition. Suppose if there are partitions in RAM and are
the number of processes, then condition must be fulfilled. Number of processes
greater than number of partitions in RAM is invalid in Fixed Partitioning.

2. Variable (or dynamic) Partitioning: –

It is a part of Contiguous allocation technique. It is used to alleviate/ligjter the problem faced by Fixed
Partitioning. In contrast with fixed partitioning, partitions are not made before the execution or during
system configure. Various features associated with variable Partitioning-
1. Initially RAM is empty and partitions are made during the run-time according to process’s need
instead of partitioning during system configure.
2. The size of partition will be equal to incoming process.
3. The partition size varies according to the need of the process so that the internal fragmentation can
be avoided to ensure efficient utilization of RAM.
4. Number of partitions in RAM is not fixed and depends on the number of incoming process and
Main Memory’s size.

BY: - S. K. Yadav Page 6


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

There are some advantages and disadvantages of variable partitioning over fixed partitioning as given
below.

Advantages of Variable Partitioning: –

1. No Internal Fragmentation:
In variable Partitioning, space in main memory is allocated strictly according to the need of
process, hence there is no case of internal fragmentation. There will be no unused space left in the
partition.
2. No restriction on Degree of Multiprogramming:
More number of processes can be accommodated due to absence of internal fragmentation. A
process can be loaded until the memory is empty.
3. No Limitation on the size of the process:
In Fixed partitioning, the process with the size greater than the size of the largest partition could not
be loaded and process cannot be divided as it is invalid in contiguous allocation technique. Here, in
variable partitioning, the process size can’t be restricted since the partition size is decided
according to the process size.
Disadvantages of Variable Partitioning: –
1. Difficult Implementation:
Implementing variable Partitioning is difficult as compared to Fixed Partitioning as it involves
allocation of memory during run-time rather than during system configure.
2. External Fragmentation:
There will be external fragmentation in spite of absence of internal fragmentation.
For example, suppose in above example- process P1(2MB) and process P3(1MB) completed their
execution. Hence two spaces are left i.e. 2MB and 1MB. Let’s suppose process P5 of size 3MB
comes. The empty space in memory cannot be allocated as no spanning is allowed in contiguous
allocation. The rule says that process must be contiguously present in main memory to get
executed. Hence it results in External Fragmentation.

BY: - S. K. Yadav Page 7


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Now P5 of size 3 MB cannot be accommodated in spite of required available space because in


contiguous no spanning is allowed.

Non-contiguous memory allocation :

The Non-contiguous memory allocation allows a process to acquire the several memory blocks at the
different location in the memory according to its requirement. The non-contiguous memory allocation
also reduces the memory wastage caused due to internal and external fragmentation. As it utilizes the
memory holes, created during internal and external fragmentation.

i. The available free memory space are scattered here and there and all the free memory space is
not at one place. So this is time-consuming.
ii. A process will acquire the memory space but it is not at one place it is at the different locations
according to the process requirement.
iii. It reduces the wastage of memory which leads to internal and external fragmentation. This
utilizes all the free memory space which is created by a different process.

Non-contiguous memory allocation is of different types,


1. Paging
2. Segmentation
3. Segmentation with paging

Paging:

Paging is a storage mechanism that allows OS to retrieve processes from the secondary storage into the
main memory in the form of pages. In the Paging method, the main memory is divided into small fixed-
size blocks of physical memory, which is called frames. The size of a frame should be kept the same as
that of a page to have maximum utilization of the main memory and to avoid external fragmentation.
Paging is used for faster access to data, and it is a logical concept.

Example:

For example, if the main memory size is 16 KB and Frame size is 1 KB. Here, the main memory will be
divided into the collection of 16 frames of 1 KB each.

There are 4 separate processes in the system that is A1, A2, A3, and A4 of 4 KB each. Here, all the
processes are divided into pages of 1 KB each so that operating system can store one page in one frame.

At the beginning of the process, all the frames remain empty so that all the pages of the processes will
get stored in a contiguous way.

BY: - S. K. Yadav Page 8


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

In this example you can see that A2 and A4 are moved to the waiting state after some time. Therefore,
eight frames become empty, and so other pages can be loaded in that empty blocks. The process A5 of
size 8 pages (8 KB) are waiting in the ready queue.

BY: - S. K. Yadav Page 9


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

In this example, you can see that there are eight non-contiguous frames which is available in the memory,
and paging offers the flexibility of storing the process at the different places. This allows us to load the
pages of process A5 instead of A2 and A4.

Advantages of Paging:

Here, are advantages of using Paging method:

 No need for external Fragmentation


 Swapping is easy between equal-sized pages and page frames.

Disadvantages of Paging:

Here, are drawbacks of Paging:

 Complex memory management algorithm


 Page tables consume additional memory.

BY: - S. K. Yadav Page 10


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Segmentation:

Segmentation method works almost similarly to paging, only difference between the two is that segments
are of variable-length whereas, in the paging method, pages are always of fixed size.

A program segment includes the program's main function, data structures, utility functions, etc. The OS
maintains a segment map table for all the processes. It also includes a list of free memory blocks along
with its size, segment numbers, and it's memory locations in the main memory or virtual memory.

Advantages of a Segmentation method

Here, are benefits of Segmentation

 Offer protection within the segments


 Not offers internal fragmentation
 Segment tables use lesser memory than paging

Disadvantages of Segmentation

Here are drawbacks of Segmentation

 In segmentation method, processes are loaded/ removed from the main memory. Therefore, the
free memory space is separated into small pieces which may create a problem of external
fragmentation
 Costly memory management algorithm

SEGMENTATION WITH PAGING:


In segmentation with paging, we take advantages of both segmentation as well as paging. It is a kind
of multilevel paging but in multilevel paging, we divide a page table into equal size partition but here
in segmentation with paging, we divide it according to segments. All the properties are the same as
that of paging because segments are divided into pages.

BY: - S. K. Yadav Page 11


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Virtual Memory:

Virtual Memory is a storage mechanism which offers user an illusion of having a very big main
memory. It is done by treating a part of secondary memory as the main memory. In Virtual memory, the
user can store processes with a bigger size than the available main memory.

Therefore, instead of loading one long process in the main memory, the OS loads the various parts of
more than one process in the main memory. Virtual memory is mostly implemented with demand paging
and demand segmentation.

Advantages of Virtual Memory:

Here, are pros/benefits of using Virtual Memory:

 Virtual memory helps to gain speed when only a particular segment of the program is
required for the execution of the program.
 It is very helpful in implementing a multiprogramming environment.
 It allows you to run more applications at once.
 It helps you to fit many large programs into smaller programs.
 More processes should be maintained in the main memory, which increases the effective
use of CPU.
 Each page is stored on a memory until it is required after that, it will be removed.
 It allows more applications to be run at the same time.
 There is no specific limit on the degree of multiprogramming.

Disadvantages of Virtual Memory:

Here, are drawbacks/cons of using virtual memory:

 Applications may run slower if the system is using virtual memory.


 Likely takes more time to switch between applications.
 Offers lesser memory space for your use.
 It doesn't offer the same performance as RAM.
 Occupy the storage space, which may be used otherwise for long term data storage.

Demand Paging:

A demand paging system is quite similar to a paging system with swapping where processes reside in
secondary memory and pages are loaded only on demand, not in advance. When a context switch occurs,
the operating system does not copy any of the old program’s pages out to the disk or any of the new
program’s pages into the main memory. Instead, it just begins executing the new program after loading
the first page and fetches that program’s pages as they are referenced.

BY: - S. K. Yadav Page 12


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Fig: Demand Paging

While executing a program, if the program references a page which is not available in the main memory
because it was swapped out a little ago, the processor treats this invalid memory reference as a page
fault and transfers control from the program to the operating system to demand the page back into the
memory.

Advantages

Following are the advantages of Demand Paging −

 Reduce memory requirement.


 Swap time is also reduced.
 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.

BY: - S. K. Yadav Page 13


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

 Page fault can occur.

Page Replacement Algorithm:

Page replacement algorithms are the techniques using which an Operating System decides which memory
pages to swap out, write to disk when a page of memory needs to be allocated. Paging happens whenever
a page fault occurs and a free page cannot be used for allocation purpose accounting to reason that pages
are not available or the number of free pages is lower than required pages.

When the page that was selected for replacement and was paged out, is referenced again, it has to read in
from disk, and this requires for I/O completion. This process determines the quality of the page
replacement algorithm: the lesser the time waiting for page-ins, the better is the algorithm.
A page replacement algorithm looks at the limited information about accessing the pages provided by
hardware, and tries to select which pages should be replaced to minimize the total number of page
misses, while balancing it with the costs of primary storage and processor time of the algorithm itself.
There are many different page replacement algorithms. We evaluate an algorithm by running it on a
particular string of memory reference and computing the number of page faults,
OR,
The page replacement algorithm decides which memory page is to be replaced. The process of
replacement is sometimes called swap out or write to disk. Page replacement is done when the requested
page is not found in the main memory (page fault).

Reference String: It means CPU is going to request for the pages.

OR,

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. ... Page p will be in
memory after the first reference; the immediately following references will not fault.

Types of Page Replacement Methods:

Here, are some important Page replacement methods

 FIFO Page Replacement Algorithm


 Optimal Page Replacement Algorithm
 LRU Page Replacement Algorithm

FIFO Page Replacement Algorithm:

FIFO (First-in-first-out) is a simple implementation method. In this method, memory selects the page for
a replacement that has been in the virtual address of the memory for the longest time.

BY: - S. K. Yadav Page 14


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Features:

 Whenever a new page loaded, the page previously comes in the memory is removed. So,
it is easy to decide which page requires to be removed as its identification number is
always at the FIFO stack.
 The oldest page in the main memory is one that should be selected for replacement first.

Example1: Suppose a sequence of string with number of frame (slot) is given. Calculate the page
fault, page hit, hit rate and fault rate using FIFO page replacement algorithm..
Reference String: 1,3,0,3,5,6
No. of frames: 3

soln.: -

0 0 0 0

3 3 3 3 6

1 1 1 1 5 5

F F F H F F

Page fault=5
Page hit=1
Hit rate= No. of page Hits/No. of references
=1/6
=0.166
Page fault rate= No. of page faults/No. of references
=5/6
=0.83
Example2: Suppose a sequence of string with number of frame(slot) is given. Calculate the page
fault, page hit, hit rate and fault rate using FIFO page replacement algorithm..
Reference String: 3,2,1,0,3,2,4,3,2,1,0,4
No. of frames: 4

soln.:-

0 0 0 0 0 0 1 1 1

1 1 1 1 1 1 2 2 2 2

2 2 2 2 2 2 3 3 3 3 4

3 3 3 3 3 3 4 4 4 4 0 0

F F F F H H F F F F F F

BY: - S. K. Yadav Page 15


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Page fault=10
Page hit=2
Hit rate= No. of page Hits/No. of references
=2/12
=0.16
Page fault rate= No. of page faults/No. of references
=10/12
=0.83

Example3: Suppose a sequence of string with number of frame(slot) is given. Calculate the page
fault, page hit, hit rate and fault rate using FIFO page replacement algorithm..
Reference String: 7,0,1,2,0,3,0,4,2,3,0,3,1,2,0
No. of frames: 3

soln.:-

1 1 1 1 0 0 0 3 3 3 3 2 2

0 0 0 0 3 3 3 2 2 2 2 1 1 1

7 7 7 2 2 2 2 4 4 4 0 0 0 0 0

F F F F H F F F F F F H F F H

Page fault=12
Page hit=3
Hit rat= No. of page Hits/No. of references
=3/15
=0.2
Page fault rate= No. of page faults/No. of references
=12/15
=0.8

Optimal Page Replacement Algorithm:

The optimal page replacement method selects that page for a replacement for which the time to the next
reference is the longest.

Features:

 Optimal algorithm results in the fewest number of page faults. This algorithm is difficult to
implement.

BY: - S. K. Yadav Page 16


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

 An optimal page-replacement algorithm method has the lowest page-fault rate of all algorithms.
This algorithm exists and which should be called MIN or OPT.

Example1: Suppose a sequence of string with number of frame(slot) is given. Calculate the page
fault, page hit, hit rate and fault rate using optimal page replacement algorithm..
Reference String: 4,7,6,1,7,6,1,2,7,2
No. of frames: 3

soln.:-

6 6 6 6 6 2 2 2

7 7 7 7 7 7 7 7 7

4 4 4 1 1 1 1 1 1 1

F F F F H H H F H H

Page fault=5
Page hit=5
Hit rate= No. of page Hits/No. of references
=5/10
=0.5
Page fault rate= No. of page faults/No. of references
=5/10
=0.5
Example2: Suppose a sequence of string with number of frame(slot) is given. Calculate the page
fault, page hit, hit rate and fault rate using optimal page replacement algorithm..
Reference String: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
No. of frames: 4

soln.:-

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

1 1 1 1 1 4 4 4 4 4 4 1 1 1 1 1 1 1

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

7 7 7 7 7 3 3 3 3 3 3 3 3 3 3 3 3 7 7 7

F F F F H F H F H H H H H F H H H F H H

BY: - S. K. Yadav Page 17


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Page fault=8
Page hit=12
Hit rate= No. of page Hits/No. of references
=12/20
=0.6
Page fault rate= No. of page faults/No. of references
=8/20
=0.4

LRU Page Replacement Algorithm:

The full form of LRU is the Least Recently Used page. This method helps OS to find page usage over a
short period of time. This algorithm should be implemented by associating a counter with an even- page.

How does it work?

 Page, which has not been used for the longest time in the main memory, is the one that
will be selected for replacement.
 Easy to implement, keep a list, replace pages by looking back into time.

Features:

 The LRU replacement method has the highest count. This counter is also called aging
registers, which specify their age and how much their associated pages should also be
referenced.
 The page which hasn't been used for the longest time in the main memory is the one that
should be selected for replacement.
 It also keeps a list and replaces pages by looking back into time.

Example1: Suppose a sequence of string with number of frame(slot) is given. Calculate the page
fault, page hit, hit rate and fault rate using LRU page replacement algorithm..
Reference String: 7,0,1,2,0,3,0,4,2,3,0,3,2
No. of frames: 4

soln.:-
2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 4 4 4 4 4 4
0 0 0 0 0 0 0 0 0 0 0 0

7 7 7 7 7 3 3 3 3 3 3 3 3
F F F F H F H F H H H H H

BY: - S. K. Yadav Page 18


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

Page fault=6
Page hit=7
Hit rate= No. of page Hits/No. of references
=7/13
=0.53
Page fault rate= No. of page faults/No. of references
=6/13
=0.46

Example2: Suppose a sequence of string with number of frame(slot) is given. Calculate the page
fault, page hit, hit rate and fault rate using LRU page replacement algorithm..
Reference String: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
No. of frames: 4

soln.:-

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

1 1 1 1 1 4 4 4 4 4 4 1 1 1 1 1 1 1

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

7 7 7 7 7 3 3 3 3 3 3 3 3 3 3 3 3 7 7 7

F F F F H F H F H H H H H F H H H F H H

Page fault=8
Page hit=12
Hit rate= No. of page Hits/No. of references
=12/20
=0.6
Page fault rate= No. of page faults/No. of references
=8/20
=0.4

Thrashing:
In computer science, thrashing occurs when a computer's virtual memory resources are overused, leading
to a constant state of paging and page faults, inhibiting most application-level processing. This causes the
performance of the computer to degrade or collapse.
Or,
Thrashing is a computer activity that makes little or no progress. Usually, this happens either of limited
resources or exhaustion of memory.
It arises when a page fault occurs. Page fault arises when memory access of virtual memory space does
not map to the content of RAM.

BY: - S. K. Yadav Page 19


HCAC-155: Operating System BCA 2nd Semester|Rajrshi Janak University

The effect of Thrashing is


1.CPU becomes idle.
2. Decreasing the utilization increases the degree of multiprogramming and hence bringing more
processes at a time which in fact increases the thrashing exponentially.

Fig: Thrashing

BY: - S. K. Yadav Page 20

You might also like