Memory Management in OS
Memory Management in OS
Memory Management in OS
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.
It allows you to check how much memory needs to be allocated to processes that
decide which processor should get memory at what time.
Tracks whenever inventory gets freed or unallocated. According to it will update the
status.
It allocates the space to application routines.
It also make sure that these applications do not interfere with each other.
Helps protect different processes from each other
It places the programs in memory so that memory is utilized to its full extent.
Which data from the secondary memory should come to main memory and at which
location
CPU generates address for data at secondary memory but have access to main
memory thus operating system performs the address translation
.
Memory is central to the operation of a modern computer system. Memory consists of a
large array of bytes or words, each with its own address. The CPU fetches instructions
from memory according to the value of the program counter.
A typical instruction execution cycle first fetches an instruction from memory. The
instruction is then decoded and may cause operands to be fetched from memory. After the
instruction has been executed on the operands, results may be stored back in memory. The
memory unit sees only a stream of memory addresses.
Basic Hardware
Main memory and the registers built into the processor itself are the only storage that
the CPU can access directly.
Hence, any instructions in execution and any data being used by the instructions must
be in one of these direct access storage devices.
If the data are not in memory, they must be moved there before the CPU can operate
on them.
Registers that are built into CPU are generally accessible within one cycle of CPU
clock.
Main memory is accessed via a transaction on the memory bus.
Memory access may take many cycles of the CPU clock to complete, in which case
the processor needs to stall since it does not have data required to complete the
instruction that it is executing.
Protection of OS from access by user processes and protection of user processes from
one another has to be ensured by hardware.
This scheme prevents a user program from modifying the code or data structures of
either the OS or other users.
The base and limit registers can be loaded only by the OS which uses a special
privileged instruction.
Since privileged instructions can be executed only in kernel mode, and since only the
OS executes in kernel mode, only the OS can load the base and limit registers.
The OS executing in kernel mode is given unrestricted access to both OS and users
memory.
Address Binding
The binding of instructions and data to memory addresses can be done at:
Compile time – If you know at compile time, where the process will reside in
memory, then absolute code can be generated.
Load time – If it is not known at compile time where the process will reside in
memory, then the compiler must generate relocatable code. Final binding is delayed
until load time.
Execution time – If the process can be moved during its execution from one memory
segment to another, then binding must be delayed until run time
Physical address is the address loaded into the memory’s memory-address register.
The physical address cannot be directly accessed by the user, but the user can
calculate it through the logical address.
Set of all physical addresses is referred to as physical address space.
MMU does the virtual mapping from the virtual address to the physical address.
It is a hardware device located within the Central Processing Unit (CPU).
Logical address is generated by the CPU. Physical address is computed by the MMU.
It is also known as Virtual address, as it does not exist
physically in the memory. Physical address can be accessed physically.
The physical address can be accessed through the
Directly accessible to the user. logical address.
Linking
In programming we write code and that code is compiled into an intermediate code
which is machine readable (known as object file)
But there are certain codes which we re-use almost every time that are for example
the libraries (header files in C)(known as function library)
Linking is a process of combining object files and function libraries into an
executable file
Types of linking
Static linking – system libraries and program code combined by the loader into the
binary program image
Dynamic linking –linking postponed until execution time
Small piece of code, stub, used to locate the appropriate memory-resident library
routine
Stub replaces itself with the address of the routine, and executes the routine
Operating system checks if routine is in processes’ memory address
If not in address space, add to address space
Dynamic linking is particularly useful for libraries
System also known as shared libraries
Consider applicability to patching system libraries
Versioning may be needed
Loading
Swapping
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.
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 rolls 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
Disadvantages of Swapping
1. The swapping algorithm must be perfect; otherwise, the number of Page Faults will
increase, and performance will decrease.
2. Inefficiency will occur when there are common/shared resources between many
processes.
3. The user will lose information if there is heavy swapping and the computer loses its
power.
Fixed-size Partition
Advantages
1. Because all of the blocks are the same size, this scheme is simple to implement. All
we have to do now is divide the memory into fixed blocks and assign processes to
them.
2. It is easy to keep track of how many blocks of memory are left, which in turn decides
how many more processes can be given space in the memory.
3. As at a time multiple processes can be kept in the memory, this scheme can be
implemented in a system that needs multiprogramming.
Disdvantages
Though the fixed-size partition scheme has many advantages, it also has some disadvantages:
1. As the size of the blocks is fixed, we will not be able to allot space to a process that
has a greater size than the block.
2. The size of the blocks decides the degree of multiprogramming, and only that many
processes can remain in the memory at once as the number of blocks.
3. If the size of the block is greater than the size of the process, we have no other choice
but to assign the process to this block, but this will lead to much empty space left
behind in the block. This empty space could've been used to accommodate a different
process. This is called internal fragmentation. Hence, this technique may lead to space
wastage.
4. External fragmentation: Due to the limitations of spanning, the total unused space
on different partitions cannot be used to load non-differentiated processes, even if the
space is available.
Consider the example of three processes, process 1, process 2, and process 3, all of
size 3 MB(same as above example) The processes will be assigned to a memory of
15MB divided into blocks of 5MB as shown below:
Now consider that process 1 and process 3 have freed the memory blocks because
they have completed their execution. After removing process 1 and process 3, the
main memory explained above will look like this:
OS
free 5 MB
P2 5 MB
free 5 MB
Let’s suppose we try to load another process, process 4, of size 7MB in the memory.
Although we have 10 MB of free memory space, the process cannot be loaded into the
memory. This is because we need a contiguous block of 7 MB to load process 4.
Variable-size Partition
It is a part of the 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 the
process’s need instead of partitioning during system configure.
2. The size of the partition will be equal to the 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. The number of partitions in RAM is not fixed and depends on the number of
incoming processes and Main Memory’s size.
There are some advantages and disadvantages of variable partitioning over fixed partitioning
as given
Advantages
Disadvantages
First Fit
In the first fit approach is to allocate the first free partition or hole large enough which
can accommodate the process. It finishes after finding the first suitable free partition.
This method works as for any process Pn, the OS searches from starting block again
and again and allocates a block to process Pn such that
Block is available
Can fit the process
In simple words First Fit algorithm finds, the first block to fix the process.
In the given example, let us assume the jobs and the memory requirements as the
following:
Best Fit
The best fit deals with allocating the smallest free partition which meets the
requirement of the requesting process.
This algorithm first searches the entire list of free partitions and considers the smallest
hole that is adequate. It then tries to find a hole which is close to actual process size
needed.
Example
If you see the image below you will see that the process size is 40.
While blocks 1, 2 and 4 can accommodate the process. Block 2 is chosen as it leaves
the lowest memory wastage
Advantage
It is allocated to the process. This scheme is considered the best approach as it results
in the most optimized memory allocation.
Also reduces internal fragmentation.
Disadvantage
Worst fit
In worst fit approach is to locate largest available free portion so that the portion left
will be big enough to be useful. It is the reverse of best fit.
This method works as for any process Pn, the OS searches from starting block again
and again and allocates a block to process Pn such that
Paging
Paging is a memory management scheme by which a computer stores and retrieves
data from secondary storage for use in main memory.
Paging is a process of reading data from, and writing data to the secondary storage.
Partition memory into small equal fixed-size chunks and divide each process into the
same size chunks.
The chunks of a process are called pages.
The chunks of memory are called frames.
The basic idea behind paging is to only keep those part of the processes in memory
that are actually being used
Paging is a non-contiguous memory allocation technique.
The basic idea behind paging is to only keep those part of the processes in memory
that are actually being used
Paging:
Divides processes into a fixed-sized pages
Then selectively allocates pages to frames in memory, and
manages (moves, removes, reallocates) pages in memory
Page number p is an index into a page table that contains the base address of each
page in physical memory.
Page offset d is a displacement, combined with the base address to define the physical
memory address that is sent to the memory unit.
Page Fault
When the page (data) requested by a program is not available in the memory, it is
called a page fault. This usually results in the application being shut down.
A page is a fixed-length memory block used as a transferring unit between physical
memory and external storage. A page fault occurs when a program accesses a page
that has been mapped in address space but has not been loaded in the physical
memory.
Page hit
Page table
A page table is the data structure used by a virtual memory system in a computer
operating system to store the mapping between virtual addresses and physical
addresses
Frame
Frame is a fixed sized block in physical memory space.
The CPU generates the logical address which contains the page number and the page
offset. The PTBR register contains the address of the page table. Now, the page table helps
in determining the frame number corresponding to the page number. Now, with the help
of frame number and the page offset the physical address is determined and the page is
accessed in the main memory.
Advantages
Disadvantages
Page table has page table entries where each page table entry stores a frame number and
optional status (like protection) bits. Many of status bits used in the virtual memory system.
The most important thing in PTE is frame Number.
1. Frame Number-
Frame number specifies the frame where the page is stored in the main memory.
The number of bits in frame number depends on the number of frames in the main
memory.
NOTE
If the required page is not present in the main memory, then it is called as Page Fault.
A page fault requires page initialization.
The required page has to be initialized (fetched) from the secondary memory and brought into the
main memory.
3. Protection Bit-
4. Reference Bit-
Reference bit specifies whether that page has been referenced in the last clock cycle or not.
If the page has been referenced recently, then this bit is set to 1 otherwise set to 0.
NOTE
Reference bit is useful for page replacement policy.
A page that has not been referenced recently is considered a good candidate for page replacement in
LRU page replacement policy.
6. Dirty Bit-
NOTE
In case the page is modified,
Before replacing the modified page with some other page, it has to be written back in the secondary
memory to avoid losing the data.
Dirty bit helps to avoid unnecessary writes.
This is because if the page is not modified, then it can be directly replaced by another page without
any need of writing it back to the disk.
Segmentation
This concept is the same as the paging concept. But unlike paging in Segmentation, the
process is not divided into pages but into large chunks called segments or modules. The CPU
generates this logical address. This logical address is to access secondary memory. But CPU
has to access the main memory. So what to do now? In this case, address translation is
required to convert a logical address into a physical one.
A physical address is an address for finding data in the main memory. So now the CPU will
take the help of the segment table. A segment table is a data structure for storing the
information of all process segments.CPU use a segment table to map the logical address to a
physical address. In the segment table, there are two types of information
Then if the value of offset(d)<=Limit. Then only the CPU can read that segment; else, the
error will be there.Offset(d) depicts the size of that segment CPU wants to read.
Segmentation Example
Suppose the CPU wants to access segment 3. It will generate a logical address(as shown
below) specifying the segment number and size of the segment it wants to read. The segment
size is supposed 300. This means the CPU wants to read 300 instructions from segment 3. So
after consulting the segment table, the CPU comes to know that the base address is 2500. But
the size of the segment is 200 instructions. So here offset(d)>Limit. So this will generate an
error.
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compared to the page table in paging.
Disadvantages
Virtual Memory is a way of using the secondary memory in such a way that it
feels like we are using the main memory.
So, the benefit of using the Virtual Memory is that if we are having some program
that is larger than the size of the main memory then instead of loading all the pages
we load some important pages.
In general, when we execute a program, then the entire program is not required to be
loaded fully in the main memory.
This is because only a few portions of the program are being executed at a time.
For example, the error handling part of any program is called only when there is
some error and we know that the error happens rarely.
So, why to load this part of the program in the main memory and fill the memory
space? Another example can be a large-sized array.
Generally, we have over-sized arrays because we reserve the space for worst-case
scenarios.
But in reality, only a small part of the array is used regularly. So, why to put the
whole array in the main memory?
So, what we do is we put the frequently used pages of the program in the main
memory and this will result in fast execution of the program because whenever
those pages will be needed then it will be served from the main memory only.
Other pages are still in the secondary memory.
Now, if some request to the page that is not in the main memory comes, then this
situation is called a Page Miss or Page Fault.
In this situation, we remove one page from the main memory and load the desired
page from the secondary memory to the main memory at the run time i.e swapping
of pages will be performed here.
By doing so, the user feels like he/she is having a lot of memory in its system but in
reality, we are just putting that part of the process in the memory that is frequently
used. The following figure shows the working in brief:
Demand Paging
Demand paging is a technique used in virtual memory systems where the pages are brought
in the main memory only when required or demanded by the CPU. Hence, it is also named
as lazy swapper because the swapping of pages is done only when required by the CPU.
How does demand paging work?
Lets us understand this with the help of an example. Suppose we have to execute a process
P having four pages as P0, P1, P2, and P3. Currently, in the page table, we have page P1
and P3.
1. Now, if the CPU wants to access page P2 of a process P, first it will search the page
in the page table.
2. As the page table does not contain this page so it will be a trap or page fault. As
soon as the trap is generated and context switching happens and the control goes to
the operating system.
3. The OS system will put the process in a waiting/ blocked state. The OS system will
now search that page in the backing store or secondary memory.
4. The OS will then read the page from the backing store and load it to the main
memory.
5. Next, the OS system will update the page table entry accordingly.
6. Finally, the control is taken back from the OS and the execution of the process is
resumed.
Hence whenever a page fault occurs these steps are followed by the operating system and
the required page is brought into memory.
So whenever a page fault occurs all the above steps(2–6) are performed. This time taken to
service the page fault is called the Page fault service time.
Effective Memory Access time
When the page fault rate is ‘p’ while executing any process then the effective memory
access time is calculated as follows:
Advantages
Disadvantages
The amount of processor overhead and the number of tables used for handling the
page faults is greater than in simple page management techniques.
Page replacement is needed in the operating systems that use virtual memory using
Demand Paging. As we know that in Demand paging, only a set of pages of a process
is loaded into the memory. This is done so that we can have more processes in the
memory at the same time.
When a page that is residing in virtual memory is requested by a process for its
execution, the Operating System needs to decide which page will be replaced by this
requested page. This process is known as page replacement and is a vital component
in virtual memory management.
Page Fault: A Page Fault occurs when a program running in CPU tries to access a
page that is in the address space of that program, but the requested page is currently
not loaded into the main physical memory, the RAM of the system.
Since the actual RAM is much less than the virtual memory the page faults occur. So
whenever a page fault occurs, the Operating system has to replace an existing page
in RAM with the newly requested page. In this scenario, page replacement algorithms
help the Operating System in deciding which page to replace. The primary objective
of all the page replacement algorithms is to minimize the number of page faults.
For Example:
Consider the page reference string of size 12: 1, 2, 3, 4, 5, 1, 3, 1, 6, 3, 2, 3 with frame size
4(i.e. maximum 4 pages in a frame).
Initially, all 4 slots are empty, so when 1, 2, 3, 4 came they are allocated to the
empty slots in order of their arrival. This is page fault as 1, 2, 3, 4 are not available
in memory.
When 5 comes, it is not available in memory so page fault occurs and it replaces the
oldest page in memory, i.e., 1.
When 1 comes, it is not available in memory so page fault occurs and it replaces the
oldest page in memory, i.e., 2.
When 3,1 comes, it is available in the memory, i.e., Page Hit, so no replacement
occurs.
When 6 comes, it is not available in memory so page fault occurs and it replaces the
oldest page in memory, i.e., 3.
When 3 comes, it is not available in memory so page fault occurs and it replaces the
oldest page in memory, i.e., 4.
When 2 comes, it is not available in memory so page fault occurs and it replaces the
oldest page in memory, i.e., 5.
When 3 comes, it is available in the memory, i.e., Page Hit, so no replacement
occurs.
Advantages
Poor performance.
Doesn’t consider the frequency of use or last used time, simply replaces the oldest
page.
Suffers from Belady’s Anomaly(i.e. more page faults when we increase the number
of page frames).
Least Recently Used page replacement algorithm keeps track of page usage over a
short period of time. It works on the idea that the pages that have been most heavily
used in the past are most likely to be used heavily in the future too.
In LRU, whenever page replacement happens, the page which has not been used for
the longest amount of time is replaced.
For Example
Advantages
Efficient.
Doesn't suffer from Belady’s Anomaly.
Disadvantages
Complex Implementation.
Expensive.
Requires hardware support.
Initially, all 4 slots are empty, so when 1, 2, 3, 4 came they are allocated to the empty
slots in order of their arrival. This is page fault as 1, 2, 3, 4 are not available in
memory.
When 5 comes, it is not available in memory so page fault occurs and it replaces 4
which is going to be used farthest in the future among 1, 2, 3, 4.
When 1,3,1 comes, they are available in the memory, i.e., Page Hit, so no replacement
occurs.
When 6 comes, it is not available in memory so page fault occurs and it replaces 1.
When 3, 2, 3 comes, it is available in the memory, i.e., Page Hit, so no replacement
occurs.
Page Fault ratio = 6/12
Advantages
Easy to Implement.
Simple data structures are used.
Highly efficient.
Disadvantages
NOTE: Belady’s anomaly is the phenomenon in which increasing the number of page
frames results in an increase in the number of page faults for certain memory access patterns.
This phenomenon is commonly experienced when using the first-in first-out (FIFO) page
replacement algorithm.
Thrashing
Thrashing is when the page fault and swapping happens very frequently at a higher rate, and
then the operating system has to spend more time swapping these pages. This state in the
operating system is known as thrashing. Because of thrashing, the CPU utilization is going to
be reduced or negligible.
The basic concept involved is that if a process is allocated too few frames, then there will be
too many and too frequent page faults. As a result, no valuable work would be done by the
CPU, and the CPU utilization would fall drastically.
The long-term scheduler would then try to improve the CPU utilization by loading some
more processes into the memory, thereby increasing the degree of multiprogramming.
Unfortunately, this would result in a further decrease in the CPU utilization, triggering a
chained reaction of higher page faults followed by an increase in the degree of
multiprogramming, called thrashing.
Thrashing has some negative impacts on hard drive health and system performance.
Therefore, it is necessary to take some actions to avoid it. To resolve the problem of
thrashing, here are the following methods, such as:
Adjust the swap file size:If the system swap file is not configured correctly, disk
thrashing can also happen to you.
Increase the amount of RAM: As insufficient memory can cause disk thrashing, one
solution is to add more RAM to the laptop. With more memory, your computer can
handle tasks easily and don't have to work excessively. Generally, it is the best long-
term solution.
Decrease the number of applications running on the computer: If there are too
many applications running in the background, your system resource will consume a
lot. And the remaining system resource is slow that can result in thrashing. So while
closing, some applications will release some resources so that you can avoid thrashing
to some extent.
Replace programs: Replace those programs that are heavy memory occupied with
equivalents that use less memory.