You are on page 1of 22

Memory Management

Is the process of controlling and coordinating computer memory, assigning


portions known as blocks to various running programs to optimize the
overall performance of the system.
It is the most important function of an operating system that manages
primary memory.
It helps processes to move back and forward between the main memory
and execution disk.
It helps OS to keep track of every memory location, irrespective of whether
it is allocated to some process or it remains free.
Memory Management Techniques are basic techniques that are used in
managing the memory in operating system. Memory Management
Techniques are basically classified into two categories:
Commonly resident monitor place in low memory and user program is
executed in high memory.
We need to protect the monitor code and data from changes by the
user program. This protection must be provided by the hardware and
can be implemented in several ways.
0 The general approach is every address generated by the user program
monitor is compared with a fence address.
fence
register Fence address(005000)
user

address true
Address>= memory
CCPU fence

32 false
k

Trap addressing error


If the generated address is greater than equal to the fence, then it is a legitimate reference to user memory
and is sent to the memory unit as usual.
If the generated address is less than the fence, the address is an illegal reference to monitor memory.
The OS executing in monitor mode is generally given unrestricted access to both monitor and user memory.
This provision allows the os to load user programs into user memory, dump them out in case of errors , access
and modify parameters of system calls and so on.
The fence register contains the address of the fence and is used to check the correctness of all user
memory references.
Fixed Partitioning (Static)
• In this method, memory is divided into partitions
whose sizes are fixed.
• OS is placed into the lowest bytes of memory.
• Processes are classified on entry to the system
according to their memory they requirements.
• If a process is selected to allocate memory, then
it goes into memory and competes for the
processor. The number of fixed partition gives
the degree of multiprogramming.
The main problem with the fixed partitioning method is how to determine the number of
partitions, and how to determine their sizes.
If a whole partition is currently not being used, then it is called an external fragmentation.
And if a partition is being used by a process requiring some memory smaller than the
partition size, then it is called an internal fragmentation.

In this composition of memory, if a new


process, P3, requiring 8 KB of memory comes,
although there is enough total space in
memory, it can not be loaded because
fragmentation.
Variable Partitioning (dynamic)
• With fixed partitions we have to deal with the problem of determining the number
and sizes of partitions to minimize internal and external fragmentation.
• If we use variable partitioning instead, then partition sizes may vary dynamically.
• In the variable partitioning method, we keep a table (linked list) indicating
used/free areas in memory. Initially, the whole memory is free and it is considered
as one large block.
• When a new process arrives, the OS searches for a block of free memory large
enough for that process. We keep the rest available (free) for the future processes.
OS

Memory
0
OS

40k

job memory time


1 60 10
2 100 5
3 30 20
4 70 8
5 50 15
256k
OS
OS OS OS OS
40 40 40 40
Job1 job5
job1 job1 job1 ends
90
100k 100 100 100
Job2 job4 job4 job4
job2 ends
170 170
170

200k 200 200 200


job3 job3 job3
job3 job3

230 230 230 230

256 256
256 256
There are three algorithms for searching the list of free blocks for a specific amount of
memory.

First Fit : Allocate the first free block that is large enough for the new process. This is a
fast algorithm.

Best Fit : Allocate the smallest block among those that are large enough for the new
process. In this method, the OS has to search the entire list, or it can keep it sorted and
stop when it hits an entry which has a size larger than the size of new process. This
algorithm produces the smallest left over block. However, it requires more time for
searching all the list or sorting it.

Worst Fit : Allocate the largest block among those that are large enough for the new
process. Again a search of the entire list or sorting it is needed. This algorithm produces
the largest over block.
Example 3.1 Consider the following memory map and assume a new process P4 comes with a memory
requirement of 3 KB. Locate this process.

a. Best fit algorithm allocates from the 4 KB block.


b. First fit algorithm allocates from the 10 KB block.
c. Worst fit algorithm allocates from the 16 KB block.
New memory arrangements with respect to each algorithms will be as follows:

At this point, if a new process, P5 of 14K arrives, then it would wait if we used worst
fit algorithm, whereas it would be located in cases of the others.
Disadvantages of Fixed Partitioning
1. Internal Fragmentation
If the size of the process is lesser then the total size of the partition then some size of
the partition get wasted and remain unused. This is wastage of the memory and called
internal fragmentation.
As shown in the image below, the 4 MB partition is used to load only 3 MB process and
the remaining 1 MB got wasted.
2. External Fragmentation
The total unused space of various partitions cannot be used to load the processes
even though there is space available but not in the contiguous form.
As shown in the image below, the remaining 1 MB space of each partition cannot be
used as a unit to store a 4 MB process. Despite of the fact that the sufficient space is
available to load the process, process will not be loaded.
3. Limitation on the size of the process
If the process size is larger than the size of maximum sized partition then that process
cannot be loaded into the memory. Therefore, a limitation can be imposed on the
process size that is it cannot be larger than the size of the largest partition.
4. Degree of multiprogramming is less
Advantages of Dynamic Partitioning over fixed partitioning

1. No Internal Fragmentation
Given the fact that the partitions in dynamic partitioning are created according to the
need of the process, It is clear that there will not be any internal fragmentation because
there will not be any unused remaining space in the partition.
2. 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 executed due to the lack of sufficient contiguous memory. Here, In Dynamic
partitioning, the process size can't be restricted since the partition size is decided
according to the process size.
3. Degree of multiprogramming is dynamic
Due to the absence of internal fragmentation, there will not be any unused space in the
partition hence more processes can be loaded in the memory at the same time.
Disadvantages of dynamic partitioning

External Fragmentation
Absence of internal fragmentation doesn't mean that there will not be external
fragmentation.
Complex Memory Allocation
In Fixed partitioning, the list of partitions is made once and will never change but in
dynamic partitioning, the allocation and deallocation is very complex since the partition
size will be varied every time when it is assigned to a new process. OS has to keep track
of all the partitions.
Due to the fact that the allocation and deallocation are done very frequently in
dynamic memory allocation and the partition size will be changed at each time, it is
going to be very difficult for OS to manage everything.
Compaction:
Compaction is a method to overcome the external fragmentation problem. All free blocks are brought
together as one large block of free space. Compaction requires dynamic relocation.
Certainly, compaction has a cost and selection of an optimal compaction strategy is difficult. One method
for compaction is swapping out those processes that are to be moved within the memory, and swapping
them into different memory locations.
Free Space Management Techniques

The memory space in the disk is limited. So we need to use the space of the deleted files for the allocation of
the new file.
The system maintains the free space list contains all the records of the free space disk block. These free blocks
are those which are not allocated to other file or directory.
When we create a file we first search for the free space in the memory and then check in the free space list for
the required amount of space that we require for our file.
If the free space is available then allocate this space to the new file. After that, the allocating space is deleted
from the free space list. Whenever we delete a file then its free memory space is added to the free space list.
The process of looking after and managing the free blocks of the disk is called free space
management. There are some methods or techniques to implement a free space list. These are as
follows:
•Bitmap
•Linked list

1. Bitmap
This technique is used to implement the free space management. When the free space is
implemented as the bitmap or bit vector then each block of the disk is represented by a bit. When
the block is free its bit is set to 1 and when the block is allocated the bit is set to 0.
The main advantage of the bitmap is it is relatively simple and efficient in finding the first free
block and also the consecutive free block in the disk. Many computers provide the bit
manipulation instruction which is used by the users.
For Example: Apple Macintosh operating system uses the bitmap method to allocate the disk
space.
Assume the following are free. Rest are allocated:

Advantages:
•This technique is relatively simple.
•This technique is very efficient to find the free space on the disk.
Disadvantages:
•This technique requires a special hardware support to find the first 1 in a word it is not 0.
•This technique is not useful for the larger disks.
2. Linked list
• This is another technique for free space management. In this linked list of all the
free block is maintained.
• In this, there is a head pointer which points the first free block of the list which
is kept in a special location on the disk.
• This block contains the pointer to the next block and the next block contain the
pointer of another next and this process is repeated.
• By using this disk it is not easy to search the free list. This technique is not
sufficient to traverse the list because we have to read each disk block that
requires I/O time.
Advantages:
•Whenever a file is to be allocated a free block, the operating system can
simply allocate the first block in free space list and move the head pointer to
the next free block in the list.
Disadvantages:
•Searching the free space list will be very time consuming; each block will have
to be read from the disk, which is read very slowly as compared to the main
memory.
•Not Efficient for faster access.

You might also like