0% found this document useful (0 votes)
37 views39 pages

Lecture 8 - Memorymanagement

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

Lecture 8 - Memorymanagement

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

Memory Management

Objectives

You will be able to describe:


 The basic functionality of two memory allocation
schemes: fixed partitions and dynamic partitions.
 Best-fit memory allocation as well as first-fit memory
allocation schemes
 How a memory list keeps track of available memory
 The importance of the bounds register in memory
allocation schemes
 The role of compaction and how it improves memory
allocation efficiency
Fixed Partitioning
 Create fixed partitions within the main memory - one
partition per job.
 Equal-size partitions
 Unequal-size partitions
 Allows multiprogramming - allows several programs to
be in memory at the same time.
 Any process whose size is less than or equal to the

partition size can be loaded into an available partition


 If all partitions are full, the operating system can swap

a process out of a partition


 A program may not fit in a partition. The programmer

must design the program with overlays


Fixed Partitioning

 Requires that the ENTIRE program to be


stored CONTINUOUSLY and in memory
from beginning till the end of execution.
 Main memory use is inefficient. Any
program, no matter how small, occupies
an entire partition. This is called internal
fragmentation.
Placement Algorithm with Partitions

 Equal-size partitions
 Because all partitions are of equal size, it
does not matter which partition is used
 Unequal-size partitions
 Can assign each process to the smallest
partition within which it will fit
 Queue for each partition

 Processes are assigned in such a way as to

minimize wasted memory within a partition


Fixed Partitions
 Fixed Partitions: Main memory is partitioned; one
partition/job
 Allows multiprogramming

 Partition sizes remain static unless and until

computer system is shut down, reconfigured, and


restarted
 Requires protection of the job’s memory space

 Requires matching job size with partition size


Fixed Partitions (continued)

To allocate memory spaces to jobs, the operating system’s


Memory Manager must keep a table as shown below:

A simplified fixed partition memory table with the free


partition shaded
Fixed Partitions (continued)

NOTE: Job 3 must


wait even though
70K of free space
is available in
Partition 1 where
Job 1 occupies
only 30K of the
100K available

Main memory use during fixed partition allocation of previous


slide
Fixed Partitions (continued)

 Disadvantages:
 Requires entire program to be stored contiguously
 Jobs are allocated space on the basis of first
available partition of required size
 Works well only if all of the jobs are of the same
size or if the sizes are known ahead of time
 Arbitrary partition sizes lead to undesired results
 Too small a partition size results in large jobs

having longer turnaround time


 Too large a partition size results in memory

waste or internal fragmentation


Dynamic Partitions

 Dynamic Partitions: Jobs are given only as much


memory as they request when they are loaded
 Available memory is kept in contiguous blocks
 Memory waste is comparatively small
 Disadvantages:
 Fully utilizes memory only when the first jobs are
loaded
 Subsequent allocation leads to memory waste or
external fragmentation
Dynamic Partitions (continued)

Main memory use during dynamic partition allocation


Dynamic Partitions (continued)

(continued): Main memory use during dynamic partition allocation


Best-Fit Versus First-Fit Allocation

 Free partitions are allocated on the following basis:


 First-fit memory allocation: First partition fitting

the requirements
 Leads to fast allocation of memory space

 Best-fit memory allocation: Smallest partition


fitting the requirements
 Results in least wasted space

 Internal fragmentation reduced but not

eliminated
Best-Fit Versus First-Fit Allocation
(continued)
 First-fit memory allocation:
 Advantage: Faster in making allocation

 Disadvantage: Leads to memory waste

 Best-fit memory allocation


 Advantage: Makes the best use of memory space

 Disadvantage: Slower in making allocation


Best-Fit Versus First-Fit Allocation
(continued)

An example of a first-fit free scheme


Best-Fit Versus First-Fit Allocation
(continued)

An example of a best-fit free scheme


Best-Fit Versus First-Fit Allocation
(continued)
 Algorithm for First-Fit:
 Assumes Memory Manager keeps two lists, one for

free memory and one for busy memory blocks


 Loop compares the size of each job to the size of

each memory block until a block is found that’s


large enough to fit the job
 Job is stored into that block of memory

 Memory Manager moves out of the loop to fetch the

next job from the entry queue


Best-Fit Versus First-Fit Allocation
(continued)
 Algorithm for First-Fit (continued):
 If the entire list is searched in vain, then the job is

placed into a waiting queue


 The Memory Manager then fetches the next job

and repeats the process


Best-Fit Versus First-Fit Allocation
(continued)
 Example of First fit memory allocation
Job List Memory
Requested
Job 1 10K
Job 2 20K
Job 3 30K
Job 4 10K

Memory Memory Job Job Status Internal


Location Block Size Number Size Fragmentation
10240 30K J1 10K Busy 20K
40960 15K J4 10K Busy 5K
56320 50K J2 20K Busy 30K
107520 20K Free
115K 40K

J3 has to wait
Best-Fit Versus First-Fit Allocation
(continued)
 Algorithm for Best-Fit:
 Goal: find the smallest memory block into which the

job will fit


 Entire table must be searched before allocation
Best-Fit Versus First-Fit Allocation
(continued)
 2. Best-fit memory allocation - keeps the free/busy lists in order by
size, smallest to largest.
 Advantage : Optimises space
 Disadvantage : Wasting time to search for the best space
Job List Memory
Requested
Job 1 10K
Job 2 20K
Job 3 30K
Job 4 10K
Memory Memory Job Job Status Internal
Location Block Size Number Size Fragmentation

40960 30K J3 30K Busy None


107520 15K J1 10K Busy 5K
10240 50K J4 10K Busy 40K
56320 20K J2 20K Busy None
115K 70K

Smaller internal fragmentation occurs known as sliver.


Next-Fit and Worst-Fit

 Hypothetical allocation schemes:


 Next-fit: Starts searching from last allocated block,

for the next available block when a new job arrives


 Worst-fit: Allocates the largest free available block

to the new job


 Opposite of best-fit

 Good way to explore the theory of memory

allocation; might not be the best choice for an


actual system
Next-Fit and Worst-Fit

 3. Next-fit memory allocation -keeps track of the last


allocated partition and starts searching from that point on
when a new job arrives.
Job List Memory
Requested
Job 1 10K
Job 2 20K
Job 3 30K
Job 4 10K
Memory Memory Job Job Status Internal
Location Block Size Number Size Fragmentation
40960 30K J1 10K Busy 5K
107520 15K Free
10240 50K J2 20K Busy 30K
56320 20K J4 10K Busy 10K
115K 40K

J3 has to wait
Next-Fit and Worst-Fit

 3. Worst-fit memory allocation -allocates the largest free


block to a new job.
Job List Memory
Requested
Job 1 10K
Job 2 20K
Job 3 30K
Job 4 10K
Memory Memory Job Job Status Internal
Location Block Size Number Size Fragmentation
40960 30K J2 20K Busy 10K
107520 15K Free
10240 50K J1 10K Busy 40K
56320 20K J4 10K Busy 10K
115K 40K

J3 has to wait
Relocatable Dynamic Partitions

 Relocatable Dynamic Partitions:


 Memory Manager relocates programs to gather

together all of the empty blocks


 Compact the empty blocks to make one block of

memory large enough to accommodate some or all


of the jobs waiting to get in
Relocatable Dynamic Partitions
(continued)
 Compaction: Reclaiming fragmented sections of the
memory space
 Every program in memory must be relocated so

they are contiguous


 Operating system must distinguish between

addresses and data values


 Every address must be adjusted to account for

the program’s new location in memory


 Data values must be left alone
Relocatable Dynamic Partitions
(continued)
 Compaction issues:
 What goes on behind the scenes when relocation

and compaction take place?


 What keeps track of how far each job has moved

from its original storage area?


 What lists have to be updated?
Relocatable Dynamic Partitions
(continued)
 What lists have to be updated?
 Free list must show the partition for the new block

of free memory
 Busy list must show the new locations for all of the

jobs already in process that were relocated


 Each job will have a new address except for those

that were already at the lowest memory locations


Relocatable Dynamic Partitions
(continued)
 Special-purpose registers are used for relocation:
 Bounds register

 Stores highest location accessible by each

program
 Relocation register

 Contains the value that must be added to each

address referenced in the program so it will be


able to access the correct memory addresses
after relocation
 If the program isn’t relocated, the value stored in

the program’s relocation register is zero


Relocatable Dynamic Partitions
(continued)

Three snapshots of memory before and after


compaction
Relocatable Dynamic Partitions
(continued)
 Compacting and relocating optimizes the use of
memory and thus improves throughput
 Options for when and how often it should be done:
 When a certain percentage of memory is busy

 When there are jobs waiting to get in

 After a prescribed amount of time has elapsed

Goal: Optimize processing time and memory use


while keeping overhead as low as possible
Relocation

 When program is loaded into memory the


actual (absolute) memory locations are
determined
 A process may occupy different partitions
which means different absolute memory
locations during execution (from swapping)
 Compaction will also cause a program to
occupy a different partition which means
different absolute memory locations
Addresses

 Logical
 Reference to a memory location independent of the
current assignment of data to memory
 Translation must be made to the physical address

 Relative
 Address expressed as a location relative to some
known point
 Physical
 The absolute address or actual location in main
memory
Summary

 Two memory management techniques were used in


early systems: fixed partitions and dynamic partitions
 Memory waste in dynamic partitions is comparatively
small as compared to fixed partitions
 First-fit is faster in making allocation but leads to
memory waste
 Best-fit makes the best use of memory space but
slower in making allocation
Summary (continued)

 Compacting and relocating optimizes the use of


memory and thus improves throughput
 Both techniques require that the entire program must:
 Be loaded into memory
 Be stored contiguously
 Remain in memory until the job is completed
 Each technique puts severe restrictions on the size of
the jobs: can only be as large as the largest partitions in
memory
Exercise
 The shaded areas are allocated blocks; the white areas are free
blocks. The next three memory requests are for 40M, 30M and
20M. Indicate the starting address for each of the three memory
blocks using the following placement algorithms:
 First-fit
 Best-fit
 Next-fit. Assume the most recently added block is at the beginning of memory
 Worst-fit

20M 20M 40M 60M 20M 10M 60M 40M 20M 30M 40M 40M

You might also like