Title: Memory Management
1Memory Management
2- Memory Management is the managing of the memory
resources of a computing system for the most
effective use by its application programs - The problem of memory management is composed of
three sub-problems
3Allocation
- This is the problem of assigning suitable storage
space to processes as required. - An efficient memory allocation mechanism may be
expected to respond quickly to - requests for assorted amounts of memory
- to assign memory only when needed
- To reclaim memory promptly when its use is
complete
4Relocation
- This is the problem of matching programs and data
to the memory locations that have been allocated
to them. - Sometimes modifications must be made to addresses
in machine instructions and data to reflect the
addresses actually used.
5Protection
- This is the problem of restricting a processs
access to memory that has actually been allocated
to it.
6In early systems
- The evolution of memory management techniques has
been shaped largely by the resources early
characteristics. - Memory was expensive and slow
- Only very limited amounts were available
- Multiprogramming evolved partly because of the
need to share such an expensive resource among
several concurrent processes.
7Memory allocation schemes
- Memory allocation refers to the action of
selecting and reserving particular parts of
memory for use by particular processes. - Usually memory is allocated in discrete blocks or
regions that must be contiguous (they must occupy
consecutive addresses in the logical or virtual
address space of the process.) - Early operating systems allocated space in
physical memory with little or no hardware
assistance.
8No allocation
A bare machine with no memory allocation
9No Allocation
- Simplest strategy is none at all.
- Possible only on systems that run a single
program at a time - Each program has total access to ALL portions of
memory and can manage and use it in any manner. - Simple approach with costs nothing to implement
(it entails no OS services) - The program can use all memory previously
occupied by the OS. Its the programs
responsibility to reload the OS when it completes.
10Single-user Operating Systems
- The simplest type of OS serves only a single user
and runs only one process at a time. - Memory needs only to be shared between the single
process and the OS itself - A program may use whatever memory it needs as
long as the required memory exists and is not
needed by the OS.
11Single user OS
12- Memory in such systems is typically viewed as
being divided into two regions one for the OS
and a transient area for the application program - The placement of the resident monitor in memory
can be in either high or low memory addresses - Some systems use low memory for the resident
monitor because this region contains locations
with special built-in properties such as
interrupt vectors. - Sometimes, even though it controls special
hardware locations in low memory, the OS itself
may be located in high memory.
13Protecting the Nucleus
- Allocation is only meaningful when it is
accompanied by protection. - Some mechanism should exist to prevent a
processs access to unallocated locations. - Usually such protection is not possible without
hardware mechanisms, not usually found on the
simplest microcomputers or microprocessors. - The lack of protection is typically viewed as not
serious since it is a single user machine, and
disruption of OS services would affect a single
user.
14Allocation for multiprogramming
- Multiprogramming operating systems require that
memory be shared among two or more processes. - A portion of memory must be chosen for allocation
to each process. - Memory allocated to process should be protected
from access by another process
15Static .vs. Dynamic Allocation
- With static allocation a process is assigned all
the memory it is expected to require when it is
loaded and started. - It can not be loaded until sufficient memory is
available - The process keeps all its memory throughout its
lifetime, and cannot obtain more - Simple to manage but does not allow effective
sharing of memory in a multiprogramming system.
16- Dynamic allocation offers more efficient memory
use. - Each process is permitted to request addition
memory as needed during execution and is expected
to release memory it no longer needs. - In this strategy, the OS is presented with an
unpredictable sequence of allocate and free
requests to assign and release individual blocks
of memory - Blocks may vary in size
- Each request must be filled by a single
contiguous region made available in the address
space of the requesting process.
17Defining memory available for allocation
- Only a fixed amount of physical memory is
available for allocation to processes - Two strategies for defining how many and what
size memory blocks to assign to processes being
loaded
18- Static memory area definition the of blocks
and the size of each block is defined when the OS
is loaded (or generated). The of blocks
available for loading processes does not change
once the OS loads - Blocks defined and allocated via this strategy
are called fixed partitions (partitions) - Dynamic memory area definition OS determines
the and size of blocks upon servicing a request
for memory allocation for loading a process. The
current state of memory (both allocated and free)
determines whether a block can be defined and
allocated and where it is located. Free memory
is viewed as a pool from which blocks can be
allocated. - Blocks defined and allocated via dynamic memory
area definition are called regions.
19Static allocation using either static or
dynamically memory area definition
20- Assuming a multiprogramming operating system uses
static allocation and either static or dynamic
memory area definition, several possibilities
exist for loading programs into memory - Several processes can share memory, each being
allocated the same amount of space using
statically-defined fixed partitions. There is a
fixed maximum number of processes. - Several processes can share memory, each being
allocated different amounts of space using
statically-defined fixed partitions. Again, there
is a fixed maximum number of processes. - Several processes can share memory, each being
allocated different amounts of space using a
variable number of variable-size regions. Here
the number of processes may have no fixed
maximum.
21Using Statically defined partitions
- A fixed memory partition strategy divides all
available space into a fixed number of
partitions. - The partitions may or may not be all the same
size ( of bytes) - Each has a fixed size established when the OS is
first loaded and initialized. - Common strategy in real-time systems
22(No Transcript)
23- In this example memory is partitioned into a
fixed partition system. - Four partitions allow up to four processes to
execute concurrently. - Simplifies memory management, because we only
need to identify if a partition is free or
allocated. Usually accomplished through the use
of control blocks which defines the size of an
individual partition, and whether or not it is
free. - Memory allocated to a process which is not
needed by that process is wasted - If no programs can fit into a free partitions
they are not used, and processes must wait.
24- Disadvantages
- of process that can run is fixed
- Unused memory at the end of each partition is
wasted if the process does not need the entire
partition
25Dynamically Defined Regions
- Allows the dynamic definition of a variable
number of variable size memory regions. - The size of each region is determined when the
region is allocated by the OS - The number of concurrent processes becomes
variable, depending on the amount of memory
available when the allocation request occurs.
26Figure 7-5 A Variable Partition Environment
27- Control blocks are dynamically created to keep
track of which regions have been allocated to
processes and which areas of memory are free. - This method has been used by several popular OSs
including OS/360 (MVT), some versions of Windows
and Macintosh OS.
28- This is a cross between static and dynamic
methods - From the standpoint of the process
- allocation is static. (only one region will be
allocated) - The space assigned to each process is variable
and determined when the process starts, but
doesnt change. - From the OSs standpoint
- Allocation is dynamic
- It deals with many processes of different sizes
that must start and stop at unpredictable times. - Memory can become fragmented as processes are
allocated regions, and then free them when the
processes terminate.
29Figure 7-6 Fragmentation in Variable Space
Allocation
30- Fragmentation occurs when memory becomes divided
into many small separate sections that are not
large enough to meet the needs of waiting
processes. - We need to implement methods to reorganize
processes to reclaim free partitions such as
swapping or compaction
31Dynamic allocation
- Processes are allocated memory as requested.
- Principal operations are allocate free
- Operations are invoked via a system call at the
program interface. - Heap management
- Memory is viewed as a collection of blocks of
random sizes that will be allocated and freed in
unpredictable order.
32Allocate
- An allocate call requests a specific amount of
memory from the OS. - There is no requirement that the memory be in a
specific location within memory - But it must be contiguous
- The OS must allocate the memory and make it
available in the address space of the process. - If no suitable region is available the process
must wait
33Free
- The free operation is used by a process to
de-allocate regions it no longer needs. - Each de-allocated block must be freed in its
entirety. - The OS needs to reclaim freed blocks and make
them available to later allocation requests - Any remaining un-freed space belonging to a
process is freed when the process terminates.
34Memory Control Blocks
- Blocks of memory whether allocated or free are
described by memory control blocks (MCBs). - Free memory control blocks represent free
blocks of memory (FMCBs) - Allocated memory control blocks represent
allocated blocks of memory. (AMCBs) - Free blocks are maintained on a linked list of
FMCBs ordered by decreasing size, and/or
increasing memory address
35- FMCBs and AMCBs may be stored in a system
control block area that is separate and protected - Alternately, they may be a part of the
corresponding storage block, conveniently placed
as a header at the beginning of the block
36Figure 7-7 Memory Control Blocks
37Boundary Tag Method
- Devised by Knuth in 1973
- Two MCBs are placed in each region
- At the end of each region is a simplified MCB
containing the size of the block and an
indication of whether or not the block is
allocated - At the beginning of each block is a complete MCB
containing the size of the block and all of the
appropriate pointers to the next and previous
FMCBs or AMCBs - This method simplifies the joining of adjacent
free blocks. - As blocks are freed, it is simple to check the
boundary tag of adjacent blocks. IF they are
free then the blocks can be combined into a
single larger block.
38Bit Maps
- Useful when memory is partitioned into blocks of
equal fixed size. - Each block is represented by a bit, and the value
of the bit indicates whether the block is free or
allocated (1 allocated, 0 free) - Allocation is done in multiples of these fixed
size blocks - The address and size of the memory blocks
allocated must be stored in or linked to the PCB - When a process terminates, the appropriate bits
in the bit map must be set back to zero to
indicate that the memory blocks are now free.
39Figure 7-8 Memory Management Using Bit Maps
40Allocation Strategies for supporting Dynamic
Allocation of a Varying number of variable sized
blocks
41(No Transcript)
42First Fit
- This method examines each FMCB starting from the
beginning of the list of FMCBs and selects the
first one large enough to fulfill the request. - The selected block is then divided into two parts
- A portion large enough to fulfill the request
- Remainder is returned to the FMCB (a new smaller
block is created) - Method is simple and fast
- May lead to a rapid breakdown of large blocks,
because a small request may be filled from a
large block. - Tendency for small blocks to collect at the front
of the free list.
43Next Fit
44- A roving pointer is maintained which points to
the location where the last search ended. - New searches begin at this location
- The list is treated as a circular list, so that
the pointer in the last FMCB is linked to the
beginning of the list - If the pointer returns to the place where it
began, then request can not be fulfilled - Eliminates some of the problems with the
first-fit strategy, because small blocks do not
accumulate at the beginning of the list. - Allocation is more evenly distributed throughout
the list and memory is used more evenly.
45Best Fit
46- Neither of the previous strategies consider the
size of the block selected. - The best fit strategy examines ALL free blocks
and chooses the one whose size most closely
matches the request. - Requires a search for the one best fitting block
(more efficient if FMCBs are linked together in
order of size!) - Wastes the smallest amount of memory
- But leftover portions are usually too small to be
useful.
47Worst-Fit
48- Selects the block that yields the largest
remainder after it is divided. - It allocates from the largest block available.
- FMCBs should be linked in descending order by
size - Yields leftovers that are likely to be useful in
future allocation requests. - However since largest blocks are always consumed
first, they are unlikely to be available if a
large request is submitted.
49Choosing a strategy
- Each of the previous strategies have advantages
and disadvantages - One measure is the amount of time it takes to
block how long before the system is unable to
process and allocation request because of
insufficient memory. - Fragmentation and eventual blockage is inevitable
with all of the algorithms - When blockage occurs it must be resolved via some
technique such as swapping or compaction.
50How do we divide blocks efficiently?
- As previously mentioned, free blocks are divided
when allocated to a process. - A part big enough to fulfill the request is
associated with an AMCB and provided to the
process. - The remainder is returned to the system
associated with an FMCB. - An important issues involves how to divide blocks
effectively so that the remainder is useful to
the system in fulfilling future requests?
51- If the remainder block is VERY small then it will
be useless as an independent free block. - To solve this situation, the memory manager may
choose NOT to divide blocks if the remainder is
less than some preselected minimum. - When a free is performed the entire block
rejoins the free list intact! - This strategy works well with a best-fit
allocation method
52- An alternative strategy is the buddy system
- In this strategy a block must be used intact or
divided exactly in half - If a block is divided the 2 halves are considered
buddies. - A freed block can ONLY be combined with its buddy
to form a larger block.
53Recovering Blocks
- The heap manager relies on the free operations
to return storage blocks when they are no longer
needed. - These blocks must be returned to the free list in
a way that makes them useful to future requests.
- The smaller a block is the less likely it is to
be useful in fulfilling future allocations.
54- Freeing memory should involve 2 steps
- Moving the blocks MCB from the allocated list to
the free list. - Checking to determine if adjacent blocks are
free, and if so combining them into a single
larger block represented by a single MCB - If the linked list of FMCBs are ordered by
increasing memory address, then checking adjacent
blocks is simple
55- The boundary tag method also simplifies
determining if adjacent blocks are free. - Also if FCMBs are maintained as a linked list it
is also simple to unlink blocks, reform them
into a single block, and then relink the new
block back into the list!
56Relocation Involves Preparing a program to run
in the location into which it is loaded
57- Addresses in source programs are generally
symbolic, such as the name of a variable. - A compiler will typically bind these addresses to
relocatable addresses, such as 14 bytes from the
beginning of this module. - The linkage editor or loader will in turn bind
these relocatable addresses to absolute addresses
within the memory space of the process.
58- Compile time If it is know at compile time
where the process will reside, then Absolute
code can be generated. - Load time If it is not know, at compile time,
where the program will be loaded, then the
compiler must generate relocatable code. Final
binding is delayed until the program is loaded. - Execution time If the process can be moved from
one memory segment to another, during its
execution then binding must be delayed until run
time.
59- If relocation is performed when the program is
loaded or before it is called STATIC RELOCATION - If relocation is performed after the program has
been loaded and execution has begun, when we have
dynamic relocation - Relocation can be performed either by hardware or
software means
60Static Relocation via software
Figure 7-12 Program Relocation by a Relocating
Loader
61Relocation Via Software
- Requires that you can determine which references
should be changed and in what way. - Object file may include a relocation dictionary
which identifies all instructions and addresses
that may need to be modified. - Object file is converted to an executable file by
either a relocating linker or processed when
loaded by a relocating loader
62- The relocating software uses the dictionary
information to decide which memory references to
adjust and how when the actual memory location is
known. - Requires that additional information be
maintained in object files - Relocation Information isnt loaded into memory,
so no further relocation is possible. - This strategy provides only static not dynamic
relocation!
63Dynamic RelocationBased Addressing
- A base register contains the first (or base)
address for the program - Relocation is achieved by loading this base
register with the actual starting address of the
program. - Address references in machine instructions are
adjusted to create effective addresses by
adding the contents of the specified base
register to the value in the address field
(displacement). - Base addressing requires that each instruction
specify the correct base register for use in
calculating effective addresses. - A program can be relocated by changing this base
register to point to a new location. - This method is no help in relocating address
constants such as pointers.
64Relative Addressing
- Variation of based addressing used by the PDP-11
and VAX - Uses a program counter as the base register.
- Displacements contained in instructions which
specify relative addressing are displacements
from the address of the instruction itself.
65Relocation Registers
- Involves a relocation register that is always
added to every instruction address and data
address before the instruction or data is
fetched. - Used uniformly for all memory references and
isnt specified with each instruction. - Simplifies relocation
- The relocation register contents must be saved
and restored via the PCB during a context switch
66(No Transcript)
67Protection
- The sub-problem of protection, involves allowing
a process to accesses only that memory which has
been allocated to it. - There are several possible strategies, but all
rely upon hardware assistance.
68Fences simple single user OS
69Bounds Registers
70Base and Limit Registers
71Protection Keys
72Swapping
- Swapping involves moving portions of programs and
their data from main memory to secondary storage
before they complete execution. - Processes which are swapped to disk are expected
to be restored at some point so that they can
finish execution
73Swapping
Program 1
Program1
3000
74- We can remove a process that has been suspended
by the medium term scheduler allowing another
process to execute - It can be used to increase free memory if an
allocation has been blocked. - We can temporarily remove a process that has been
blocked awaiting a long term event, such as
waiting for resources. - We can use swapping to prepare a process for
relocation to a new location, such as during
compaction or when its size is to be increased
75Compaction