Upload
sheena-lane
View
218
Download
1
Embed Size (px)
Citation preview
1
Memory Management
Requirements of memory management system
• to provide the memory space to enable several processes to
• execute concurrently
• to provide a satisfactory level of performance for users
• to protect processes from each other
• to enable sharing of memory space between processes
• to make addressing transparent
2
Basics• Source code into the object code via compilation or assembly.
• Software systems of any size are usually made up of several modules which are joined together into one complex system using a linker.
• At the time the source code is translated into an object module, it contains external references. The compiler will generate a symbol table which is used to resolve these references during linking. The resulting module is known as the load module.
• At this point all addresses generated are logical addresses as the compiler and linker have no idea where the code will be placed in memory. These must be converted into physical addresses before they can be loaded and run. If these external references are resolved before the execution of the program, the linking is referred to as static, whereas if they are made at execution time they are known as dynamic.
• Finally load module from secondary storage into main memory.
• The logical to physical address translation is known as binding. It can occur at programming time, compile time or link time in an embedded system or simple computer system. All three strategies are static binding techniques and the resultant code is not relocatable.
• When binding occurs at runtime it is known as dynamic binding all memory references are logical but must be translated into physical addresses before being presented to the memory. The logical to physical transformation is accomplished using an address map.
3
Relocatable Code
• If virtual address space is organised as below, then the basic requirement for modules is to be held contiguously in physical memory and to contain addresses relative to their first location. The physical address of this first location is called the base of the process.
Physical memory is much smaller than Virtual memory.
The user program is shown starting at an arbitrary address and sharing memory with other programs
Virtual Memory
Physical Memory
The simplest form of dynamic relocation hardware is a single base register and a memory management unit (MMU) to perform the translation
4
UNUSED
USERPROCESSES
OPERATINGSYSTEM
Single Process Allocation
Memory Management
Fixed Partition Allocation
PROCESS A
PROCESS C
PROCESS B
OPERATING SYSTEM
Process C = 300k
Process B = 200k
Process A = 150k
Partition3 400k
Partition 2 300k
Partition 1 200k
5
Fixed Partitions
• Fixed size partitions to allow for large and small processes
• Wasted space leads to internal fragmentation
• Where internal infers space allocated for a process
• System must manage memory protection
Disadvantages
• process can’t run because no partition of suitable size available
• internal fragmentation wastes space
6
Variable Partitions
AVAILABLE SPACE
PROCESS C
PROCESS B
PROCESS A
OPERATING SYSTEM
1000k
750k
450k
250k
100k
0k
PROCESS C
PROCESS B
PROCESS A
OPERATING SYSTEM
1000k
750k
450k
250k
100k
0k
NewPROCESS D
225k
975k
Initially processes are loaded until memory full or no spaces large enough
7
FREE
PROCESS C
FREE
PROCESS A
OPERATING SYSTEM
1000k
750k
450k
250k
100k
0k
PROCESS Cterminatesleaving threeholes to coalesce
PROCESS A
OPERATING SYSTEM
1000k
750k
450k
250k
100k
0k
Variable Partitions
(a) (b)
In (a) not enough space for new process although total space o.kWasted space is called external fragmentation where:
external = space without allocation
8
Storage placement policies
• Memory allocation is the process of assigning blocks of memory on request – performed by the allocator receives memory from the operating system in a small number of large blocks that it must divide up to satisfy the requests for smaller blocks. It must also make any returned blocks available for reuse.
– Best fit policy
– First fit policy
– Worst fit policy
– Buddy System
• Best fit is intuitively appealing, since it would appear that minimum space is wasted and is simple. However most programs grow and shrink dynamically. There are two reasons for this:
• The stack – it’s size increases with the depth of function calls
• The use of dynamically acquired memory using the malloc command (in ‘C’), which permits it to acquire extra memory in a structure called the heap.
9
Storage placement policies
• Worst fit doesn’t look too promising. However, worst fit could possibly leave a hole large enough to accommodate another process, while best fit, in trying to minimise the unused space could create a hole too small to be useful.
• First fit, appears to be too indiscriminate but there is very little overhead in implementing it. In the first fit algorithm, the allocator keeps a list of free blocks (known as the free list) and, on receiving a request for memory, scans along the list for the first block that is large enough to satisfy the request.
• The first fit algorithm performs reasonably well, as it ensures that allocations are quick.
10
NEWPROCESS
150K
100k
300k
200k
400k
First fit, Hole residue = 150k
Best fit, Hole residue = 50k
Worst fit, Hole residue = 250k
AVAILABLE HOLESIZES
(in memory order)
ALLOCATIONSELECTION
Memory Management
Best fit – hole into which process fits most tightly
First fit – first available hole
Worst fit – leaves maximum amount of free space
11
Buddy System• In a buddy system, the allocator will only allocate blocks of
certain sizes which are usually either powers of two, or form a Fibonacci sequence. Blocks can be divided into two smaller blocks.
• When the allocator receives a request for memory, it rounds the requested size up to a permitted size. If the free list for that size is empty, the allocator splits a block from a larger size.
• When blocks are recycled, there may be some attempt to merge adjacent blocks into ones of a larger permitted size (coalescence).
A binary buddy heap before allocation
A binary buddy heap after allocating a 8 kB block
A binary buddy heap after allocating a 10 kB block; note the 6 kB wasted because of rounding up
12
Memory Management
FREE
PROCESS C
PROCESS A
OPERATING SYSTEM
Need to suspend all process while compaction takes place
Major overhead – particularly in time critical systems
When to perform compaction
• when process terminates
• when process cannot load due to fragmentation
• at fixed intervals
• when user decides to
Compaction of free space
13
Managing Memory• If the memory requirements of the system exceed the
available main memory there are at least three ways of solving the problem:
– Overlays
– Swapping
– Paging
• Overlays are sections of an application's code or data that are stored on secondary memory when not in use.
• Swapping - if memory is needed, a ready process is selected to be swapped out. Swapping a process out of main memory involves copying the data segments of that process to a disk. When a process is selected to be run, if that process is swapped out, the system must first find or make space in main memory, then swap that process in, before running it.
14
ED
CB
A
Overlays – function call hierarchy
k2
k1
A
C
E
A
C
D
A
B
Overlays – address assignments
Since B and D do not call each other, they need not reside in memory at the same time. The same is true of functions D and E; furthermore their presence is needed only when C is resident.
15
Simple Paging
PAGE 1 2 3456
PAGE 1 2 34
PAGE 1 2 345
Process A
Process B
Process C
A1A2A3A4A5A6B1B2B3B4B5C1C2C3C4---
Memory space
Paging System Allocation
16
Simple Paging
A1A2A3A4A5A6-----
C1C2C3C4---
A1A2A3A4A5A6D1D2D3E1E2C1C2C3C4E3E4-
Newprocess
D
Newprocess
E
A1A2A3A4A5A6F1F2F3H1H2G1G2H3H4--
F4
(a) (b) (c)
Updated Paging System
17
Simple Paging
0101 010110011011 1110 010110011011
0 01111 10012 00013 10114 00005 11106 0110
Memoryaddress
Convertedmemory address
p
p d
Page Address Translation
19
Memory Leakage• A memory leak is a particular kind of unintentional
memory consumption by a computer program where the program fails to release memory when no longer needed.
• The program in this case is part of some very simple software designed to control a lift This part of the program is run whenever anyone inside the lift presses the button for a floor.
When a button is pressed:
Get some memory, which will be used to remember the
floor number
Put the floor number into the memory
Are we already on the target floor?
{
If so, we have nothing to do:
finished
}
Otherwise
{
Wait until the lift is idle
Go to the required floor
Release the memory we used to remember the
floor number
}
20
#include <stdio.h>#include <stdlib.h>
void f(void){
void* s; s = malloc(50); /* get memory */ return; /* memory leak */
main(){
while (1) f(); /* This function call will fail
to malloc sooner or later */}
Memory was available and pointed to by s, but not saved. After this function returns, the pointer is destroyed and the allocated memory becomes unreachable. To "fix" this code, either the f() function itself needs to add "free(s)" somewhere.
22
Correspond to logical divisions in a process i.e. code segments/ modules
FREE
A4
A3
B2
A2
A1
B3
B1
OPERATING SYSTEM
SEGMENT 4
SEGMENT 3
SEGMENT 2
SEGMENT 1
SEGMENT 3
SEGMENT 2
SEGMENT 1
Process Structures Memory allocation
Process A
Process B
Segmentation
23
Memory Management
0011 000101111010 0101001001111011
Logical address Physical address
+
LENGTH BASE ADDRESS
0
1
2
3 010110110111 010100010000001
4
s
displacement d
0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 1 1 1 1 0 1 0 0 1 0 1 0 0 1 0 0 1 1 1 1 0 1 1