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

Unit 5. Memory Management

Uploaded by

akshayapamul7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Unit 5. Memory Management

Uploaded by

akshayapamul7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter no 5: Memory management

Memory Management

Basics of Memory management:

Two important terms considered while loading programs into the partition are
internal fragmentation and external fragmentation.

Internal fragmentation: is memory which is internal to a region but not being


used i.e. a memory space wasted in a partition. If block of data is smaller than
partition size then the internal fragmentation takes place.

External fragmentation: occurs when memory partition is available and unused


but too small for any waiting process i.e. process size is greater than the size of
available partition.

Fixed Partitioning: - Main memory is divided into multiple partitions of fixed size
at the time of system generation. A process may be loaded into a partition of equal
size or greater size.

 Partitions can be of equal size or unequal size.

A) Equal size partitioning: - Main memory is divided into equal size


partitions. Any process with less or equal size can be loaded in any available
partition. If all partitions are full and no process is in ready or running state
then the operating system can swap a process out of any partition and load in
any other process.

Disadvantage: -Main memory Utilization is extremely inefficient.

B) Unequal size partitioning: - Main memory is divided into multiple


partitions of unequal size. Each process can be loaded into the smallest
partition within which the process will fit.

1
Variable partitioning:-

 When a process enters in main memory, it is allocated exact size that is


required by that process. So in this method, partitions can vary in size
depending on memory space required by a process entering in main
memory.
 Operating system maintains a table indicating which parts of memory are
available and which are occupied. When new process arrives and it needs
space, system searches for available memory space in main memory. If it is
available then memory is allocated to the process by creating a partition in
memory.
 Like this depending on size of process and available memory, partitions
take place in main memory.

For example:-Consider following table with process and memory space.

Process Memory Space


P1 20 M
P2 14M
P3 18M
P4 8M
2
P5 10M

Process of memory allocation:-

Total memory size is 64 M .From this 8 M partition is occupied by operating


system and remaining can be partitioned as per the size of processes.

3
Free-space Management

1) Bitmap
2) Linked list

Bitmaps:
 When memory is assigned dynamically, the operating system must manage
it.
 With a bitmap, memory is divided up into allocation units, starting from
few words to several kilobytes.
 Corresponding to each allocation unit is a bit in the bitmap, which is 0 if
the unit is free and 1if it is occupied (or vice versa).
 Figure below shows part of memory and the corresponding bitmap:

ss

Fig:

(a) A part of memory with five processes and three holes. The tick marks show the
memory allocation units. The shaded regions (0 in the bitmap) are free.

(b) The corresponding bitmap.

(c) The same information as a list.

 The size of the allocation unit is an important design issue.


 The smaller the allocation unit, the larger the bitmap.
 A bitmap provides a simple way to keep track of memory words in a fixed
amount of memory because the size of the bitmap depends only on the size
of memory and the size of the allocation unit.
 The main problem with it is that when it has been decided to bring a k unit
process into memory, the memory manager must search the bitmap to find
4
a run of k consecutive 0 bits in the map. Searching a bitmap for a run of a
given length is a slow operation.

Linked Lists
 Another way of keeping track of memory is to maintain a linked list of
allocated and free memory segments, where a segment is either a process or
a hole between two processes.
 Each entry in the list specifies a hole (H) or process (P), the address at
which it starts, the length, and a pointer to the next entry.
 In this example, the segment list is kept sorted by address. Sorting this way
has the advantage that when a process terminates or is swapped out,
updating the list is straightforward.
 A terminating process normally has two neighbors (except when it is at the
very top or very bottom of memory). These may be either processes or
holes, leading to the four combinations shown in fig below.

 When the processes and holes are kept on a list sorted by address, several
algorithms can be used to allocate memory for a newly created process (or
an existing process being swapped in from disk). First fit, Best Fit, Worst
fit etc.

Virtual memory

 Virtual memory is a technique that allows the execution of processes which


are not completely available in memory.
 The main visible advantage of this scheme is that programs can be larger
than physical memory.
 Virtual memory is the separation of user logical memory from physical
memory.
5
 This separation allows an extremely large virtual memory to be provided for
programmers when only a smaller physical memory is available.

Following are the situations, when entire program is not required to be


loaded fully in main memory.

 User written error handling routines are used only when an error occurred in
the data or computation.
 Certain options and features of a program may be used rarely.
 Many tables are assigned a fixed amount of address space even though only
a small amount of the table is actually used.
 The ability to execute a program that is only partially in memory would
counter many benefits.
 Less number of I/O would be needed to load or swap each user program into
memory.
 A program would no longer be constrained by the amount of physical
memory that is available.
 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.

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.

6
Paging

 External fragmentation is avoided by using paging technique.


 Paging is a technique in which logical memory is broken into blocks of the
same size called pages.
 When a process is to be executed, its corresponding pages are loaded into
any available memory frames.
 Logical address space of a process can be non-contiguous and a process is
allocated physical memory whenever the free memory frame is available.
 Operating system keeps track of all free frames.
 Operating system needs n free frames to run a program of size n pages.

 Following figure show the paging table architecture.

7
Address generated by CPU is divided into

 Page number (p) -- page number is used as an index into a page table
which contains base address of each page in physical memory.
 Page offset (d) -- page offset is combined with base address to define the
physical memory address.

Demand Paging
 A demand paging system is quite similar to a paging system with swapping.
When we want to execute a process, we swap it into memory. Rather than
swapping the entire process into memory, however, we use a lazy swapper
called pager.
 When a process is to be swapped in, the pager guesses which pages will be
used before the process is swapped out again. Instead of swapping in a
whole process, the pager brings only those necessary pages into memory.
Thus, it avoids reading into memory pages that will not be used in anyway,
decreasing the swap time and the amount of physical memory needed.
 Hardware support is required to distinguish between those pages that are in
memory and those pages that are on the disk using the valid-invalid bit
scheme.
 Where valid and invalid pages can be checked by checking the bit. Marking
a page will have no effect if the process never attempts to access the page.
While the process executes and accesses pages that are memory resident,
execution proceeds normally.

8
Access to a page marked invalid causes a page-fault trap. This trap is the result of
the operating system's failure to bring the desired page into memory. But page fault
can be handled as following

Swapping

 Swapping is a mechanism in which a process can be swapped temporarily


out of main memory to a backing store, and then brought back into memory
for continued execution.
 Backing store is a usually a hard disk drive or any other secondary storage
which is fast in access and large enough to accommodate copies of all
memory images for all users. It must be capable of providing direct access
to these memory images.
 Major time consuming part of swapping is transfer time. Total transfer time
is directly proportional to the amount of memory swapped.

9
Segmentation

 In Operating Systems, Segmentation is a memory management technique in


which, the memory is divided into the variable size parts.
 Each part is known as segment which can be allocated to a process.
 The details about each segment are stored in a table called as segment table.
Segment table is stored in one (or many) of the segments.

Segment table contains mainly two information about segment:


1. Base: It is the base address of the segment
2. Limit: It is the length of the segment.

10
CPU generates a logical address which contains two parts:

1. Segment Number
2. Offset

The Segment number is mapped to the segment table. The limit of the respective
segment is compared with the offset. If the offset is less than the limit then the
address is valid otherwise it throws an error as the address is invalid.

In the case of valid address, the base address of the segment is added to the offset
to get the physical address of actual word in the main memory.

 Advantages

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 compare to the page table in paging.

 Disadvantages

1. It can have external fragmentation.


2. It is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.

11
Difference between paging and segmentation

Sr.No. Paging Segmentation


1. A page is a contiguous range of A segment is an independent
memory addresses which is address space. Each segment has
mapped to physical memory. addresses in a range from 0 to
maximum value.

2. It has only one linear address It has many address spaces.


space.
3. Programmer does not know that it Programmer knows that it is
is implemented implemented.
4. Procedures and data cannot be Procedures and data can be
separated separated
5. Procedures cannot be shared Procedures can be shared between
between users users
6. Procedures and data cannot be Procedures and data can be
protected separately protected separately
7. A page is a physical unit A segment is a logical unit
8. A page is of fixed size A segment is of arbitrary size.

Page Replacement Algorithm


Page replacement algorithms are the techniques using which 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.

12
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.

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.

13
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.

14

You might also like