Upload
sat0912
View
217
Download
0
Embed Size (px)
Citation preview
8/12/2019 04. Memory Management
1/38
SIT222Operating SystemsSession 04. Memory management
8/12/2019 04. Memory Management
2/38
Outline
Introduction to memory management
Virtual memory and paging Practical task
2SIT222 Session 4
8/12/2019 04. Memory Management
3/38
Introduction to memory management
The main memory (RAM) of a computer must becarefully 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?
3SIT222 Session 4
8/12/2019 04. Memory Management
4/38
Introduction to memory management
Historically, computers would load and run a singleprogram at a time Processes would run until they terminate Processes access physical memory directly
Can corrupt the operating systems memory easilyas 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
4SIT222 Session 4
8/12/2019 04. Memory Management
5/38
Introduction to memory management
Swapping is very inefficient Only one program is loaded at any one time
Obvious improvement is to load multiple programs intomemory at the same time!
The problem: programs are working directly with physicalmemory, i.e., they load and store data at physical locationsin 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
5SIT222 Session 4
8/12/2019 04. Memory Management
6/38
Introduction to memory management
To properly solve the problem of memorymanagement, we need to implement amemory 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
6SIT222 Session 4
8/12/2019 04. Memory Management
7/38
8/12/2019 04. Memory Management
8/38
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 thelimit it is terminated(logical address + base > limit == error)
8SIT222 Session 4
8/12/2019 04. Memory Management
9/38
Introduction to memory management
9SIT222 Session 4
8/12/2019 04. Memory Management
10/38
Introduction to memory management
10SIT222 Session 4
8/12/2019 04. Memory Management
11/38
Introduction to memory management
Having several processes in memory increases thelikelihood that the demand for memory will exceedphysical 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
11SIT222 Session 4
8/12/2019 04. Memory Management
12/38
Introduction to memory management
Swapping processes leads to the need for analgorithm 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
12SIT222 Session 4
8/12/2019 04. Memory Management
13/38
Introduction to memory management
Algorithms for allocating free memory regions: First fit
Traverse the list until you find the first freememory 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
13SIT222 Session 4
8/12/2019 04. Memory Management
14/38
Introduction to memory management
Algorithms for allocating free memory regions: Next fit
Almost identical to first fit, but keep yourplace in the list rather than searching fromthe start of the list each time
14SIT222 Session 4
8/12/2019 04. Memory Management
15/38
Introduction to memory management
Algorithms for allocating free memory regions: Best fit
Search the entire list for the smallest freememory region that is big enough
Unfortunately, because best fit selects thesmallest regions, it leaves very tiny free
regions behind and tends to perform poorly
15SIT222 Session 4
8/12/2019 04. Memory Management
16/38
8/12/2019 04. Memory Management
17/38
Introduction to memory management
Algorithms for allocating free memory regions: Quick fit
Keeps several lists of free regions, each ofdifferent sizes, e.g., 4K, 8K, etc.
Very fast to find a matching hole
17SIT222 Session 4
8/12/2019 04. Memory Management
18/38
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.
18SIT222 Session 4
8/12/2019 04. Memory Management
19/38
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
19SIT222 Session 4
8/12/2019 04. Memory Management
20/38
Introduction to memory management
Compaction is possible in some circumstances,e.g., move process and adjust base and limitregisters 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)
20SIT222 Session 4
8/12/2019 04. Memory Management
21/38
Virtual memory and paging
A better solution to the problem is to usevirtual 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 configuresand manages the MMU
21SIT222 Session 4
8/12/2019 04. Memory Management
22/38
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 wholeprocesses are loaded/unloaded
22SIT222 Session 4
8/12/2019 04. Memory Management
23/38
Virtual memory and paging
Virtual address space is mapped to the physicaladdress space by the MMU, which is similarlydivided into page frames Page frames are generally the same size as the
pages
23SIT222 Session 4
8/12/2019 04. Memory Management
24/38
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:
24SIT222 Session 4
8/12/2019 04. Memory Management
25/38
Virtual memory and paging
The record of whatpage is stored in whatpage frame is kept inthe page table for aprocess
25SIT222 Session 4
8/12/2019 04. Memory Management
26/38
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
26SIT222 Session 4
8/12/2019 04. Memory Management
27/38
Virtual memory and paging
A typical page table entry
Modified bit (dirty bit) is set whenever the
page has been changed/written to
27SIT222 Session 4
8/12/2019 04. Memory Management
28/38
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
28SIT222 Session 4
8/12/2019 04. Memory Management
29/38
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
29SIT222 Session 4
8/12/2019 04. Memory Management
30/38
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
30SIT222 Session 4
8/12/2019 04. Memory Management
31/38
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)
31SIT222 Session 4
8/12/2019 04. Memory Management
32/38
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!!!
32SIT222 Session 4
8/12/2019 04. Memory Management
33/38
Virtual memory and paging
Solution 1: Multi-level page table
Still uses one page table entry per pageframe
Rather than one table containing all entries,break the table into parts and index thoseparts with another table
Does not require all parts to be present Can have more than two levels of page
tables
33SIT222 Session 4
8/12/2019 04. Memory Management
34/38
Virtual memory and paging
Solution 1: Multi-level page table
34SIT222 Session 4
8/12/2019 04. Memory Management
35/38
Virtual memory and paging
Solution 2: Inverted page table
One page table entry per physical frame (notper 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 toindex the table Must perform a manual search instead
TLBs still help here
35SIT222 Session 4
8/12/2019 04. Memory Management
36/38
Virtual memory and paging
Solution 2: Inverted page table
36SIT222 Session 4
8/12/2019 04. Memory Management
37/38
Practical task
Package management Monitoring memory usage Monitoring processes Creating and managing swap space
37SIT222 Session 4
8/12/2019 04. Memory Management
38/38
Summary
Introduction to memory management Virtual memory and paging Practical task
38SIT222 S i 4