0% found this document useful (0 votes)
12 views

unit 3

Uploaded by

Akansha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

unit 3

Uploaded by

Akansha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Memory management

The term Memory can be defined as a collection of data in a specific format. It is used to store
instructions and processed data. The memory comprises a large array or group of words or
bytes, each with its own location. The primary motive of a computer system is to execute
programs. These programs, along with the information they access, should be in the main
memory during execution. The CPU fetches instructions from memory according to the value
of the program counter.
To achieve a degree of multiprogramming and proper utilization of memory, memory
management is important.
What is memory management?

Memory management is a method in the operating system to manage operations between main
memory and disk during process execution. The main aim of memory management is to
achieve efficient utilization of memory.

 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 freed or unallocated and correspondingly it updates the status.
All the programs are loaded in the main memory for execution. Sometimes the complete
program is loaded into the memory, but sometimes a certain part or routine of the program
is loaded into the main memory only when it is called by the program, this mechanism is
called Dynamic Loading, which enhances the performance.

Also, at times one program is dependent on some other program. In such a case, rather than
loading all the dependent programs, the CPU links the dependent programs to the main
executing program when required. This mechanism is known as Dynamic Linking.

Why Memory Management is required?


Here, are reasons for using memory management:
 This technique helps in placing the programs in memory in such a way so that memory is
utilized at its fullest extent.
 This technique helps to protect different processes from each other so that they do not
interfere with each other's operations.
 It helps to allocate space to different application routines.
 This technique allows you to check how much memory needs to be allocated to processes
that decide which processor should get memory at what time.
 It keeps the track of each memory location whether it is free or allocated.
 This technique keeps the track of inventory whenever memory gets freed or unallocated
and it will update the status accordingly.
Swapping:
Swapping is a memory management technique and is used to temporarily remove the inactive
programs from the main memory of the computer system. Any process must be in the memory
for its execution, but can be swapped temporarily out of memory to a backing store and then
again brought back into the memory to complete its execution. Swapping is done so that other
processes get memory for their execution.

Due to the swapping technique performance usually gets affected, but it also helps in running
multiple and big processes in parallel. The swapping process is also known as a technique
for memory compaction. Basically, low priority processes may be swapped out so that
processes with a higher priority may be loaded and executed.

Let us understand this technique with the help of a figure given below:

The above diagram shows swapping of two processes where the disk is used as a Backing store.

In the above diagram, suppose there is a multiprogramming environment with a round-robin


scheduling algorithm; whenever the time quantum expires then the memory manager starts to
swap out those processes that are just finished and swap another process into the memory that
has been freed. And in the meantime, the CPU scheduler allocates the time slice to some other
processes in the memory.
The swapping of processes by the memory manager is fast enough that some processes will be in
memory, ready to execute, when the CPU scheduler wants to reschedule the CPU.

A variant of the swapping technique is the priority-based scheduling algorithm. If any higher-
priority process arrives and wants service, then the memory manager swaps out lower priority
processes and then load the higher priority processes and then execute them. When the process
with higher priority finishes .then the process with lower priority swapped back in and continues
its execution. This variant is sometimes known as roll in and roll out.

There are two more concepts that come in the swapping technique and these are: swap
in and swap out.

Swap In and Swap Out in OS

The procedure by which any process gets removed from the hard disk and placed in the main
memory or RAM commonly known as Swap In.

On the other hand, Swap Out is the method of removing a process from the main memory or
RAM and then adding it to the Hard Disk.

Advantages of Swapping

1. The swapping technique mainly helps the CPU to manage multiple processes within a
single main memory.
2. This technique helps to create and use virtual memory.
3. With the help of this technique, the CPU can perform several tasks simultaneously. Thus,
processes need not wait too long before their execution.
4. This technique is economical.
5. This technique can be easily applied to priority-based scheduling in order to improve its
performance.

Disadvantages of Swapping

1. There may occur inefficiency in the case if a resource or a variable is commonly used by
those processes that are participating in the swapping process.
2. If the algorithm used for swapping is not good then the overall method can increase the
number of page faults and thus decline the overall performance of processing.
3. If the computer system loses power at the time of high swapping activity then the user
might lose all the information related to the program.
Memory allocation technique:
Memory partitioning is the system by which the memory of a computer system is divided
into sections for use by the resident programs. These memory divisions are known as
partitions. There are different ways in which memory can be partitioned: fixed and variable
partitioning.
There are two Memory Management Techniques: Contiguous, and Non-Contiguous.

Contiguous: In the contiguous memory allocation, both the operating system and the user
space for processes 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. Contiguous
Technique, executing process must be loaded entirely in main-memory. Contiguous
Technique can be divided into:
1. Fixed (or static) partitioning
2. Variable (or dynamic) partitioning

Non contiguous:

 In the non-contiguous memory allocation 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.
 In the non-contiguous memory allocation, 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.
 This technique of non-contiguous memory allocation 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.
1. Fixed size partitioning:
 This technique is also known as static partitioning. In this scheme, the system
divides the memory into fixed-size partitions.
 The partitions may or may not be the same size. The size of each partition is fixed
as indicated by the name of the technique and it cannot be changed. In this
partition scheme, each partition may contain exactly one process.
 There is a problem that this technique will limit the degree of multiprogramming
because the number of partitions will basically decide the number of processes.
Free= 1 MB

Free= 1 MB

Free= 2 MB

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.

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 a certain partition without focusing on the emergence of
Internal and External Fragmentation.

2. Little OS overhead:
Processing of Fixed Partitioning requires lesser excess and indirect computational power.

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 the size of the partition in Main Memory cannot be
accommodated. The partition size cannot be varied according to the size of the incoming
process size. Hence, the process size of 32MB in the above-stated example is invalid.
4. Limitation on Degree of Multiprogramming:
Partitions in Main Memory are made before execution or during system configure. Main
Memory is divided into a fixed number of partitions.

2. Variable (or dynamic) partitioning


It is a part of Contiguous allocation technique. It is used to alleviate 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.
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 any 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 inspite of absence of internal fragmentation.

In Partition Allocation, when there is more than one partition freely available to
accommodate a process’s request, a partition must be selected. To choose a particular
partition, a partition allocation method is needed. A partition allocation method is
considered better if it avoids internal fragmentation.
When it is time to load a process into the main memory and if there is more than one
free block of memory of sufficient size then the OS decides which free block to
allocate.
There are different Placement Algorithm:
A. First Fit
B. Best Fit
C. Worst Fit

1. First Fit: In the first fit, the partition is allocated which is the first sufficient block from
the top of Main Memory. It scans memory from the beginning and chooses the first
available block that is large enough. Thus it allocates the first hole that is large enough.
Advantages of First-Fit Memory Allocation:
It is fast in processing. As the processor allocates the nearest available memory
partition to the job, it is very fast in execution.
Disadvantages of First-Fit Memory Allocation :
It wastes a lot of memory. The processor ignores if the size of partition allocated to the
job is very large as compared to the size of job or not. It just allocates the memory. As a
result, a lot of memory is wasted and many jobs may not get space in the memory, and
would have to wait for another job to complete.

2. Best Fit Allocate the process to the partition which is the first smallest sufficient
partition among the free available partition. It searches the entire list of holes to find the
smallest hole whose size is greater than or equal to the size of the process.

Advantages of Best-Fit Allocation :


Memory Efficient. The operating system allocates the job minimum possible space in
the memory, making memory management very efficient. To save memory from getting
wasted, it is the best method.
Disadvantages of Best-Fit Allocation :
It is a Slow Process. Checking the whole memory for each job makes the working of
the operating system very slow. It takes a lot of time to complete the work.
3. Worst Fit Allocate the process to the partition which is the largest sufficient among the
freely available partitions available in the main memory. It is opposite to the best-fit
algorithm. It searches the entire list of holes to find the largest hole and allocate it to
process.

Advantages of Worst-Fit Allocation :


Since this process chooses the largest hole/partition, therefore there will be large
internal fragmentation. Now, this internal fragmentation will be quite big so that other
small processes can also be placed in that leftover partition.
Disadvantages of Worst-Fit Allocation :
It is a slow process because it traverses all the partitions in the memory and then selects
the largest partition among all the partitions, which is a time-consuming process.

You might also like