Professional Documents
Culture Documents
Chapter 8:
MEMORY MANAGEMENT
2013 Mazlan Osman - UiTM Terengganu 1
8.1 Memory Hierarchy
8.2 Physical Memory
8.3 Virtual Memory
Memory Management
Memory management is a process which is
subdividing memory to accommodate
multiple processes. This process will be done
by memory manager.
Memory needs to be allocated efficiently to
pack as many processes into memory as
possible
If only a few processes can be kept in main
memory, then much of the time all processes
will be waiting for I/O and the CPU will be idle
.
Memory Management
A process may be broken up into pieces, which do
not need to be located contiguously in main
memory
It is not necessary for all pieces of a process to be
loaded in main memory during execution of the
process
Memory Manager Responsibilities:-
1) Allocating primary memory to processes and for
assisting the programmer in loading and storing
the contents of the primary memory.
2) Keep track of which processes are running in
which memory locations, and
3) Determine how to allocate and de-allocate
available memory when new processes are
created and when old processes complete
execution.
8.1 Memory Hierarchy
What is memory ??
Memory is a central operation of modern computer.
Memory refers to the physical devices used to store
programs (sequences of instructions) or data (e.g.
program state information) on a temporary or
permanent basis.
8.1 Memory Hierarchy
Physical Organization
Computer memory is organized into two levels:
a) Main memory - Main memory is a volatile
memory and it provides fast access at relatively
high cost.
Cache
Registers
Cache
Registers
8.2 Physical Memory
Physical/Main Memory Allocation
1) Contiguous Allocation
a) Single-Partition
b) Multiple-Partition
2) Non-contiguous Allocation
a) Paging
b) Segmentation
8.2 Physical Memory
Physical/Main Memory Allocation
1) Contiguous Allocation
Main memory usually divided into two
partitions:
✓ Resident operating system, usually held in
low memory with interrupt vector.
Equal-size partitions
8.2 Physical Memory
Physical/Main Memory Allocation
b) Unequal-size partitions
▪ Processes are assigned in such a way as to
minimize wasted memory within a partition.
▪ In a variable partitioning there are a number of
different partition sizes. It is intended that
processes are allocated to the smallest partition
it fits into.
▪ This attempts to minimize the amount of
internal fragmentation as most applications
are quite small, but also to allow large
applications to be run.
8.2 Physical Memory
Physical/Main Memory Allocation
b) Unequal-size partitions
▪ If the partition which best suited a process is
currently occupied there are a number of
strategies to handle such cases.
▪ The first is to multiple queue, a queue per
partition size, if a partition is currently
occupied processes queue to use the memory.
▪ The second strategy is to have a single queue
and place the process in the partition of best fit.
Unequal-size partitions
8.2 Physical Memory
Physical/Main Memory Allocation
Dynamic Partitioning
Partitions are of variable length and number
Process is allocated exactly as much memory as
required exactly the size it requires.
Termination of a process releases its memory
block.
8.2 Physical Memory
Physical/Main Memory Allocation
Dynamic Partitioning
▪ A hole of 64K is left after
loading 3 processes: not
enough room for another
process
November 19 32
None of the above processes solve the
external fragmentation issues but
next and first fit are considered
better algorithms as they do not
suffer from the linear search
overheads that the best and worst fit
algorithms do.
In practice next and first generally do
a better job at reducing external
fragmentation.
November 19 33
Given memory partitions of 100K,
500K, 200K, 300K, and 600K (in
order), how would each of the First-
fit, Best-fit, and Worst-fit
algorithms place processes of 212K,
417K, 112K, and 426K (in order)?
November 19 34
8.2 Physical Memory
Physical/Main Memory Allocation
Fragmentation Problem:
▪ Solution for external fragmentation :-
1) Compaction - Shuffle memory contents to
place all free memory together in one large
block.
2) Paging - To allocated process non-
contiguously
8.2 Physical Memory
Physical/Main Memory Allocation
2) Non-Contiguous Allocation
❑ Process is divided into parts, different parts
of the process should not be allocated
simultaneously.
❑ Process gets physical memory wherever
this memory is available.
❑ Is presented by :
a) paging
b) segmentation
8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging
❑ A memory management scheme that
permits the physical address space of a
process to be non-contiguous.
❑ Physical memory is broken into fixed-sized
blocks known as Frames.
❑ Logical memory is also broken into blocks
of the same size called Pages.
Divide physical memory into fixed-sized blocks called frames
(size is power of 2, between 512 bytes and 16 MB)
Divide logical memory into blocks of same size called pages
The frame size is equal to the page size
To run a program of size n pages, need to find n free frames
and load program
Requires page table to translate logical to physical
addresses. Page table contains base address of each page in
physical memory. Base address is combined with the page
offset to define the physical memory address that is sent to
the memory unit
November 19 38
8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging
❑ Requires Page Table to map (translate)
from logical to physical memory.
❑ O/S keeps track of all the free frames.
❑ A program of size n pages can use any of n
frames.
❑ The frame size is equal to the page size
Page size 4 bytes
Memory size 32 bytes (8 pages)
November 19 40
November 19 41
8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging
❑ Address generated by CPU is divided into
i) Page number (p)
✓ Used as an index into a page table which
contains base address of each page in
physical memory
i) Page Offset (d)
✓ Combined with base address to define the
physical memory address that is sent to
the memory unit.
8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging
❑ Formula:
✓ Physical address = (Frame number *
Frame size) + offset
✓ Number of pages = Program size/Page
size
✓ Number of frames = memory size/frame
size
8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging
❑ Formula:
✓ (p, d) =
✓ (f, d) =
3 2
4
3
user space
(Logical address space) physical memory space
8.2 Physical Memory
Physical/Main Memory Allocation
b) Segmentation
❑ Logical address consists of :
✓ <segment-number, offset>
❑ Requires a Segment Table to map the logical
address space to physical memory, consists of :-
✓ Base = starting physical address where the
segment resides in memory.
✓ Limit = specifies the length of the segment.
8.2 Physical Memory
Physical/Main Memory Allocation
b) Segmentation
❑ Segmentation causes External Fragmentation.
❑ Physical Address = Segment Base + Offset.
❑ Offset must be less than limit value in the segment
table.
❑ Allows sharing of common codes.
❑ Protection is done using the protection bit
8.2 Physical Memory
Physical/Main Memory Allocation
b) Segmentation
Example of Segmentation
8.3 Virtual Memory
What is Virtual Memory ???
❑ The various memory-management strategies
discussed have one common goal: to keep many
processes in memory simultaneously to allow
multiprogramming.
❑ However, they tend to require that an entire
process be in memory before it can execute.
❑ VIRTUAL MEMORY is a technique that allows
the execution of processes that are not
completely in memory.
8.3 Virtual Memory
What is Virtual Memory ???
❑ Advantages :-
✓ Program size can be larger than physical
memory size.
✓ Programmers need not to worry about the
amount of physical memory available.
✓ More programs can be run at the same time.
❑ Virtual memory can be implemented via :
✓ Demand paging
✓ Demand segmentation
3.2.3 Virtual Memory
Page Fault
❑ Pages of processes are resided on secondary
storage, when it is needed for execution, a page
will be swapped into memory.
❑ Page is needed reference to it
✓ invalid reference abort
✓ not-in-memory bring to memory
❑ Requires Valid-Invalid bit to distinguish between
pages that are in memory and pages that are on
the disk.
3.2.3 Virtual Memory
Page Fault
❑ When it is needed for execution, a page will be
swapped into memory.
❑ Requires Valid-Invalid bit to distinguish between
pages that are in memory and pages that are on
the disk.
✓ Valid (V @ bit 1) = page is in the memory.
✓ Invalid (I @ bit 0) = page is not valid OR page is valid but
not in memory and store on disk.
❑ During address translation, if valid–invalid bit in
page table entry is 0 @ i page fault = a trap to
OS indicating the page is not in memory.
3.2.3 Virtual Memory
Page Fault
November 19 65
First-fit:
❖ 212K is put in 500K partition
❖ 417K is put in 600K partition
❖112K is put in 288K partition (new
partition 288K = 500K - 212K)
❖426K must wait
November 19 66
Best-fit:
▪ 212K is put in 300K partition
▪ 417K is put in 500K partition
▪ 112K is put in 200K partition
▪ 426K is put in 600K partition
Worst-fit:
▪ 212K is put in 600K partition
▪ 417K is put in 500K partition
▪ 112K is put in 388K partition
▪ 426K must wait
November 19 67