0% found this document useful (0 votes)
34 views

Memory Management@Unit 3

Uploaded by

MANJU Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Memory Management@Unit 3

Uploaded by

MANJU Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Memory Management

Memory Allocation Techniques


In operating systems, Memory Management is the function responsible for allocating and managing
computer’s main memory. Memory Management function keeps track of the status of each memory
location, either allocated or free to ensure effective and efficient use of Primary Memory.
There are two Memory Management Techniques: Contiguous, and Non-Contiguous. In 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
Fixed Partitioning:
This is the oldest and simplest technique used to put more than one processes in the main memory.
In this partitioning, number of partitions (non-overlapping) in RAM are fixed but size of each
partition may or may not be same. As it is contiguous allocation, hence no spanning is allowed. Here
partition 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.
Suppose process P5 of size 7MB comes. But this process cannot be accommodated inspite of
available free space because of contiguous allocation (as spanning is not allowed). Hence, scattered
unused 7MB becomes part of External Fragmentation.
There are some advantages and disadvantages of fixed partitioning.
Advantages of Fixed Partitioning –
1. Easy to implement:
Algorithms needed to implement Fixed Partitioning are easy to implement. It simply requires putting
a process into certain partition without focussing on the emergence of Internal and External
Fragmentation.
2. Little OS overhead:
Processing of Fixed Partitioning require lesser excess and indirect computational power.
Disadvantages of Fixed Partitioning –
1. Internal Fragmentation:
Main memory use is inefficient. Any program, no matter how small, occupies an entire partition. This
can cause internal fragmentation.
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).
3. 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. Hence, process size of 32MB in
above stated example is invalid.
4. 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. Suppose if there are partitions in RAM and are the
number of processes, then condition must be fulfilled. Number of processes
greater than number of partitions in RAM is invalid in Fixed Partitioning.

Variable Partitioning –
It is a part of 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 process’s need
instead of partitioning during system configure.
2. The size of partition will be equal to 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 utilisation of RAM.
4. Number of partitions in RAM is not fixed and depends on the number of incoming process and Main
Memory’s size.

There are some advantages and disadvantages of variable partitioning over fixed partitioning as
given below.
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 case of internal fragmentation. There will be no unused space left in the partition.
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.
For example, suppose in above example- process P1(2MB) and process P3(1MB) completed their
execution. Hence two spaces are left i.e. 2MB and 1MB. Let’s suppose process P5 of size 3MB comes.
The empty space in memory cannot be allocated as no spanning is allowed in contiguous allocation.
The rule says that process must be contiguously present in main memory to get executed. Hence it
results in External Fragmentation.

Now P5 of size 3 MB cannot be accommodated in spite of required available space because in


contiguous no spanning is allowed.

Fragmentation:
There are two types of fragmentation in OS which are given as: Internal fragmentation, and
External fragmentation.
Internal Fragmentation:
Internal fragmentation happens when the memory is split into mounted sized blocks. Whenever a
method request for the memory, the mounted sized block is allotted to the method. just in case
the memory allotted to the method is somewhat larger than the memory requested, then the
distinction between allotted and requested memory is that the Internal fragmentation.

The above diagram clearly shows the internal fragmentation because the difference between
memory allocated and required space or memory is called Internal fragmentation.
External Fragmentation:
External fragmentation happens when there’s a sufficient quantity of area within the memory to
satisfy the memory request of a method. however the process’s memory request cannot be
fulfilled because the memory offered is during a non-contiguous manner. Either you apply first-fit
or best-fit memory allocation strategy it’ll cause external fragmentation.

In above diagram, there is enough space (55 KB) to run a process-07 (required 50 KB) but the
memory (fragment) is not contiguous. Here, we use compaction, paging or segmentation to use the
free space to run a process.
Difference between Internal fragmentation and External fragmentation:-
S.NO Internal fragmentation External fragmentation

In internal fragmentation fixed-sized In external fragmentation, variable-sized


memory, blocks square measure memory blocks square measure appointed to
1. appointed to process. method.

Internal fragmentation happens when


the method or process is larger than External fragmentation happens when the
2. the memory. method or process is removed.

The solution of internal fragmentation Solution of external fragmentation is


3. is best-fit block. compaction, paging and segmentation.

Internal fragmentation occurs when External fragmentation occurs when memory is


memory is divided into fixed sized divided into variable size partitions based on the
4. partitions. size of processes.

The difference between memory The unused spaces formed between non-
allocated and required space or contiguous memory fragments are too small to
memory is called Internal serve a new process, is called External
5. fragmentation. fragmentation .

Placement Algorithms
Paging (Non Contiguous Memory Allocation):
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.
For example, if the main memory size is 16 KB and Frame size is 1 KB. Here, the main memory will
be divided into the collection of 16 frames of 1 KB each.

There are 4 separate processes in the system that is A1, A2, A3, and A4 of 4 KB each. Here, all the
processes are divided into pages of 1 KB each so that operating system can store one page in one
frame. At the beginning of the process, all the frames remain empty so that all the pages of the
processes will get stored in a contiguous way.
In this example you can see that A2 and A4 are moved to the waiting state after some time.
Therefore, eight frames become empty, and so other pages can be loaded in that empty blocks. The
process A5 of size 8 pages (8 KB) are waiting in the ready queue.

• Paging reduces external fragmentation, but still suffer from internal fragmentation.
• Paging is simple to implement and assumed as an efficient memory management technique.
• Due to equal size of the pages and frames, swapping becomes very easy.
• Page table requires extra memory space, so may not be good for a system having small RAM.
Address space
Address uniquely identifies a location in the memory. We have two types of addresses that are logical
address and physical address.
Logical Address is generated by CPU while a program is running. The logical address is virtual
address as it does not exist physically, therefore, it is also known as Virtual Address. This address is
used as a reference to access the physical memory location by CPU. The term Logical Address Space
is used for the set of all logical addresses generated by a program’s perspective.
The hardware device called Memory-Management Unit is used for mapping logical address to its
corresponding physical address.
CPU generates a logical address consisting of two parts-
p d
Page Number Page Offset
Page Number specifies the specific page of the process from which CPU wants to read the data.
Page Offset specifies the specific word on the page that CPU wants to read.

Physical Address identifies a physical location of required data in a memory. The user never
directly deals with the physical address but can access by its corresponding logical address. The
user program generates the logical address and thinks that the program is running in this logical
address but the program needs physical memory for its execution, therefore, the logical address
must be mapped to the physical address by MMU before they are used. The term Physical Address
Space is used for all physical addresses corresponding to the logical addresses in a Logical address
space.
The frame number combined with the page offset forms the required physical address.
f d
Frame Number Offset

Frame number specifies the specific frame where the required page is stored.
Page Offset specifies the specific word that has to be read from that page.
Page Table-
Page table is a data structure stored in the main memory. It maps the page number referenced by the
CPU to the frame number where that page is stored.
Number of entries in a page table = Number of pages in which the process is divided.
Each process has its own independent page table. Page Table Base Register (PTBR) contains the base
address of page table.

Working-
• Page Table Base Register (PTBR) provides the base address of the page table.
• The base address of the page table is added with the page number referenced by the CPU.
• It gives the entry of the page table containing the frame number where the referenced page is
stored.
Page Table Entry-
• A page table entry contains several information about the page.
• The information contained in the page table entry varies from operating system to operating
system.
• The most important information in a page table entry is frame number.
In general, each entry of a page table contains the following information-

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.
2. Present / Absent Bit-
• This bit is also sometimes called as valid / invalid bit.
• This bit specifies whether that page is present in the main memory or not.
• If the page is not present in the main memory, then this bit is set to 0 otherwise set to 1.
3. Protection Bit-
• This bit is also sometimes called as “Read / Write bit“.
• This bit is concerned with the page protection.
• It specifies the permission to perform read and write operation on the page.
• If only read operation is allowed to be performed and no writing is allowed, then this bit is set
to 0.
• If both read and write operation are allowed to be performed, then this bit is set to 1.
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.
5. Caching Enabled / Disabled-
• This bit enables or disables the caching of page.
• Whenever freshness in the data is required, then caching is disabled using this bit.
• If caching of the page is disabled, then this bit is set to 1 otherwise set to 0.
6. Dirty Bit-
• This bit is also sometimes called as “Modified bit“.
• This bit specifies whether that page has been modified or not.
• If the page has been modified, then this bit is set to 1 otherwise set to 0.
Virtual Memory
Virtual Memory is a storage scheme that provides user an illusion of having a very big main memory.
This is done by treating a part of secondary memory as the main memory.
In this scheme, User can load the bigger size processes than the available main memory by having the
illusion that the memory is available to load the process.
Instead of loading one big process in the main memory, the Operating System loads the different
parts of more than one process in the main memory.
By doing this, the degree of multiprogramming will be increased and therefore, the CPU utilization
will also be increased.
In this scheme, whenever some pages needs to be loaded in the main memory for the execution and
the memory is not available for those many pages, then in that case, instead of stopping the pages
from entering in the main memory, the OS search for the RAM area that are least used in the recent
times or that are not referenced and copy that into the secondary memory to make the space for the
new pages in the main memory. Since all this procedure happens automatically, therefore it makes
the computer feel like it is having the unlimited RAM.
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
Following are the advantages of Demand Paging −
• Large virtual memory.
• More efficient use of memory.
• There is no limit on degree of multiprogramming.
Disadvantages
• 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.
Page Replacement Algorithm
Page replacement algorithms are the techniques using which an Operating System decides which
memory pages to swap out, write to disk when a page of memory needs to be allocated. Paging
happens whenever a page fault occurs and a free page cannot be used for allocation purpose
accounting to reason that pages are not available or the number of free pages is lower than required
pages. When the page that was selected for replacement and was paged out, is referenced again, it
has to read in from disk, and this requires for I/O completion. 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 looks at the limited information about accessing the pages provided
by hardware, and tries to select which pages should be replaced to minimize the total number of
page misses, while balancing it with the costs of primary storage and processor time of the
algorithm itself. There are many different page replacement algorithms. We evaluate an algorithm
by running it on a particular string of memory reference and computing the number of page faults,
Reference String
The string of memory references is called reference string. Reference strings are generated
artificially or by tracing a given system and recording the address of each memory reference. The
latter choice produces a large number of data, where we note two things.
• For a given page size, we need to consider only the page number, not the entire address.
• If we have a reference to a page p, then any immediately following references to page p will
never cause a page fault. Page p will be in memory after the first reference; the immediately
following references will not fault.
• For example, consider the following sequence of addresses − 123,215,600,1234,76,96
• If page size is 100, then the reference string is 1,2,6,12,0,0
First In First Out (FIFO) algorithm
• Oldest page in main memory is the one which will be selected for replacement.
• Easy to implement, keep a list, replace pages from the tail and add new pages at the head.

Example: Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames.

Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3 Page
Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —>1 Page
Fault.
6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page Fault.
Finally when 3 come it is not avilable so it replaces 0 1 page fault
Belady’s anomaly – Belady’s anomaly proves that it is possible to have more page faults when
increasing the number of page frames while using the First in First Out (FIFO) page replacement
algorithm. For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots, we
get 9 total page faults, but if we increase slots to 4, we get 10 page faults.

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.
Example: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page frame.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already there so —> 0 Page fault.
when 3 came it will take the place of 7 because it is not used for the longest duration of time in the
future.—>1 Page fault.
0 is already there so —> 0 Page fault..
4 will takes place of 1 —> 1 Page Fault.

Now for the further page reference string —> 0 Page fault because they are already available in
the memory.

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.

Example: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4 page frames.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in the
memory.

You might also like