Memory Management - PQ
Memory Management - PQ
It is used to store
instructions and process data. The memory comprises a large array or group of words or bytes, each
with its own location. The primary purpose 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. Many memory management methods exist, reflecting various approaches, and the
effectiveness of each algorithm depends on the situation.
Here, we will cover the following memory management topics:
What is Main Memory?
What is Memory Management?
Why Memory Management is Required?
Logical Address Space and Physical Address Space
Static and Dynamic Loading
Static and Dynamic Linking
Swapping
Contiguous Memory Allocation
o Memory Allocation
o First Fit
o Best Fit
o Worst Fit
o Fragmentation
o Internal Fragmentation
o External Fragmentation
o Paging
Before we start Memory management, let us know what is main memory is.
What is Main Memory?
The main memory is central to the operation of a Modern Computer. Main Memory is a large array of
words or bytes, ranging in size from hundreds of thousands to billions. Main memory is a repository of
rapidly available information shared by the CPU and I/O devices. Main memory is the place where
programs and information are kept when the processor is effectively utilizing them. Main memory is
associated with the processor, so moving instructions and information into and out of the processor is
extremely fast. Main memory is also known as RAM (Random Access Memory). This memory is
volatile. RAM loses its data when a power interruption occurs.
Main Memory
In this approach, the operating system keeps track of the first and last location available for the
allocation of the user program
The operating system is loaded either at the bottom or at top
Interrupt vectors are often loaded in low memory therefore, it makes sense to load the operating
system in low memory
Sharing of data and code does not make much sense in a single process environment
The Operating system can be protected from user programs with the help of a fence register.
Advantages of Memory Management
It is a simple management approach
Disadvantages of Memory Management
It does not support multiprogramming
Memory is wasted
Multiprogramming with Fixed Partitions (Without Swapping)
A memory partition scheme with a fixed number of partitions was introduced to support
multiprogramming. this scheme is based on contiguous allocation
Each partition is a block of contiguous memory
Memory is partitioned into a fixed number of partitions.
Each partition is of fixed size
Example: As shown in fig. memory is partitioned into 5 regions the region is reserved for updating the
system the remaining four partitions are for the user program.
Fixed Size Partitioning
Operating System
p1
p2
p3
p4
Partition Table
Once partitions are defined operating system keeps track of the status of memory partitions it is done
through a data structure called a partition table.
Sample Partition Table
Starting Address of Partition Size of Partition Status
0k 200k allocated
Memory Allocation
To gain proper memory utilization, memory allocation must be allocated efficient manner. One of the
simplest methods for allocating memory is to divide memory into several fixed-sized partitions and
each partition contains exactly one process. Thus, the degree of multiprogramming is obtained by the
number of partitions.
Multiple partition allocation: In this method, a process is selected from the input queue and
loaded into the free partition. When the process terminates, the partition becomes available for
other processes.
Fixed partition allocation: In this method, the operating system maintains a table that indicates
which parts of memory are available and which are occupied by processes. Initially, all memory is
available for user processes and is considered one large block of available memory. This available
memory is known as a “Hole”. When the process arrives and needs memory, we search for a hole
that is large enough to store this process. If the requirement is fulfilled then we allocate memory
to process, otherwise keeping the rest available to satisfy future requests. While allocating a
memory sometimes dynamic storage allocation problems occur, which concerns how to satisfy a
request of size n from a list of free holes. There are some solutions to this problem:
First Fit
In the First Fit, the first available free hole fulfil the requirement of the process allocated.
First Fit
Here, in this diagram, a 40 KB memory block is the first available free hole that can store process A
(size of 25 KB), because the first two blocks did not have sufficient memory space.
Best Fit
In the Best Fit, allocate the smallest hole that is big enough to process requirements. For this, we search
the entire list, unless the list is ordered by size.
Best Fit
Here in this example, first, we traverse the complete list and find the last hole 25KB is the best suitable
hole for Process A(size 25KB). In this method, memory utilization is maximum as compared to other
memory allocation techniques.
Worst Fit
In the Worst Fit, allocate the largest available hole to process. This method produces the largest leftover
hole.
Worst Fit
Here in this example, Process A (Size 25 KB) is allocated to the largest available memory block which
is 60KB. Inefficient memory utilization is a major issue in the worst fit.
Fragmentation
Fragmentation is defined as when the process is loaded and removed after execution from memory, it
creates a small free hole. These holes can not be assigned to new processes because holes are not
combined or do not fulfill the memory requirement of the process. To achieve a degree of
multiprogramming, we must reduce the waste of memory or fragmentation problems. In the operating
systems two types of fragmentation:
1. Internal fragmentation: Internal fragmentation occurs when memory blocks are allocated to the
process more than their requested size. Due to this some unused space is left over and creating an
internal fragmentation problem.Example: Suppose there is a fixed partitioning used for memory
allocation and the different sizes of blocks 3MB, 6MB, and 7MB space in memory. Now a new
process p4 of size 2MB comes and demands a block of memory. It gets a memory block of 3MB
but 1MB block of memory is a waste, and it can not be allocated to other processes too. This is
called internal fragmentation.
2. External fragmentation: In External Fragmentation, we have a free memory block, but we can
not assign it to a process because blocks are not contiguous. Example: Suppose (consider the
above example) three processes p1, p2, and p3 come with sizes 2MB, 4MB, and 7MB
respectively. Now they get memory blocks of size 3MB, 6MB, and 7MB allocated respectively.
After allocating the process p1 process and the p2 process left 1MB and 2MB. Suppose a new
process p4 comes and demands a 3MB block of memory, which is available, but we can not
assign it because free memory space is not contiguous. This is called external fragmentation.
Both the first-fit and best-fit systems for memory allocation are affected by external fragmentation. To
overcome the external fragmentation problem Compaction is used. In the compaction technique, all free
memory space combines and makes one large block. So, this space can be used by other processes
effectively.
Another possible solution to the external fragmentation is to allow the logical address space of the
processes to be noncontiguous, thus permitting a process to be allocated physical memory wherever the
latter is available.
Paging
Paging is a memory management scheme that eliminates the need for a contiguous allocation of
physical memory. This scheme permits the physical address space of a process to be non-contiguous.
Logical Address or Virtual Address (represented in bits): An address generated by the CPU.
Logical Address Space or Virtual Address Space (represented in words or bytes): The set of
all logical addresses generated by a program.
Physical Address (represented in bits): An address actually available on a memory unit.
Physical Address Space (represented in words or bytes): The set of all physical addresses
corresponding to the logical addresses.
Example:
If Logical Address = 31 bits, then Logical Address Space = 231 words = 2 G words (1 G = 230)
If Logical Address Space = 128 M words = 27 * 220 words, then Logical Address = log2 227 =
27 bits
If Physical Address = 22 bits, then Physical Address Space = 222 words = 4 M words (1 M =
220)
If Physical Address Space = 16 M words = 24 * 220 words, then Physical Address = log2 224 =
24 bits
The mapping from virtual to physical address is done by the memory management unit (MMU) which
is a hardware device and this mapping is known as the paging technique.
The Physical Address Space is conceptually divided into several fixed-size blocks, called frames.
The Logical Address Space is also split into fixed-size blocks, called pages.
Page Size = Frame Size
Let us consider an example:
Physical Address = 12 bits, then Physical Address Space = 4 K words
Logical Address = 13 bits, then Logical Address Space = 8 K words
Page size = frame size = 1 K words (assumption)
Paging
Memory Management Techniques are basic techniques that are used in managing the memory in the
operating system. In this article, we will deal with the implementation of Continuous Memory
Management Techniques. Memory Management Techniques are classified broadly into two categories:
Contiguous
Non-contiguous
What is Contiguous Memory Management?
Contiguous memory allocation is a memory allocation strategy. As the name implies, we utilize this
technique to assign contiguous blocks of memory to each task. Thus, whenever a process asks to access
the main memory, we allocate a continuous segment from the empty region to the process based on its
size. In this technique, memory is allotted in a continuous way to the processes. Contiguous Memory
Management has two types:
Fixed(or Static) Partition
Internal Fragmentation is found in fixed partition scheme. To overcome the problem of internal
fragmentation, instead of fixed partition scheme, variable partition scheme is used.
Disadvantages Fix partition scheme
Maximum process size <= Maximum partition size.
The degree of multiprogramming is directly proportional to the number of partitions.
Internal fragmentation which is discussed above is present.
If a process of 19kb wants to allocate and we have free space which is not continuous we are not
able to allocate the space.
2. Variable Partition Scheme
In the variable partition scheme, initially memory will be single continuous free block. Whenever the
request by the process arrives, accordingly partition will be made in the memory. If the smaller
processes keep on coming then the larger partitions will be made into smaller partitions.
In variable partition schema initially, the memory will be full contiguous free block
Memory divided into partitions according to the process size where process size will vary.
One partition is allocated to each active partition.
External Fragmentation is found in variable partition scheme. To overcome the problem of external
fragmentation, compaction technique is used or non-contiguous memory management techniques are
used.
But, in what manner we divide a process to allocate them into main memory is very important to
understand. The process is divided after analysing the number of empty spaces and their size in the
main memory. Then only we do divide our process. It is a very time-consuming process. Their number
as well as their sizes changing every time due to execution of already present processes in main
memory.
In order to avoid this time-consuming process, we divide our process in secondary memory in
advance before reaching the main memory for its execution. Every process is divided into various
parts of equal size called Pages. We also divide our main memory into different parts of equal size
called Frames. It is important to understand that:
Although their numbers can be different. Below diagram will make you understand it in a better
way: consider empty main memory having a size of each frame is 2 KB, and two processes P1 and
P2 are 2 KB each.
Resolvent main memory, In main memory first page of P1 will store after that first page of
P2 will store then second page of P1 will store and at last second page of P2 will store. Hence
processes are stored in main memory non contiguous manner.
Concluding, we can say that Paging allows the memory address space of a process to be
non-contiguous. Paging is more flexible as only pages of a process are moved. It allows more
processes to reside in main memory than Contiguous memory allocation.
Advantages of Non-Contiguous Allocation
It reduces internal fragmentation since memory blocks can be allocated as needed, regardless of
their physical location.
It allows processes to be allocated memory in a more flexible and efficient manner since
the operating system can allocate memory to a process wherever free memory is available.
Disadvantages of Non-Contiguous Allocation
It can lead to external fragmentation, where the available memory is broken into small, non-
contiguous blocks, making it difficult to allocate large blocks of memory to a process.
Additionally, the use of pointers to link memory blocks can introduce additional overhead,
leading to slower memory allocation and deallocation times.
Conclusion
In conclusion, non-contiguous allocation is a useful memory allocation technique in situations where
processes do not require a contiguous block of memory. It is commonly used in operating systems,
such as Unix and Linux, where processes often require variable amounts of memory that are not
contiguous. It allows processes to use memory more efficiently by breaking them into smaller parts
that can be stored in different locations. This method helps reduce memory waste and makes better
use of available space. Although it adds some complexity in managing memory, it offers flexibility
and improved performance by minimizing fragmentation and optimizing memory usage.
Occupied space
Free space
Occupied space
Occupied space
Free space
After Compaction
After compaction, all the occupied space has been moved up and the free space at the bottom. This
makes the space contiguous and removes external fragmentation. Processes with large memory
requirements can be now loaded into the main memory.
Main Memory
Occupied space
Occupied space
Occupied space
Occupied space
Free space
Free space
INTRODUCTION:
Best-Fit Allocation is a memory allocation technique used in operating systems to allocate memory to
a process. In Best-Fit, the operating system searches through the list of free blocks of memory to find
the block that is closest in size to the memory request from the process. Once a suitable block is
found, the operating system splits the block into two parts: the portion that will be allocated to the
process, and the remaining free block.
Advantages of Best-Fit Allocation include improved memory utilization, as it allocates the smallest
block of memory that is sufficient to accommodate the memory request from the process.
Additionally, Best-Fit can also help to reduce memory fragmentation, as it tends to allocate smaller
blocks of memory that are less likely to become fragmented.
Disadvantages of Best-Fit Allocation include increased computational overhead, as the search for the
best-fit block of memory can be time-consuming and requires a more complex search algorithm.
Additionally, Best-Fit may also result in increased fragmentation, as it may leave smaller blocks of
memory scattered throughout the memory space.
Overall, Best-Fit Allocation is a widely used memory allocation technique in operating systems, but
its effectiveness may vary depending on the specifics of the system and the workload being executed.
For both fixed and dynamic memory allocation schemes, the operating system must keep list of each
memory location noting which are free and which are busy. Then as new jobs come into the system,
the free partitions must be allocated.
These partitions may be allocated by 4 ways:
As illustrated in above figure, the operating system first search throughout the memory and allocates
the job to the minimum possible memory partition, making the memory allocation efficient.
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.
Improved memory utilization
Reduced memory fragmentation
Minimizes external fragmentation
Best-fit allocation is a memory allocation algorithm used in operating systems to allocate memory to
processes. In this algorithm, the operating system searches for the smallest free block of memory that
is big enough to accommodate the process being allocated memory.
Here is a brief overview of the best-fit allocation algorithm:
1. The operating system maintains a list of all free memory blocks available in the system.
2. When a process requests memory, the operating system searches the list for the smallest free
block of memory that is large enough to accommodate the process.
3. If a suitable block is found, the process is allocated memory from that block.
4. If no suitable block is found, the operating system can either wait until a suitable block becomes
available or request additional memory from the system.
5. The best-fit allocation algorithm has the advantage of minimizing external fragmentation, as it
searches for the smallest free block of memory that can accommodate a process. However, it can
also lead to more internal fragmentation, as processes may not use the entire memory block
allocated to them.
Overall, the best-fit allocation algorithm can be an effective way to allocate memory in an operating
system, but it is important to balance the advantages and disadvantages of this approach with other
allocation algorithms such as first-fit, next-fit, and worst-fit.
Worst-Fit Allocation in Operating Systems
\For both fixed and dynamic memory allocation schemes, the operating system
must keep a list of each memory location noting which are free and which are
busy. Then as new jobs come into the system, the free partitions must be
allocated.
These partitions may be allocated in 4 ways:
1. First-Fit Memory Allocation
2. Best-Fit Memory Allocation
3. Worst-Fit Memory Allocation
4. Next-Fit Memory Allocation
These are Contiguous memory allocation techniques.
Worst-Fit Memory Allocation :
In this allocation technique, the process traverses the whole memory and
always search for the largest hole/partition, and then the process is placed in
that hole/partition. It is a slow process because it has to traverse the entire
memory to search the largest hole.
Worst-fit allocation tends to leave large blocks of memory unused, but it has
specific use cases in system design. For a deeper dive into memory
management and operating systems, the GATE CS Self-Paced Course covers
these topics comprehensively.
Here is an example to understand Worst Fit-Allocation –
RODUCTION:
ADVANTAGES OR DISADVANTAGES:
Fixed partitioning, also known as static partitioning, is one of the earliest memory
management techniques used in operating systems. In this method, the main
memory is divided into a fixed number of partitions at system startup, and each
partition is allocated to a process. These partitions remain unchanged throughout
system operation, ensuring a simple, predictable memory allocation process.
Despite its simplicity, fixed partitioning has several limitations, such as internal
fragmentation and inflexible handling of varying process sizes. This article delves
into the advantages, disadvantages, and applications of fixed partitioning in
modern operating systems.
Fixed Partitioning:
This is the oldest and simplest technique used to put more than one process in the
main memory. In this partitioning, the number of partitions (non-overlapping)
in RAM is fixed but the size of each partition may or may not be the same. As
it is a contiguous allocation, hence no spanning is allowed. Here partitions 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.
2. 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.
6.Analyze the function of the Page Table Base Register (PTBR) in the paging
mechanism of an operating system.
7.Summarize the function of demand paging in virtual memory.
8.Differentiate between internal and external fragmentation in terms of their
impact on memory usage.
9.Assess whether variable partition memory allocation is better than fixed partition
for certain applications.
10.Given free memory partitions of 150 K, 400 K, 250 K, 350 K, and 700 K (in
order), how would each of the First-fit, Best-fit, and Worst-fit algorithms place
processes of 175 K, 300 K, 125 K, and 500 K (in order)?
11,Describe the concept of Virtual Memory and outline the procedure for
translating virtual addresses into physical addresses, including a clear diagram.
16.Consider a main memory with five page frames and the following sequence of
page references: 3, 8, 2, 3, 9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3. which one of the following
is true with respect to page replacement policies First-In-First-out (FIFO) and
Least Recently Used (LRU)? Analyze number of page fault and give answer that
both incur the same number of page fault or different?
20.You have the following free memory partitions (in kilobytes): 80 K, 300 K, 150
K, 500 K, and 250 K. You need to allocate processes of sizes 90 K, 210 K, 120 K,
and 400 K.
Calculate how each of the following memory allocation techniques would place
the processes into the partitions:
First-fit
Best-fit
Worst-fit