L-2.1.3 Swapping, Fragmentation _ compaction

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

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.

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

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.

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.

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.

Note:

o In a single tasking operating system, only one process occupies the user program
area of memory and stays in memory until the process is complete.
o In a multitasking operating system, a situation arises when all the active
processes cannot coordinate in the main memory, then a process is swap out
from the main memory so that other processes can enter it.

Fragmentation

Both the first-fit and best-fit strategies for memory allocation suffer from external
fragmentation.
Internal Fragmentation

 It arises when we use fixed sized partitioning.


 Some part of the memory is kept for operating system and the rest is available for
user space.
 In this case the user space is divided into blocks of 10 KB each.
 When process 1 with size 8 KB is allocated a block of 10 KB, 2 KB space is left
unused. When process 2 with size 10 KB is allocated a !0 KB block no space is left
unused. When process 3 with size 9KB is allocated a 10 KB block, 1KB is left
unused.
 Here 2 processes are allocated space more than required and this unused space is so
small to store a new process and is wasted. This is called as INTERNAL
FRAGMENTATION.

External Fragmentation
 It arises when dynamic partitioning technique is used.
 Here memory is allocated to the processes dynamically based on their size.
 So in the above example, the user space contains processes 1,2 & 3 Out of which
process 1 & 3 complete their execution and are swapped out and two other
processes, process 4 & 5 are swapped in their places.
 Process 4 takes place of process 1 but as its size is only 8 KB, it is allocated only 8
KB and rest is left unused.
 Process 5 takes place of process 3. It is allocated 6 KB space and 8 KB is left
unused.
 Now suppose a new process, process 6 wants to be swapped in and its size is 6 KB.
Though we have total 6 KB space but we cannot service this request as these blocks
are not contiguous(adjacent). This is called as EXTERNAL
FRAGMENTATION.

BASIS FOR INTERNAL EXTERNAL


COMPARISON FRAGMENTATION FRAGMENTATION
Basic It occurs when fixed sized It occurs when variable
memory blocks are size memory space are
allocated to the processes. allocated to the
processes dynamically.
Occurrence When the memory When the process is
assigned to the process is removed from the
slightly larger than the memory, it creates the
memory requested by the free space in the
process this creates free memory causing
space in the allocated external fragmentation.
block causing internal
fragmentation.
Solution The memory must be Compaction, paging
partitioned into variable and segmentation.
sized blocks and assign
the best fit block to the
process.

Compaction
 Compaction is a process in which the free space is collected in a large memory chunk to
make some space available for processes.
 In memory management, swapping creates multiple fragments in the memory because of
the processes moving in and out.
 Compaction refers to combining all the empty spaces together and processes.
 Compaction helps to solve the problem of fragmentation, but it requires too much of CPU
time.
 It moves all the occupied areas of store to one end and leaves one large free space for
incoming jobs, instead of numerous small ones.

 In compaction, the system also maintains relocation information and it must be performed
on each new allocation of job to the memory or completion of job from memory.

We got to know that the dynamic partitioning suffers from external fragmentation.
However, this can cause some serious problems.

To avoid compaction, we need to change the rule which says that the process can't be
stored in the different places in the memory.

We can also use compaction to minimize the probability of external fragmentation. In


compaction, all the free partitions are made contiguous and all the loaded partitions are
brought together.

By applying this technique, we can store the bigger processes in the memory. The free
partitions are merged which can now be allocated according to the needs of new
processes. This technique is also called defragmentation.
As shown in the image above, the process P5, which could not be loaded into the
memory due to the lack of contiguous space, can be loaded now in the memory since the
free partitions are made contiguous.

Problem with Compaction


The efficiency of the system is decreased in the case of compaction due to the fact that
all the free spaces will be transferred from several places to a single place.

Huge amount of time is invested for this procedure and the CPU will remain idle for all
this time. Despite of the fact that the compaction avoids external fragmentation, it
makes system inefficient.

Let us consider that OS needs 6 NS to copy 1 byte from one place to another.

1. 1 B transfer needs 6 NS
2. 256 MB transfer needs 256 X 2^20 X 6 X 10 ^ -9 secs

hence, it is proved to some extent that the larger size memory transfer needs some huge
amount of time that is in seconds.

Video Link:
https://www.youtube.com/watch?v=SqYigYLFvcI

https://www.youtube.com/watch?v=ZN-baY3x85o
https://www.youtube.com/watch?v=buRdtPIieOM

References:
https://www.geeksforgeeks.org/partition-allocation-methods-in-memory-management/

https://www.javatpoint.com/os-memory-management-introduction

http://www2.latech.edu/~box/os/ch08.pdf

https://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/
8_MainMemory.html#:~:text=8.3%20Contiguous%20Memory%20Allocation,allocated%20to
%20processes%20as%20needed.

http://www.csdl.tamu.edu/~furuta/courses/99a_410/slides/chap08

You might also like