Unity University
Faculty of Engineering , Technology and Computational Sciences
Department of Computer Science
Course Title: Operating System (COSC 3052)
Topic: Memory management
Learning objectives
After completing this topic, you will be able to:
Explain role of OS with regard to Memory Mgmt
Describe ways of organizing memory hardware
Discuss various memory-management schemes:
a) Contiguous memory partitioning (Fixed-size, Variable-size)
b) Dynamic Partitioning Placement Algorithm( First-fit, Best-fit,Worst-fit)
Introduction to Memory Management
Main memory usually divided into two partitions:
– Resident operating system, usually held in low memory with interrupt vector.
– User processes then held in high memory.
RAM
Example:
Note
Note
0000
In
In the
the multiprogramming
multiprogramming environment
environment
Operating System the main
the main memory
memory must
must accommodate
accommodate
400K both the
both the operating
operating system
system and
and various
various
user processes.
user
Subdividing
Subdividing the
the memory
memory (into
(into number
number of
of
partitions that
partitions that isis carried
carried out
out dynamically
dynamically
user processes
by the
by the OS)
OS) to
to accommodate
accommodate multiple
multiple
(2160K)
processes
processes
Memory
Memory needs
needs to
to be
be allocated
allocated efficiently
efficiently
to pack
to pack as
asmany
many processes
processes into
intomemory
memory as
as
2560K possible
possible
Fig. 1 Main memory partition
Source: Feleke Merin (Dr.-Engr.) Page 1 of 12
Unity University
Memory management in multi-programming system concerned with three functions. These are:
i) To keep track of which parts of memory are currently used and which parts are not in use,
ii) To allocate memory to processes when they need it and de-allocate it when they are done,
iii) To manage swapping b/n main memory and hard-disk when main memory is too small to hold all
the processes.
Swapping
Remove a process temporarily from main memory to a backing store and later, bring it back into
memory for continued execution
Roll-out, Roll-in Swapping
Allows to preempt a lower priority process in favor of a higher priority process
After the higher priority process is done, the preempted process is continued
Major part of swap time is transfer time; total transfer time is directly proportional to the
amount of memory swapped.
Process may or may not be swapped into the same memory space depending upon the availability
of execution time binding
Fig. 2 Swapping of two processes using a disk as a backing store
Source: Feleke Merin (Dr.-Engr.) Page 2 of 12
Unity University
Backing store
Preferably a fast disk
Must be large enough to accommodate copies of all memory images of all processes
Must provide direct access to each memory image
Maintain a ready queue of all processes on the backing store
Dispatcher brings the process into memory if needed
Calculation of swap time
User process of size 100K
Backing store Standard head disk with a transfer rate of 1 MB/sec
Actual transfer time 100 msec
Add latency (8 msec) 108 msec
Swap-in + Swap-out 216 msec
Total transfer time directly proportional to the amount of memory swapped
Swap only completely idle processes (not with pending I/O)
Memory partitioning techniques
a) Fixed Partitioning(types: Equal-size partitions and Unequal-size partitions)
1. Equal-size partitions (refer fig. 3 a )
– Divide memory into partitions at boot time, partition sizes don’t change
(i. e. 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 OS can swap a process out of a partition and load-in another process.
Difficulties with the use of equal-size fixed partitions:
a program may be too big to fit into a partition. In this case, the programmer must design the
program with the use of overlays so that only a portion of the program need be in memory at any
time. When a module is needed that is not present, the user’s program must load that module
into the program’s partition, overlaying whatever programs or data are there.
Overlay: is a technique that is used to keep in memory only those instructions and data
that are needed at any given time.
Needed when process is larger than amount of memory allocated to it.
Implemented by user, no special support needed from operating system;
programming design of overlay structure is complex.
Source: Feleke Merin (Dr.-Engr.) Page 3 of 12
Unity University
Simple to implement, but main memory use is extremely inefficient due to internal
fragmentation. The phenomenon, in which there is wasted space internal to a partition
due to the fact that the block of data loaded is smaller than the partition, is referred to as
internal fragmentation.
a) Equal-size partitions b) Unequal-size partitions
Fig. 3 Example of fixed partitioning of a 64-Megabyte memory
Source: Feleke Merin (Dr.-Engr.) Page 4 of 12
Unity University
2. Unequal-size partitions (refer fig. 3 b)
can assign each process to the smallest partition within which it will fit
the advantage of this approach is that processes are assigned in such a way as to minimize
wasted memory within a partition. Adv. of unequal-size partitioning: provides flexibility
Advantages of both types of fixed-partitioning
relatively simple and require minimal operating system software and processing
overhead
Disadvantages of both types of fixed-partitioning
i) the number of partitions specified at the time of system generation limits the
number of active (not suspended) processes in the system
ii) because partition sizes are preset at the time of system generation, small jobs do
not use partition space efficiently.
Note: The use of fixed partitioning is almost unknown today. One example of an OS that
did use this technique was an early IBM mainframe OS (OS/MFT; MFT:
Multiprogramming with a Fixed number of Tasks)
b) Variable-size / dynamic Partitioning
– In this memory partition method the entire user space is treated as a “Big hole”.
– Hole – block of available memory; holes of various size are scattered throughout memory.
– When a process arrives, it is allocated memory from a hole large enough to accommodate it
(i. e. each process is loaded into partition of exactly the same size at that process).
– The boundaries of partitions are dynamically changed, and the boundaries are depend on
the size of the processes.
– Operating system maintains information about:
a) allocated partitions
a) free partitions (hole)
– Partitions are of variable length (size) and number
– Process is allocated exactly as much memory as required
– Avoids internal fragmentation, but must deal with external fragmentation
Source: Feleke Merin (Dr.-Engr.) Page 5 of 12
Unity University
Example: see figure 4 and fig. 5
An example using a 64-Mega byte of main memory
is shown in fig. 4 and fig. 5.
Job queue
Initially, main memory is empty except for the
Processes Memory Time(ms)
operating system (fig. 4 a).
The first three processes (P1, P2, and P3) are P1 20M 10
loaded-in, starting where the operating system P2 14M 20
ends, (see fig. 4 b, c, and d). P3 18M 15
This leaves a “hole” at the end of memory P4 8M 7
that is too small for a fourth process.
Fig. 4 The effect of dynamic partitioning
Source: Feleke Merin (Dr.-Engr.) Page 6 of 12
Unity University
At some point, none of the processes in memory is ready. The OS therefore swaps-out process 2
(fig. 5 e), which leaves sufficient room to load a new process, process 4 (fig. 5 f ).
Because process 4 is smaller than process 2, another small hole is created.
Later, a point is reached at which none of the processes in main memory is ready, but process 2
in the ready, suspend state is available. Because there is insufficient room in memory for process
2, the Operating system swaps process 1 out (fig. 5 g ) and swaps process 2 back-in (fig. 5 h).
Fig. 5 The effect of dynamic partitioning
As this example shows, there are a lot of small holes in memory. As times goes on, memory
becomes more and more fragmented, and memory use declines. This phenomenon is called
external fragmentation.
Must use compaction to shift processes so they are contiguous and all free memory is in one block.
Source: Feleke Merin (Dr.-Engr.) Page 7 of 12
Unity University
• Compaction After Memory Compaction
– Shifting processes so all of the available memory is in one block
Operating System
– Requires processor time to move items around in memory
P2
– Requires relocation hardware that can handle the change in addresses
• Cannot easily find all possible addresses in the program
P4
• Need to move program without changing user addresses
P3
Dynamic Partitioning Placement Algorithm
When it is time to load or swap a process into main memory and if there is
more than one free hole of memory of sufficient size, then the operating 16M
system must decide which free hole to allocate (i. e. how to satisfy a
request of size n from a list of free holes.).
Three placement algorithms that can be considered are:
i) First-fit: It begins to scan memory from the beginning and chooses the first available hole
that is large enough (regardless of size).
ii) Best-fit: Find the smallest available block that will hold the program.
– It chooses the hole that is closest in size to the request; must search entire list,
unless ordered by size. Produces the smallest leftover hole. (Example: Use
30K block for 28K program, leaves 2K)
– Since smallest block is found for process, the smallest amount of fragmentation
is left memory compaction must be done more often
iii) Worst-fit: Allocate the largest hole; must also search entire list. Produces the largest leftover
hole.
Source: Feleke Merin (Dr.-Engr.) Page 8 of 12
Unity University
Example:
Given memory partitions of 100K, 500K, 200K, 300K, and 600K (in-the-order). How would the
following algorithms place processes of P1= 212K, P2=417K, P3=112K, and P4=426K (in-the-order)?
Required: With the help of graphical illustration, calculate the following:
a) The total internal fragmentation caused by first-fit algorithm.
b) The total external fragmentation resulted in by first-fit algorithm.
c) The total internal fragmentation resulted in by best-fit algorithm.
d) The total external fragmentation resulted in by best-fit algorithm.
Answer:
First-fit algorithm
Memory partitions
OS size: 100K Processes Partition Internal Fragmentation
500K P1=212k In 500K 288K
200K P2=417K In 600K 183K
300K P3=112K In 200K 88K
600K
Total 559K
==============================================================
Answer:
Best-fit algorithm
Memory partitions
OS size: 100K Processes Partition Internal Fragmentation
500K P1=212k In 300K 88K
200K P2=417K In 500K 83K
300K P3=112K In 200K 88K
600K P4=426K In 600K 174K
Total 433K
Source: Feleke Merin (Dr.-Engr.) Page 9 of 12
Unity University
Memory protection
Main memory usually divided into two partitions (refer fig. 1):
– Resident operating system, usually held in low memory with interrupt vector.
– User processes then held in high memory.
0000
OS
800K
1200K Job 1 120K 1200K Base register
1800K Job 2 180K 600K Limit register
2200K Job 3
3200K Job n
Fig. 7 Base and Limit registers define a logical address
To ensure correct operation me must protect :
i) The operating system from access by user programs
ii) User programs from one another.
Q: How?
A: This protection must be provided by the hardware. Consider the fig. 7, it depicts the hardware
protection mechanism with base and limit registers.
– The base register holds the smallest legal physical address (1200K);
– The limit register contains the size of the process (600K).
– Then the program can legally access all addresses from 1200K through 1800K inclusive.
This protection is accomplished by the CPU hardware comparing every address
generated in user mode with the registers. When the CPU-Scheduler selects a process for
execution, the OS (the dispatcher) loads the base and limits registers with the correct
values as part of the context switch.
Source: Feleke Merin (Dr.-Engr.) Page 10 of 12
Unity University
If the logical address is greater than the contents of the base register it is authorized to
access, otherwise it is trap to operating system. The physical address (logical + base) is
less than (base + limit) it causes no problems, otherwise it is trap to the operating
system.
Base Base + Limit
OS
CPU
Logical
Yes
Physical
address address
No No
Trap to OS Memory
Fig. 8 Hardware address protection with base and limit registers
Review questions
1. List down different types of memory partitioning tecchniques.
2. What do you mean by swapping in regard to computer operating system?
3. Explain the difference between internal and external fragmentation?
4. How can it be overcome?
Source: Feleke Merin (Dr.-Engr.) Page 11 of 12
Unity University
5. Define term Overlay in term of computer operating system.
6. Explain the following memory allocation algorithms:
a) Best-Fit
b) First-Fit
c) Worst-Fit
7. Suppose a process requests 15K byte of memory and the memory manager currently has a list
of unallocated blocks of 10 KB, 20 KB, 16 KB, 7 KB, 25 KB, 35 KB, and 45 KB blocks (in- the-
order). Which memory space will be selected for 15 KB process using the following algorithms:
a) First fit
b) Best-Fit
8. What do you mean by memory compaction? Why compaction is not always possible?
-------------------------------- The End ! --------------------------------
Source: Feleke Merin (Dr.-Engr.) Page 12 of 12