0% found this document useful (0 votes)
2 views50 pages

5.1 Memory Management

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 50

• Memory Management

• In computer system, to improve utilization of CPU


and speed of computers response to its users, there
may be multiple processes executing at the same
time and every process that needs to execute
requires certain amount of primary memory.
• Thus multiprogramming creates need of Memory
management.
• Memory management is one of the important
function of operating system which helps in allocating
the main memory space to the processes and their
data at the time of their execution.
• The main task of Memory management is efficient
memory utilization.
1. Memory management manages primary memory
and moves processes back and forth between main
memory and disk during execution.
2. Memory management keeps track of each and
every memory location, regardless of either it is
allocated to some process or it is free.
3. It checks how much memory is to be allocated to
processes.
4. It decides which process will get memory at what
time.
5. It tracks whenever some memory gets freed or
unallocated and correspondingly it updates the
status.
• Memory partitioning
• It is the system by which the memory of a computer
system is divided into sections for use by the running
programs.
• These memory divisions are known as partitions.
• There are different ways in which memory can be
partitioned: fixed and variable partitioning.
1. Fixed partitioning
• It is defined as the system of dividing memory into
non-overlapping sizes that are fixed, unmovable,
static.
• Main memory is divided into a no. of static partitions
at system generation time.
• A process may be loaded into a partition of equal or
greater size and is remains to its allocated partition.
• If we have comparatively small processes with
respect to the fixed partition sizes, this creates a
big problem.
• This results in occupying all partitions with lots of
unoccupied space left.
• This unoccupied space is known as fragmentation.
• Within the fixed partition context, this is known
as internal fragmentation (IF). This is because of
unused space created by a process within its
allocated partition (internal).
• Two possibilities:
• a). Equal size partitioning
• b). Unequal size Partition
• Advantages of Fixed Partitioning –
• Easy to implement:
Algorithms needed to implement Fixed
Partitioning are easy to implement. It simply
requires putting a process into certain partition
without focusing on the emergence of Internal
and External Fragmentation.
• Little OS overhead:
Processing of Fixed Partitioning require lesser and
indirect computational power.
• Disadvantages of Fixed Partitioning –
• Internal Fragmentation:
Main memory use is inefficient. Any program, no matter
how small, occupies an entire partition. This can cause internal
fragmentation.
• 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 .
• Limit process size:
Process of size greater than size of partition in Main
Memory cannot be accommodated. Partition size cannot be
varied according to the size of incoming process’s size.
• Limitation on Degree of Multiprogramming:
Partition in Main Memory are made before execution or
during system configure. Main Memory is divided into fixed
number of partition. Number of processes greater than number
of partitions in RAM is invalid in Fixed Partitioning.

2. Variable partitioning
• It is the system of dividing memory into non-
overlapping but variable sizes.
• This system of partitioning is more flexible than
the fixed partitioning configuration, but it's still
not the most ideal solution.
• Small processes are fit into small partitions and
large processes fit into larger partitions.
• These processes do not necessarily fit exactly,
even though there are other unoccupied
partitions.
• The flexibility offered in variable partitioning still
does not completely solve our problems.
• With dynamic partitioning, processes are
allocated the exact amount of memory space they
need.
• This completely eliminates internal
fragmentation, and processes occupy the total
space allocated to them.
• Since processes do not stay permanently in
memory, once they are finished, memory is
released from those processes and allocated to
other waiting processes.
• In reallocating these spaces, remember that
dynamic partitioning process makes available the
exact size of memory requested. This means, for
example, if a process occupying the 12Mb space is
complete and there is a 9Mb process waiting, the
12Mb partition is split making available exactly
9Mb for the awaiting process to load and a 3Mb
empty space.
• This empty space created is known as external
fragmentation. This is because the fragment
(unoccupied free space) is created externally to
the process that's being loaded.
• Over time, this causes the memory to be become
fragmented in multiple places.
• Compaction
• It's true that dynamic partitioning causes external
fragmentation, which is still an issue. To correct
this, the processor carries out a process known as
compaction.
• Compaction is defined as the process of moving
allocated partitions together to isolate all
free/unoccupied space into a contiguous free
space. Notice that the allocated partitions are
moved upwards to isolate the eternally
fragmented free spaces to the bottom.


• 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 no
internal fragmentation.
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 can not 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.

• First, best & worst fit
• Free Space Management Techniques
• Files are created and deleted frequently during
the operation of a computer system.
• Since there is only limited amount of memory
space, it is necessary to reuse the space from
deleted files for new files.
• To keep track for new space , the file system
maintains a free space list. The free space list
records all dis blocks, which are free.
• The system maintains a free space list which
keeps track of the disk blocks that are not
allocated to some file or directory.
• The free space list can be implemented using:
1. Bitmap or Bit vector –
A Bitmap or Bit Vector is series or collection of bits where each bit
corresponds to a disk block. The bit can take two values: 0 and 1:
– 0 indicates that the block is allocated and
– 1 indicates a free block.
The given instance of disk blocks
on the disk in Figure 1
(where green blocks are allocated)
can be represented by a bitmap
of 16 bits as:
0000111000000110.
• Advantages –
1. Simple to understand.
2. Finding the first free block is efficient.
3. Finding the n consecutive free block is also efficient.
4. A bit map requires lesser space as it uses 1 bit per block.
• Disadvantages:
1. Extra space required to store bit map.
2. It may require a special hardware support to do
bit operations i.e finding out which bit is 1 and
which bit is 0.
3. Bit map are inefficient unless they are kept in
main memory and are written to disk
occasionally for recovery needs.
Finding the first free block is efficient.
• It requires scanning the words (a group of 8 bits) in a bitmap for a
non-zero word.
• The first free block is then found by scanning for the first 1 bit in
the non-zero word.
• The block number can be calculated as:

• (number of bits per word) *(number of 0-values words) + offset of


bit first bit 1 in the non-zero word .

• For the Figure-1, we scan the bitmap sequentially for the first non-
zero word.
The first group of 8 bits (00001110) constitute a non-zero
word since all bits are not 0. After the non-0 word is found, we look
for the first 1 bit. This is the 5th bit of the non-zero word. So, offset
= 5.
Therefore, the first free block number = 8*0+5 = 5.
• Linked List –
In this approach, the free disk blocks are linked together i.e. a
free block contains a pointer to the next free block.
The block number of the very first disk block is stored at a
separate location on disk and is also cached in memory.
In Figure-2, the free space list head points to Block 5 which
points to Block 6, the next free block and so on.
The last free block would
contain a null pointer indicating
the end of free list.
• Advantages
1. If a file is to be allocated a free block, the operating
system can simply allocate the first block in the free space
list and move the head pointer to the next free block in the
list.
2. No disk fragmentation as every block is utilized.

• Disadvantages
1. It is not very efficient method as to traverse the
list, each block must be read, which requires substantial
I/O time.
2. Pointers used here will also consume disk space.
Additional memory is therefore required.
• Virtual Memory
• A computer can address more memory than the
amount physically installed on the system.
• This extra memory is actually called virtual
memory and it is a section of a hard disk that's set up
to emulate(compete) the computer's RAM.
• The main visible advantage of this scheme is that
programs can be larger than physical memory.
• Virtual memory serves two purposes.
1. First, it allows us to extend the use of physical
memory by using disk.
2. Second, it allows us to have memory protection,
because each virtual address is translated to a
physical address.

Following are the situations, when entire program is not
required to be loaded fully in main memory.

1. User written error handling routines are used only when


an error occurred in the data or computation.
2. Certain options and features of a program may be used
rarely.
3. Many tables are assigned a fixed amount of address space
even though only a small amount of the table is actually used.
4. The ability to execute a program that is only partially in
memory would counter many benefits.
5. Less number of I/O would be needed to load or swap each
user program into memory.
6. A program would no longer be constrained by the amount
of physical memory that is available.
7. Each user program could take less physical memory, more
programs could be run the same time, with a corresponding
increase in CPU utilization and throughput.
• Modern microprocessors intended for general-purpose use, a
memory management unit, or MMU, is built into the hardware.
The MMU's job is to translate virtual addresses into physical
addresses. A basic example is given below −
• Virtual memory is commonly implemented by demand paging. It can also be
implemented in a segmentation system. Demand segmentation can also be used
to provide virtual memory.
• Demand Paging
• A demand paging system is quite similar to a paging
system with swapping where processes reside in
secondary memory and pages are loaded only on
demand, not in advance.
• When a context switch occurs, the operating system does
not copy any of the old program’s pages out to the disk
or any of the new program’s pages into the main memory
Instead, it just begins executing the new program after
loading the first page and fetches that program’s pages as
they are referenced.

• While executing a program, if the program references
a page which is not available in the main memory
because it was swapped out a little ago, the
processor treats this invalid memory reference as
a page fault and transfers control from the program
to the operating system to demand the page back
into the memory.
• Advantages of Demand Paging
1. Large virtual memory.
2. More efficient use of memory.
3. There is no limit on degree of multiprogramming.
• Disadvantages of Demand Paging
1. Number of tables and the amount of processor
overhead for handling page interrupts are greater than
in the case of the simple paged management
techniques.
Paging
• Paging is a storage mechanism that allows OS
to retrieve processes from the secondary
storage into the main memory in the form of
pages.
• In the Paging method, the main memory is
divided into small fixed-size blocks of physical
memory, which is called frames.
• The size of a frame should be kept the same as
that of a page to have maximum utilization of
the main memory and to avoid external
fragmentation.
• Paging is used for faster access to data, and it
is a logical concept.


• Advantages of Paging
• Easy to use memory management algorithm
• No need for external Fragmentation
• Swapping is easy between equal-sized pages
and page frames.
• Disadvantages of Paging
• May cause Internal fragmentation
• Complex memory management algorithm
• Page tables consume additional memory.
• Multi-level paging may lead to memory
reference overhead.
• Segmentation
• Segmentation method works almost similarly to
paging, only difference between the two is that
segments are of variable-length whereas, in the
paging method, pages are always of fixed size.
• A program segment includes the program's main
function, data structures, utility functions, etc.
• The OS maintains a segment map table for all the
processes.
• It also includes a list of free memory blocks along
with its size, segment numbers, and it's memory
locations in the main memory or virtual memory.
• Advantages of a Segmentation method
• Offer protection within the segments
• You can achieve sharing by segments referencing
multiple processes.
• No internal fragmentation.
• Segment tables use lesser memory than paging
• Disadvantages of Segmentation
• In segmentation method, processes are loaded/
removed from the main memory. Therefore, the
free memory space is separated into small pieces
which may create a problem of external
fragmentation
• Costly memory management algorithm
• Page Replacement Algorithm
• When the processor needs to execute a page and if
that page is not available in main memory then this
situation is called page fault.
• When a page fault occurs, the operating system has to
choose a page to remove from memory to make room
for the page that has to be bought in. This is known as
page replacement.
• Page replacement algorithms are the techniques using
which an Operating System decides which memory
pages to swap out,
• The approach of page replacement is , if no frame is
free, we have find one that is not currently being used
and free it.
• This process determines the quality of the page
replacement algorithm: the lesser the time
waiting for page-ins, the better is the algorithm.
• A page replacement algorithm tries to select
which pages should be replaced to minimize the
total number of page misses.
• There are many different page replacement
algorithms.
1. FIFO(First In First Out) Page replacement Algorithm.
2. Optimal Page Replacement Algorithm.
3. LRU (Least Recently Used) Page Replacement Algorithm.
• Steps in page replacement are listed below:
1. Find the location of the desired page on the disk.
2. Find a free frame:
a. If there is no free frame,
b. use a page-replacement algorithm to
select a victim frame.
c. Write the victim frame to the disk; change the
page and frame tables accordingly.
3. Read the desired page into the newly freed frame;
change the page and frame tables.
4. Restart the user process.
• Reference String
• The string of memory references is called reference
string.
• First In First Out (FIFO) Algorithm
The simplest page replacement algorithm is
a FIFO.
A FIFO replacement algorithm associates
with each page the time when that page was
brought into memory.
When a page must be replaced, the oldest
page is chosen.
FIFO queue is created to hold all pages in memory.
We replace the page at the head of the queue.
When a page is brought into memory, we
insert it at the tail of the Queue.
• Consider the example:
Consider the Reference String and calculate page fault for three frames.
Referene String: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

Solution:-

Reference String:-
Frames 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
1 7 7 7 2 2 2 2 4 4 4 0 0 0 0 0 0 0 7 7 7
2 0 0 0 0 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0
3 1 1 1 1 0 0 0 3 3 3 3 3 2 2 2 2 2 1
Page Fault √ √ √ √ √ √ √ √ √ √ √ √ √ √ √

• There are total 15 faults altogether.


• Fault rate = 15/20
=0.75

• Optimal Page algorithm
• An optimal page-replacement algorithm has the lowest page-fault
rate of all algorithms. An optimal page-replacement algorithm
exists, and has been called OPT or MIN.
• Replace the page that will not be used for the longest period of
time. Use the time when a page is to be used.

• Least Recently Used (LRU) algorithm
• Page which has not been used for the longest
time in main memory is the one which will be
selected for replacement.
• Easy to implement, keep a list, replace pages by
looking back into time.

• Advantages of FIFO Page Replacement Algorithm
1. It is simple to implement
2. It is easiest algorithm
3. Easy to understand and execute.
• Disadvantages of FIFO Page Replacement
Algorithm
1. It is not very effective.
2. System needs to keep track of each frame
3. Its performance is not always good.
4. Bad replacement choice increases the page fault
rate and slow process execution.
• Optimal Page Replacement Algorithm
Optimal Page Replacement Algorithm has the
lowest page fault rate of all algorithm.
Optimal Page Replacement Algorithm replace
that page which will not be used for longest period
of time.
• Advantages of Optimal Page Replacement
Algorithm
1. It is best possible optimal algorithm.
2. It gives smallest number of page faults.
3. Twice as good as FIFO Page Replacement
Algorithm
• Disadvantages of Optimal Page Replacement
Algorithm
1. This algorithm is difficult to implement.
2. It requires future knowledge of reference string.
LRU(Least Recently Used) Page Replacement
Algorithm
If we use the recent past as an approximation of the
near future, then we would replace that page which has not
been used for the longest period of time. This is least
recently used algorithm.
LRU replacement associates with each page the time
of its last use.
When the page is to be replaced, LRU chooses that
page which has been used for the longest period of time.
we can think this concept as looking backward in
time rather than forward as per optimal Page Replacement
Algorithm.
• Advantages of LRU Page Replacement Algorithm
1. LRU is actually quite a good algorithm.
2. It is very flexible to implement.

Disadvantages of LRU Page Replacement Algorithm

1. Its implementation is not very easy.


2. It requires additional data structure and
hardware support.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy