You are on page 1of 57

MODULE III

MEMORY MANAGEMENT
Memory Management
 To improve the performance of a computer, several processes

must keep in memory

 Many memory management schemes exist, reflecting various

approaches

 The effectiveness of each algorithm depends on the situation

 Selection of a memory management scheme for a system

depends on many factors, especially on the hardware design


03/10/2023 Unit III Memory Management
Address Bindings
 Addresses in the source program are generally symbolic.
 A compiler typically binds these symbolic addresses to
relocatable addresses .
 The linkage editor or loader binds the relocatable addresses
to absolute addresses
 Each binding is a mapping from one address space to
another.
Address Bindings
1. COMPILE TIME

2. LOAD TIME

3. EXECUTION TIME/RUN TIME

03/10/2023 Unit III Memory Management


Address Bindings
COMPILE TIME

 If the address of the process is known at compile time, then


absolute code can be generated.
 The MS-DOS .COM-format programs are absolute code bound
at compile time

03/10/2023 Unit III Memory Management


Address Bindings
LOAD TIME

 If the address of the process is not known at compile time, then


the compiler must generate relocatable code .
 In this case, final binding is delayed until load time.
 If the starting address changes, we need only reload the user
code to incorporate this changed value

03/10/2023 Unit III Memory Management


Address Bindings
EXECUTION TIME/RUN TIME

 If the process can be moved during its execution from one


memory segment to another, then binding must be delayed until
run time.

03/10/2023 Unit III Memory Management


Logical & Physical Address
 LOGICAL ADDRESS – address generated by the CPU; also

referred to as virtual address


 PHYSICAL ADDRESS – address seen by the memory unit—that

is, the one loaded into the memory-address register of the


memory—is referred to as a physical address.
 Compile-time and load-time address-binding methods
generate identical logical and physical addresses

03/10/2023 Unit III Memory Management


Logical & Physical Address
 Execution-time address binding scheme results in differing
logical and physical addresses

 Memory Management Unit performs the mapping from


virtual to physical addresses at run time

 Value in the base register called relocation register is added


to every address generated by a user process at the time it is
sent to memory
03/10/2023 Unit III Memory Management
Logical & Physical Address

03/10/2023 Unit III Memory Management


Contiguous Memory Allocation
 Memory is usually divided into two partitions: one for the

resident operating system, and one for the user processes

 Usually the operating system is in the low memory

 Usually several user processes reside in high memory at

the same time

03/10/2023 Unit III Memory Management


Contiguous Memory Allocation
1. Fixed sized partition

2. Variable sized partition

03/10/2023 Unit III Memory Management


Fixed Sized Partition
 Divide memory into several fixed-sized partitions

 Each partition may contain exactly one process

 When the process terminates, the partition becomes

available for another process

 When a partition is free, a process is loaded into it

 E.g. IBM OS/360 operating system


03/10/2023 Unit III Memory Management
Fixed Sized Partition
OS OS OS OS
Process 5 Process 5
Process 6 Process 6 Process 6 Process 6

Process 3 Process 3 Process 3

Process 9 Process 9 Process 9 Process 9

03/10/2023 Unit III Memory Management


Variable Sized Partition
 Operating system keeps a table indicating which parts of

memory are available and which are occupied

 When process arrives and needs memory, OS search for a

hole large enough for this process

 If find one, OS allocate only as much memory as is needed

03/10/2023 Unit III Memory Management


Variable Sized Partition

OS OS OS OS

process 5 process 5 process 5 process 5


process 9
process10
process 8

process 2 process 2 process 2 process 2

03/10/2023 Unit III Memory Management


Variable Sized Partition
STRATEGIES USED TO SELECT MEMORY PARTITIONS

1. First Fit

2. Best Fit

3. Worst Fit

03/10/2023 Unit III Memory Management


Variable Sized Partition
1. FIRSTFIT - Allocate the first hole that is big enough.

 Searching can start either at the beginning of the set of

holes or where the previous first-fit search ended

 Stop searching as soon as we find a free hole that is large

enough.

03/10/2023 Unit III Memory Management


Variable Sized Partition
2. BESTFIT: Allocate the smallest hole that is big enough

 Search the entire list, unless the list is kept ordered by size

 This strategy produces the smallest leftover hole.

03/10/2023 Unit III Memory Management


Variable Sized Partition
3. WORST FIT: Allocate the largest hole

 Search the entire list, unless it is sorted by size

 This strategy produces the largest leftover hole

 More useful than the smaller leftover hole from best-fit

approach.
03/10/2023 Unit III Memory Management
Fragmentation
 Processes are loaded and removed from memory, the free

memory space is fragmented into large number of small

holes

 Memory fragmentation can be internal as well as external

03/10/2023 Unit III Memory Management


Internal Fragmentation
 Fixed sized memory allocation suffer internal fragmentation

 Memory allocated to a process may be slightly larger than

the requested memory

 The difference between these two sizes is internal

fragmentation

 Memory that is internal to a partition but is not being used

03/10/2023 Unit III Memory Management


External Fragmentation
 External fragmentation exists when enough total memory

space exists to satisfy a request, but it is not contiguous

 Memory is fragmented into a large number of small holes

 Solution - Compaction : shuffle the memory contents to place

all free memory together in one large block

 Permit logical address space of a process to be noncontiguous


03/10/2023 Unit III Memory Management
Paging
 Memory management scheme that permits the physical-

address space of a process to be non-contiguous

 Physical memory is broken into fixed-sized blocks: frames

 Logical memory is also broken into same size blocks: pages

03/10/2023 Unit III Memory Management


Paging
 When a process is to be executed, its pages are loaded

into any available memory frames

 Every address generated by the CPU is divided into two

parts: a page number (p) and a page offset (d)

 Page number is used as an index into a page table


03/10/2023 Unit III Memory Management
Paging
 Page table contains the base address of each page in

physical memory

 This base address is combined with the page offset to

define the physical memory address that is sent to the

memory unit
03/10/2023 Unit III Memory Management
Paging

03/10/2023 Unit III Memory Management


Segmentation

03/10/2023 Unit IV
1. SQL
Logical View of Segmentation

4
1

3 2
4

user space physical memory space


Segmentation
 Memory management scheme that supports user view of

memory

 A logical-address space is a collection of segments

 Each segment has a name and a length

 The addresses specify both the segment name and the

offset within the segment


03/10/2023 Unit III Memory Management
Segmentation
 The mapping of logical address into physical address is

affected by a segment table

 Each entry of the segment table has a segment base and a

segment limit

 A logical address consists of two parts: a segment number,

s, and an offset into that segment, d.


03/10/2023 Unit III Memory Management
Segmentation
 The segment number s is used as an index into the

segment table

 The offset d of the logical address must be between 0 and

the segment limit

03/10/2023 Unit III Memory Management


Segmentation

03/10/2023 Unit III Memory Management


Segmentation Vs Paging
Segmentation Paging
User view of memory System view of memory
View memory as a collection of View memory as a collection of
variable-sized segments, with no fixed-sized frames
necessary ordering among
segments

The addresses specify both the user specified only a single


segment name and the offset address. Hardware partitioned
within the segment the address into a page number
and offset

User specifies the address Address is invisible to user

03/10/2023 Unit III Memory Management


Virtual Memory
 Technique that allows the execution of processes that may

not be completely in memory

 Logical address space can be much larger than physical

address space.

 Allows address spaces to be shared by several processes

 Allows for more efficient process creation


03/10/2023 Unit III Memory Management
Virtual Memory

03/10/2023 Unit III Memory Management


Virtual Memory
 Virtual memory is commonly implemented by demand paging

 Also be implemented in a segmentation system

 Several systems provide a paged segmentation scheme

 Demand segmentation can also be used to provide virtual

memory

03/10/2023 Unit III Memory Management


Virtual Memory
 Virtual memory is commonly implemented by

1. Demand paging

2. Demand segmentation

03/10/2023 Unit III Memory Management


Demand Paging
 Paging system with swapping

 Bring a page into memory only when it is needed

 Less I/O needed

 Less memory needed

 Faster response

 More users
03/10/2023 Unit III Memory Management
Demand Paging
 Need some form of hardware support to distinguish between

pages that are in memory and pages that are on the disk

 Valid-invalid bit scheme can be used for this purpose

 When this bit is set to "valid,“ indicates the associated page is

both legal and in memory

 If the bit is set to "invalid,“, indicates the page either is not

valid, or is valid but is currently on the disk.


03/10/2023 Unit III Memory Management
Demand Paging

03/10/2023 Unit III Memory Management


Page Fault
 Access to a page marked invalid causes a page-fault

 Paging hardware, in translating the address through the

page table, will notice that the invalid bit is set, causing a

trap to the operating system

 This trap is the result of the operating system's failure to

bring the desired page into memory


03/10/2023 Unit III Memory Management
Page Fault Handling
1. Check internal table for this process to determine whether

the reference was a valid or invalid memory access

2. If the reference was invalid, we terminate the process

3. If it was valid, and is not brought in, now page in

a) Find a free frame.

b) Read the desired page into the newly allocated frame.

c) Modify the internal tables


03/10/2023 Unit III Memory Management
Demand Paging

03/10/2023 Unit III Memory Management


Page Replacement
 Takes place if no frame is free to start a new page for a

process

 Find a page that is not currently being used.

 Free a frame by writing its contents to swap space, and

changing the page table

 Now use the freed frame to hold the page

03/10/2023 Unit III Memory Management


Page Replacement Algorithms
1. FIFO Page replacement

2. Optimal page replacement

3. LRU page replacement

03/10/2023 Unit III Memory Management


FIFO Page Replacement
 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

 Implemented using FIFO queue

03/10/2023 Unit III Memory Management


FIFO Page Replacement

15 page faults
03/10/2023 Unit III Memory Management
Belady’s Anomaly
 For some page-replacement algorithms, the page fault

rate may increase as the number of allocated frames

increases.
 Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5
 Adding more frames can cause more page faults!
 Belady’s Anomaly

03/10/2023 Unit III Memory Management


Optimal Page Replacement
 Replace the page that will not be used for the longest period

of time

 Use of this page-replacement algorithm guarantees the

lowest possible page fault rate for a fixed number of frames

 Optimal page-replacement algorithm is difficult to

implement, because it requires future knowledge of the

reference string
03/10/2023 Unit III Memory Management
Optimal Page Replacement

9 page faults
03/10/2023 Unit III Memory Management
LRU Page Replacement
 Recent past is used as an approximation of the near future,

then replace the page that has not been used for the

longest period of time

 LRU (Least Recently Used) replacement associates with

each page the time of that page's last use

03/10/2023 Unit III Memory Management


LRU Page Replacement
 When a page must be replaced, LRU chooses that page

that has not been used for the longest period of time

 This strategy is the optimal page-replacement algorithm

looking backward in time, rather than forward.

03/10/2023 Unit III Memory Management


LRU Page Replacement

12 page faults

03/10/2023 Unit III Memory Management


Thrashing
 If the process does not have enough number of frames, it

will quickly page fault.

 Replacing an active page causes page fault again

 This high paging activity is called thrashing

 A process is thrashing if it is spending more time paging

than executing
03/10/2023 Unit III Memory Management
Thrashing (Cont.)

You might also like