Memory Management - UCLA Computer Science

streambabySoftware and s/w Development

Dec 14, 2013 (3 years and 6 months ago)

69 views

memory management
3/5/03
-
1
Overview of Presentation
?
responsibilities of a memory manager
?
user view of memory management
?
memory management strategies, algorithms

fixed partitions, variable partitions

algorithms, fragmentation, coalescing
?
swapping and the relocation problem
?
material adequately covered by the text

dynamic address relocation, swapping
memory management
3/5/03
-
2
Memory Management Duties
?
allocate/assign memory to processes

explicit requests: malloc

implicit: program loading, stack extension
?
map in and out of process address space

when allocated, with each context switch
?
manage migration to/from 2
nd
ary storage

optimize use of main storage

minimize overhead (waste, migrations)
memory management
3/5/03
-
3
User View: address space
Code segment
Data segment
Stack segment
0x00000000 0xFFFFFFFF
Hole
DLL
DLL
DLL
ALL of these segments appear to be present whenever process runs
memory management
3/5/03
-
4
(User View: code segments)
?
program code

allocated when program loaded

initialized with contents of load module
?
Dynamically Loadable Libraries

automatically mapped in whenever needed

somehow shared by multiple processes
?
all are read-only and fixed size
memory management
3/5/03
-
5
(User View: data segments)
?
initialized data

allocated when program loaded

initialized from load module
?
expansion/contraction of data segment

requested via system calls (e.g. sbrk)

only added/truncated part is affected
?
process stack

allocated and grown automatically
memory management
3/5/03
-
6
Implementing: code segments
?
program loader

ask for memory (size and virtual location)

copy code from load module into memory
?
run-time loader

request DLL be mapped (location and size)

edit linkage pointers from program to DLL
?
memory manager

allocates memory, maps into process
memory management
3/5/03
-
7
Implementing: data segments
?
program loader

ask for memory (location and size)

copy data from load module into memory

zero the BSS
?
memory manager

invoked for allocations and stack extensions

allocates and deallocates memory

adjusts process address space accordingly
memory management
3/5/03
-
8
Memory Allocation Strategies
?
fixed partitions

pre-allocate partitions for N processes

each process assigned to one partition
?
variable allocation

one massive "heap" of memory

divided among processes based on their needs
?
paged allocation
... and in that moment he became enlightened
memory management
3/5/03
-
9
Fixed Partition Memory Allocation
?
very easy to implement

common in old batch processing systems

well suited to well-known job mix
?
presumes largest possible process size

must reconfigure system for larger processes
?
likely to use memory inefficiently

large internal fragmentation losses

swapping results in convoys on partitions
memory management
3/5/03
-
10
Internal Fragmentation
partition 1
8mb
partition 2
4mb
partition 3
4mb
proc 1
6mb
proc 2
3mb
proc 3
1mb
waste
2mb
waste
3mb
waste 1mb
total waste = 2 + 1 + 3 = 6/16Mb = 37%
memory management
3/5/03
-
11
(Internal Fragmentation)
?
wasted space in fixed sized blocks
?
caused by a mis-match between

the chosen sizes of a fixed-sized blocks

the actual sizes that programs request
?
average waste: 50% of each block
?
overall waste reduced by multiple sizes

suppose blocks come in sizes S
1
and S
2

average waste = ((S
1
/2) + (S
2
- S
1
)/2)/2
memory management
3/5/03
-
12
Frequency of allocation requests
size of request
frequency
64 128 4096
memory management
3/5/03
-
13
(distribution of allocation requests)
?
internal fragmentation in fixed partition
allocation resulted from random requests

but are memory allocations really random?

there are peaks in the frequency v.s. size curve,
some sizes are used and reused very often
?
many services use fixed size transient buffers

OS: terminal I/O, network packets, disk blocks, ...

applications: messages, request descriptors, ...
memory management
3/5/03
-
14
fixed partition allocation revisited
?
if we know what the popular sizes are

create/reserve special pools of fixed size buffers

satisfy matching requests from those pools
?
benefit: improved efficiency

much simpler than variable partition allocation

reduces external fragmentation of heap
?
but ... we must know how much to reserve

too little -> contention, too much -> wasted space
memory management
3/5/03
-
15
fixed partition bit-map free list
0 1 1 0 0 0 1
...
free partition bit map (one bit per partition)
actual memory partitions
partn 1
in use
partn 2
free
partn 3
free
partn 4
in use
partn 5
in use
a bit-map is a very simple and compact way to
keep track of which partitions are free and in use
memory management
3/5/03
-
16
Variable Memory Allocation
?
start with one large "heap" of memory
?
when a process requests more memory

find a large enough chunk of memory

carve off a piece of the requested size

put the remainder back on the free list
?
when a process frees memory

put it back on the free list
?
eliminates internal fragmentation losses
memory management
3/5/03
-
17
External Fragmentation
P
0
P
1
P
2
P
0
P
3
P
2
P
0
P
3
P
2
P
4
P
5
P
3
P
2
P
4
memory management
3/5/03
-
18
(External/Global Fragmentation)
?
each allocation creates left-overs

over time they become smaller and smaller
?
the small left-over fragments are useless

they are too small to satisfy any request

a second form of fragmentation waste
?
solutions:

try not to create tiny fragments

try to recombine fragments into big chunks
memory management
3/5/03
-
19
variable partition free list
free
free free
Each chunk begins with a header:
size of chunk, pointer to next chunk,
other optional diagnostic information
List might contain only free chunks, or all chunks
head
memory management
3/5/03
-
20
(Free lists: keeping track of it all)
?
fixed sized blocks are easy to track

a bit map indicating which blocks are free
?
variable allocation requires more information

a linked list of descriptors, one per chunk

each lists size of chunk, whether it is free

each has pointer to next chunk on list

descriptors often at front of each chunk
?
allocated memory often has descriptors too
memory management
3/5/03
-
21
Which chunk: best fit
?
search for the "best fit" chunk

smallest size greater/equal to requested size
?
advantages:

might find a perfect fit
?
disadvantages:

have to search entire list every time

quickly creates very small fragments
memory management
3/5/03
-
22
Which chunk: worst fit
?
search for the "worst fit" chunk

largest size greater/equal to requested size
?
advantages:

tends to create very large fragments
?
disadvantages:

still have to search entire list every time
memory management
3/5/03
-
23
Which chunk: first fit
?
take first chunk that is big enough
?
advantages:

very short searches

creates random sized fragments
?
disadvantages:

the first chunks quickly fragment

searches become longer

ultimately it fragments as badly as best fit
memory management
3/5/03
-
24
Which chunk: next fit
free
free free
after search, leave guess pointing to next free chunk
next search starts where previous search left off
when guess reaches end of list, wrap back to start
head
guess
memory management
3/5/03
-
25
(next-fit ... guess pointers)
?
the best of both worlds

short searches (maybe shorter than first fit)

spreads out fragmentation (like worst fit)
?
guess pointers are a general technique

think of them as a lazy (non-coherent) cache

if they are right, they save a lot of time

if they are wrong, the algorithm still works

they can be used in a wide range of problems
memory management
3/5/03
-
26
Coalescing – de-fragmentation
?
all VP algorithms have external fragmentation

some do it faster, some spread them out
?
we need a way to reassemble fragments

check neighbors when ever a chunk is freed

recombine free neighbors whenever possible

free list can be designed to make this easier
?
e.g. where are the neighbors of this chunk?
?
this counters forces of external fragmentation
memory management
3/5/03
-
27
Free list must support coalescing
?
coalescing happens at free time

when freeing a region, check its neighbors

if either neighbor is free, recombine them
?
it must be easy to find both adjacent regions

e.g. doubly linked list of region descriptors

other coupling between neighbors
(e.g. buddy system, where all regions are paired)
memory management
3/5/03
-
28
Coalescing vs. Fragmentation
?
opposing processes operate in parallel

which of the two processes will dominate?
?
what fraction of space is typically allocated?

coalescing works better with more free space
?
how fast is allocated memory turned over?

chunks held for long time cannot be coalesced
?
how variable are requested chunk sizes?

high variability increases fragmentation rate
memory management
3/5/03
-
29
fixed vs. variable partitions
?
fixed partition allocation

internal fragmentation is inevitable

multiple sizes may greatly reduce the problem
?
variable partition allocation

more complex free lists, possible long searches

external fragmentation evolves over time

coalescing can counteract the fragmentation
?
at a cost of more complexity and longer operations
memory management
3/5/03
-
30
memory allocation within a process
?
use OS allocation to get memory for process

e.g. sbrk system call to extend data segment
?
UNIX malloc (user mode memory allocation)

variable partition, first fit-allocation

go back to OS to get more if heap is empty
?
UNIX mfree (return memory when done)

return memory to free list w/neighbor coalescing
memory management
3/5/03
-
31
Diagnostic Free lists
?
common mistakes made w/dynamic memory

memory leaks (allocate it and never free it)

overruns (use more memory than you allocated)

clobbers (keep on using it after you free it)
?
free list can help to catch these problems

all chunks in list (whether allocated or free)

record of who last allocated each chunk

guard zones at beginning and end of chunks
memory management
3/5/03
-
32
garbage collection
?
programmers often forget to free memory
?
garbage collection is an alternative to freeing

applications allocate objects, but never free them
?
when we run out, start garbage collection

search data space finding every object pointer

note address/size of all accessible objects

compute the compliment (what is inaccessible)

add all inaccessible memory to the free list
memory management
3/5/03
-
33
enabling garbage collection
?
how to find all accessible data?

in the general case, this is impossible
?
object oriented languages often enable this

all object references are tagged

all object descriptors include size information
?
it is often possible for system resources

where all possible references are known (e.g. do
any processes still owned by this user)
memory management
3/5/03
-
34
What to do when coalescing loses
?
garbage collection is just another way to free
?
ongoing activity can always starve coalescing

chunks reallocated before they can be combined
?
we could stop accepting new allocations

convoy on memory manager destroys throughput
?
we need a way to rearrange active memory

re-pack all processes in one end of memory

create one big chunk of free space at other end
memory management
3/5/03
-
35
Memory Compaction
P
5
P
3
P
2
P
4
P
5
P
3
P
2
P
4
memory before
compaction
memory after
compaction
swap
device
memory management
3/5/03
-
36
Reasons to Relocate a Process
?
to compact fragmented free space
?
when a process increases its data space size

for example, the UNIX sbrk system call
?
migration to and from 2
nd
ary storage

primary memory needed for runnable processes

swap blocked processes to 2
nd
ary storage

swap them back-in when they become runnable

the original memory may no longer be available?
memory management
3/5/03
-
37
The Relocation Problem ...
?
how to move a process

from memory region where it was initially loaded

into a new and different region of memory
?
all addresses in the program will be wrong

references in the code segment
?
calls and branches to other parts of the code
?
references to variables in the data segment

plus new pointers in data and stack segments
memory management
3/5/03
-
38
The Relocation Problem ...
?
it is not feasible to re-relocate a process

maybe we could re-relocate references to code
?
if we kept the relocation information around

but how can we relocate references to data
?
pointer values may have been changed
?
new pointers may have been created
?
we could never find/fix all address references

like the general case of garbage collection
?
can we make processes location independent?
memory management
3/5/03
-
39
Virtual Address Translation
physical
address space
addresses on CPU/memory bus
00000
00000
NNNN
MMMM
memory
management
unit
virtual
address space
addresses seen by processes
memory management
3/5/03
-
40
Segment Relocation
?
computer has special relocation registers

they are called segment base registers

used in IBM/360, still used in Intel Pentium
?
point to start of code, data, stack in memory

code and data references are relative to these

OS initializes base registers when loading program
?
if process must be moved to a new location

reset base registers to point to start of new region

all code/data references are automatically relocated
memory management
3/5/03
-
41
Segment Relocation
code
data
stack
memory
code base
data base
stack base
base registers
physical = virtual + base
memory management
3/5/03
-
42
Paged Memory Management
?
divide physical memory into fixed-size pages

typically 1,024, 4,096 or 8,192 bytes long
?
divide virtual memory into same sized pages
?
paging Memory Management Unit

maps virtual page #s into physical page #s
?
more flexible than base register relocation

consecutive virtual pages need not map to
consecutive physical pages
memory management
3/5/03
-
43
Paging Address Translation
physical
pages
memory
management
unit
virtual
pages
vpage = virtual/pagesize;
offset = virtual%pagesize;
ppage = map(vpage);
physical = (ppage * pagesize) + offset;
memory management
3/5/03
-
44
Paging and Fragmentation
A code or data segment is made up of multple pages
Internal fragmentation is limited to the unused portion
of the last page of the segment. If a page is 4K, the
average waste is 2K. If the average segment is 200K,
the expected loss due to internal fragmentation is 1%.
External fragmentation is non-existent because we
only allocate whole pages, we don't carve them up.
memory management
3/5/03
-
45
(Paging solves many problems)
?
internal fragmentation

average of ½ page per address space segment

but there are many pages per segment

e.g. a 200K segment and a 4K page yields 1% loss
?
external fragmentation

non-existent: pages are never carved up
?
finding enough contiguous memory

contiguous physical memory is not required

mapping can make any pages appear contiguous
memory management
3/5/03
-
46
For the next lecture
?
read chapter 12

there will be a quiz on this material
?
topics for next lecture

paging virtual address translation

page faults and demand paging

replacement algorithms and working sets

segmentation and shared memory

user and kernel address spaces
memory management
3/5/03
-
47
key points
?
memory manager responsibilities

allocation, map-in, map-out, migration
?
fixed partitions, internal fragmentation

estimating and mitigating fragmentation
?
variable partition, external fragmentation

algorithms, performance, free-lists, coalescing
?
special pools of fixed size buffers
memory management
3/5/03
-
48
key points
?
garbage collection

what it is, why it is done, how it is done
?
the relocation problem

what it is, why it is hard
?
how virtual address spaces solve it

segment relocation, paged relocation
?
advantages of paged allocation