Вы находитесь на странице: 1из 38

SIT222

Operating Systems
Session 04. Memory management
Outline
Introduction to memory management
Virtual memory and paging
Practical task
2 SIT222 Session 4
Introduction to memory management
The main memory (RAM) of a computer must be
carefully managed
Many processes compete for memory
Demand for memory often exceeds physical
memory available
Disk can be used to supplement memory,
but this introduces new problems
Processes expect a consistent view of memory
How to achieve this without conflicts?
3 SIT222 Session 4
Introduction to memory management
Historically, computers would load and run a single
program at a time
Processes would run until they terminate
Processes access physical memory directly
Can corrupt the operating systems memory easily
as a result
Can this be extended to multiple programs to run?
Yes, through swapping
Save out memory contents to disk
Load next programs memory contents from disk
4 SIT222 Session 4
Introduction to memory management
Swapping is very inefficient
Only one program is loaded at any one time
Obvious improvement is to load multiple programs into
memory at the same time!

The problem: programs are working directly with physical
memory, i.e., they load and store data at physical locations
in memory
One solution is static relocation
Modifies all memory references used by the program
Longer loading time as addresses must be updated
Processes can still access/modify each others
memories
5 SIT222 Session 4
Introduction to memory management
To properly solve the problem of memory
management, we need to implement a
memory abstraction
Processes have an abstract view of memory
(logical address space)
Every logical memory address is then mapped
to a physical address (physical address space)
i.e., processes do not work directly with
physical memory
Uses hardware support to improve efficiency
6 SIT222 Session 4
Introduction to memory management
Applying a memory abstraction also has other
benefits
Processes will always have the same view of
memory regardless of how much physical
memory is installed
Processes are not aware of the existence of
other processes and cannot see the memory of
other processes
Operating system can manage how much
physical memory each process can have
Without the process knowledge!
7 SIT222 Session 4
Introduction to memory management
The simplest memory abstraction uses two registers
Base register stores the location in memory where
a program is loaded
Base register is added to all memory references
(logical address + base = physical address)
Known as dynamic relocation
Limit register stores the amount of memory used
by that process
If a process references memory outside the
limit it is terminated
(logical address + base > limit == error)
8 SIT222 Session 4
Introduction to memory management
9 SIT222 Session 4
Introduction to memory management
10 SIT222 Session 4
Introduction to memory management
Having several processes in memory increases the
likelihood that the demand for memory will exceed
physical memory
We have already considered swapping as a
solution earlier
Swapping moves whole processes in/out of
memory
Idle processes can be swapped out to allow
room for ready processes
11 SIT222 Session 4
Introduction to memory management
Swapping processes leads to the need for an
algorithm to manage the free regions of memory
If there are several free regions, which one to
you allocate to a new/swapped in process?

To solve this problem, the location and size of free
memory regions are usually stored in a (linked) list
12 SIT222 Session 4
Introduction to memory management
Algorithms for allocating free memory regions:
First fit
Traverse the list until you find the first free
memory region whose size is big enough
Memory region is then broken into two units,
the first for the new/swapped in process, the
second is returned to the free list
13 SIT222 Session 4
Introduction to memory management
Algorithms for allocating free memory regions:
Next fit
Almost identical to first fit, but keep your
place in the list rather than searching from
the start of the list each time
14 SIT222 Session 4
Introduction to memory management
Algorithms for allocating free memory regions:
Best fit
Search the entire list for the smallest free
memory region that is big enough
Unfortunately, because best fit selects the
smallest regions, it leaves very tiny free
regions behind and tends to perform poorly
15 SIT222 Session 4
Introduction to memory management
Algorithms for allocating free memory regions:
Worst fit
The opposite of best fit, always select the
largest available free region
Unfortunately, when a large allocation is
requested, it is likely that there are no free
memory regions large enough
16 SIT222 Session 4
Introduction to memory management
Algorithms for allocating free memory regions:
Quick fit
Keeps several lists of free regions, each of
different sizes, e.g., 4K, 8K, etc.
Very fast to find a matching hole
17 SIT222 Session 4
Introduction to memory management
Algorithms for allocating free memory regions:
All of these algorithms suffer from the need to
search the list after every deallocation
Two adjacent free regions must be
recombined into a single free region
Can be done periodically and/or when a large
allocation request cannot be serviced.
18 SIT222 Session 4
Introduction to memory management
Because processes are variable sized,
fragmentation becomes a problem over time
After swapping out a process, a smaller process
is loaded in its place, leaving a gap
This is known as external fragmentation
The fragments of unused memory are located
outside of allocated memory regions
19 SIT222 Session 4
Introduction to memory management
Compaction is possible in some circumstances,
e.g., move process and adjust base and limit
registers
Compaction moves the processes in memory to
remove the gaps
This is an intensive operation because
effectively you are copying most/all memory
Unfortunately, processes also tend to grow over
time (dynamic memory allocation)
20 SIT222 Session 4
Virtual memory and paging
A better solution to the problem is to use
virtual memory
Processes access virtual address space
Virtual address space is mapped to the physical
address space by:
Hardware: The MMU translates virtual
memory addresses to physical addresses
Software: The operating system configures
and manages the MMU
21 SIT222 Session 4
Virtual memory and paging
The virtual address space is broken up into small,
fixed size chunks known as pages
Virtual address space is limited only by the size
of a memory reference
e.g., 32-bit platform = 2
32
addresses (4GB)

Do not need to use all pages
Do not need to have all pages used by a process
loaded in physical memory
i.e., processes can be partially loaded in
memory whereas under swapping whole
processes are loaded/unloaded
22 SIT222 Session 4
Virtual memory and paging
Virtual address space is mapped to the physical
address space by the MMU, which is similarly
divided into page frames
Page frames are generally the same size as the
pages
23 SIT222 Session 4
Virtual memory and paging
Page/page frame sizes are always 2
n
, e.g., 2KB,
4KB, 8KB, etc., as this simplifies mapping
Consider a memory address in binary, for a 4KB
page size:

Mapping only requires substituting the page
number for the frame number:
24 SIT222 Session 4
Virtual memory and paging
The record of what
page is stored in what
page frame is kept in
the page table for a
process
25 SIT222 Session 4
Virtual memory and paging
A typical page table entry




Referenced bit is set whenever the page has
been accessed (read or write)
Can reset and record periodically to tell
whether the page has been recently used
26 SIT222 Session 4
Virtual memory and paging
A typical page table entry




Modified bit (dirty bit) is set whenever the
page has been changed/written to
27 SIT222 Session 4
Virtual memory and paging
A typical page table entry




Protection has read/write/execute rights, e.g.,
Code region read + execute
Data region read + write
Heap region read + write
28 SIT222 Session 4
Virtual memory and paging
A typical page table entry




Present indicates whether that page is currently
loaded into a page frame or not
Where a page is not loaded a page fault
exception will result
29 SIT222 Session 4
Virtual memory and paging
Problem 1 of using page tables:
The page table itself is usually stored in memory
Every memory reference made by a program
takes two physical memory references:
One reference to read the page table
One reference to complete the request made
by the process
30 SIT222 Session 4
Virtual memory and paging
Solution:
Translation Lookaside Buffers (TLBs)
Also called associative memory
Very fast hardware cache (expensive!)
Usually only a small number of entries
Fortunately, most processes only access a
small number of pages at any one time
Referred to as the working set of pages
due to locality of reference (more later)

31 SIT222 Session 4
Virtual memory and paging
Problem 2 of using page tables:
The page table itself can be very large
Assume a page table entry is ~32 bits/4 bytes
4KB pages on a 32-bit platform will require
2
20
page table entries (1,048,576 entries)
Page table is approx. 4MB per process
4KB pages on a 64-bit platform will require
2
52
page table entries
(4,503,599,627,370,496 entries)
Page table is approx. 16 petabytes per
process!!! WOW!!!
32 SIT222 Session 4
Virtual memory and paging
Solution 1:
Multi-level page table
Still uses one page table entry per page
frame
Rather than one table containing all entries,
break the table into parts and index those
parts with another table
Does not require all parts to be present
Can have more than two levels of page
tables
33 SIT222 Session 4
Virtual memory and paging
Solution 1:
Multi-level page table
34 SIT222 Session 4
Virtual memory and paging
Solution 2:
Inverted page table
One page table entry per physical frame (not
per page!)
Result is a much smaller table
However, it is much harder to find the correct
entries in the page table
Page number can no longer be used to
index the table
Must perform a manual search instead
TLBs still help here
35 SIT222 Session 4
Virtual memory and paging
Solution 2:
Inverted page table








36 SIT222 Session 4
Practical task
Package management
Monitoring memory usage
Monitoring processes
Creating and managing swap space
37 SIT222 Session 4
Summary
Introduction to memory management
Virtual memory and paging
Practical task
38 SIT222 Session 4

Вам также может понравиться