You are on page 1of 67

CSC204

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.

b) Secondary memory - Secondary memory is a


non-volatile memory and it is slower and
cheaper than main memory.
8.1 Memory Hierarchy
Physical Organization
 Main memory available for a program plus its
data, may be insufficient.
 Therefore, programmer must use the mechanism
known as "overlaying“ that is keep in memory
only those instructions and data that are needed
at any given time.
 Program must be brought secondary memory
into main memory and placed within a process
for it to be run
8.1 Memory Hierarchy
Logical Organization
 The concept of logical address space that is
bound to a separate physical address space of
central to proper memory management.
a) Logical address = generated by the CPU; also
referred to as virtual address.

b) Physical address = address seen by the memory


unit.
8.1 Memory Hierarchy
Memory

Cache
Registers

What if we want to support programs that require more


memory than what’s available in the system ???
8.1 Memory Hierarchy
Answer: Pretend we had something bigger:
Virtual Memory
Virtual Memory
Memory

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.

✓ User processes then held in high memory


8.2 Physical Memory
Physical/Main Memory Allocation
a) Single-partition allocation
✓ One process in a memory at one time.
✓ Protection is done by having one relocation
(base) and one limit registers for a single
process in memory.
8.2 Physical Memory
Physical/Main Memory Allocation
b) Multiple-partition allocation
✓ Allows more than one user processes to be in
memory at one time.
✓ Hole = is a block of available memory space;
holes of various size are scattered throughout
memory.
✓ When a process arrives, it will be allocated to a
hole that is large enough to accommodate it.
✓ Partitioning : fixed versus dynamic
8.2 Physical Memory
Physical/Main Memory Allocation
b) Multiple-partition allocation
✓ Allows more than one user processes to be in
memory at one time.
✓ Hole = is a block of available memory space;
holes of various size are scattered throughout
memory.
✓ When a process arrives, it will be allocated to a
hole that is large enough to accommodate it.
✓ Partitioning : fixed versus dynamic
8.2 Physical Memory
Physical/Main Memory Allocation
Fixed Partitioning
 Partition available memory into regions with
fixed boundaries
 Fixed partitioning is divided by 2 types:
a) Equal-size partitions
b) Unequal-size partitions
8.2 Physical Memory
Physical/Main Memory Allocation
a) Equal-size partitions
▪ 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 (If program
size > partition size). Then, programmer must
design the program with overlays
8.2 Physical Memory
Physical/Main Memory Allocation
a) Equal-size partitions ( variable size partitions)
 But if main memory is used inefficiently, any
program, no matter how small, occupies an
entire partition.
▪ This may cause an Internal Fragmentation -
part of partition unused or wasted
▪ Unequal-size partitions are better in terms of
internal fragmentation

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

▪ Eventually each process is


blocked. The OS swaps out
process 2 to bring in process 4
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

▪ Eventually each process is


blocked. The OS swaps out
process 2 to bring in process 4
8.2 Physical Memory
Physical/Main Memory Allocation
Dynamic Partitioning
▪ Eventually each process is
blocked. The OS swaps
out process 1 to bring in
again process 2 and
another hole of 96K is
created.
▪ Compaction would
produce a single hole of
256K
8.2 Physical Memory
Physical/Main Memory Allocation
Dynamic Partitioning
 Possible allocation algorithms:
a) First Fit = Allocate the first hole that is big
enough.
b) Best Fit = Allocate the smallest hole that is big
enough.
c) Next-Fit = Allocate the first from the last
placement
d) Worst Fit = Allocate the largest hole in which it
will fit.
8.2 Physical Memory
Physical/Main Memory Allocation
a) First Fit
 Starts scanning from beginning and choose first
available block that is large enough.
 Fastest
 May have many process loaded in the front end
of memory that must be scanned
8.2 Physical Memory
Physical/Main Memory Allocation
b) Best Fit
 Allocate the smallest hole that is big enough;
must search entire list, unless ordered by size
 Main memory quickly forms holes too small to
hold any process: compaction generally needs
to be done more often
 This is a long process and often results in
extreme external fragmentation where very
small unusable regions of memory are left.
8.2 Physical Memory
Physical/Main Memory Allocation
c) Next Fit
▪ Scan memory from the location of the last
allocation and chooses the next available
block that is large enough
▪ More often allocate a block of memory at the
end of memory where the largest block is
found.
▪ Compaction is required to obtain a large block
at the end of memory
8.2 Physical Memory
Physical/Main Memory Allocation
d) Worst Fit
▪ Allocate the largest hole; must also search
entire list
▪ Produces the largest leftover hole
▪ First-fit and best-fit better than worst-fit in
terms of speed and storage utilization
8.2 Physical Memory
Physical/Main Memory Allocation

Example of memory configuration before and after allocation of 16 Kbyte Block


 External : when processes are loaded and
removed from memory, total memory space
exists to satisfy a request, but it is not
contiguous
 Internal : allocated memory may be slightly
larger than requested memory; this size
difference is memory internal to a partition, but
not being used
 Compaction : Shuffle memory contents to place
all free memory together in one large block

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) =

❑ Make sure the offset is less than the page size@frame


size
8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging

Address Translation Architecture


8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging
8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging : Example
❑ Given a physical memory size is 64 bytes,the
page size is 8 bytes and the page table as below :
Page no Frame no
0 1
1 4
2 3
3 7
8.2 Physical Memory
Physical/Main Memory Allocation
a) Paging : Example
Calculate :
i) The physical address for each logical address :
(2,3) and (1,0)
ii) The logical address for each physical address : 15
and 37
iii) The page number and offset for each logical
address : 13 and 18.
8.2 Physical Memory
Physical/Main Memory Allocation
b) Segmentation
❑ Allocation method that supports user’s
view of memory.
❑ Logical and physical address is divided into
variable length blocks.
8.2 Physical Memory
Physical/Main Memory Allocation
b) Segmentation
❑ A program is a collection of segments. A
segment is a logical unit such as:

User’s View of a Program


8.2 Physical Memory
Physical/Main Memory Allocation
b) Segmentation
1
4
1

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

Segmentation Hardware Example of Segmentation


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

Page Table When Some Pages Are Not in Main Memory


3.2.3 Virtual Memory
Handling Page Fault
❑ To handle page faults, the memory manager takes
the following steps:
1. Operating system looks at another table to decide.
2. Invalid reference, just not in memory  abort
(trap)
3. Check page is on backing store
4. Get empty frame, then swap page into frame
5. Reset tables
6. Set validation bit = v
7. Restart the instruction that caused the page fault

Steps in Handling a Page Fault


3.2.3 Virtual Memory
Handling Page Fault

Steps in Handling a Page Fault


3.2.3 Virtual Memory
Trashing
❑ Thrashing is a situation when a process is busy
swapping pages in and out (very high paging
activity).
❑ Swapping out a piece of a process just before
that piece is needed
❑ More time doing paging than executing.
3.2.3 Virtual Memory
Principle of Locality
❑ A locality is a set of pages that are actively used
together.
❑ As a process executes, it moves from locality to
locality.
❑ A program is generally composed of several different
localities which may overlap
❑ Only a few pages of a process will be needed over a
short period of time
❑ Possible to make intelligent guesses about which
pages will be needed in the future
 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 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

You might also like