unit 3
unit 3
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.
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.
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.
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.
2. Little OS overhead:
Processing of Fixed Partitioning requires lesser excess and indirect computational power.
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).
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.