memory management · 2004. 11. 9. · garbage collection: stop & copy! partition heap in from...
TRANSCRIPT
System-Software WS 04/05
73© P. Reali / M. Corti
Memory ManagementOverview
! Abstractions for applications
– heap– memory blocks
( << memory pages)
! Operations:– Allocate– Deallocate
! Topics:– memory organization– free lists– allocation strategies– deallocation explicit– garbage collection
! type-aware! conservative! copying / moving! incremental! generational
System-Software WS 04/05
74© P. Reali / M. Corti
Memory ManagementObjects on the heap
❍ Object Instances: a, b, c, d, …❍ Sequence:
NEW(a)NEW(b)NEW(c)DISPOSE(b)NEW(d)NEW(e)
a
b
c
dynamicallocation
explicitdisposal
„Heap“
e
a
c
d
!
e
Case 1
e
e
Case 2
not enough space
System-Software WS 04/05
75© P. Reali / M. Corti
Memory ManagementProblem overview
❍ Problems● Heap size limitation (→ e, case 1)● External Fragmentation (→ e, case 2)● Dangling Pointers (a points to b)
❍ Solutions● System-managed list of free blocks
(„free list“)● Vector of blocks with fixed size
(Bitmap, with 0=free, 1=used)● Automated detection and reclamation of unused blocks
(„garbage collection“)
System-Software WS 04/05
76© P. Reali / M. Corti
Memory ManagementTheory: 50% rule
" Assumption: stable state" M free blocks, N block allocated" 50%-Rule: M = 1/2 N
A B B B BC C
N = A + B + CM = 1/2 (2A + B + e) e = 0,1, or 2
block disposal: ∆M = (C - A) / Nblock allocation:(splitting likelihood)
∆M = 1 - p
B
(C - A) / N = 1 - pC - A - N + pN = 0
2M = 2A + B + e2M = 2A + N - A - C + e2M = N + A - C + e
2M +e = pN
System-Software WS 04/05
77© P. Reali / M. Corti
Memory ManagementTheory: Memory Fragmentation
Parameter Variable Memory Size H Allocated block average size B Free block average size F Number of allocated blocks b Memory usage b*B/H λ Factor F/B µ
⇒ { 50%-Rule }(b/2)*F = H - b*B, µ/2*b*B = H - b*B⇒H/(b*B) = 1 + µ/2, µ = 2/λ - 2
λ µ1/4 61/2 22/3 13/4 2/3
Criticalpoint
System-Software WS 04/05
78© P. Reali / M. Corti
Memory ManagementFree-list management with a Bitmap
! Idea– partition heap in blocks of size s– use bitmap to track allocated blocks
bitmap[i] = true # blocki allocated! Problems
– internal fragmentationround up block size to next multiple of s
– map sizesize is (heap_size / s) bits
loss due to internal
fragmentation
System-Software WS 04/05
79© P. Reali / M. Corti
Memory ManagementFree-list management with a list
! List organization– sorted / non-sorted
! merging of empty blocks is simpler with sorted list– one list / many lists (per size)
! search is simpler, merging is more difficult– management data stored in the free block
! size, next pointer
! Operations– Allocation– Disposal with merge
! find free blocks next to current block, merge into bigger free block
System-Software WS 04/05
80© P. Reali / M. Corti
Memory ManagementMemory allocation strategies
! block splitting:– if a free-block is bigger than the requested block, then it is split
! first-fit– use first free block which is big enough
! best-fit– take smallest fitting block # causes a lot of fragmentation
! worst-fit– take biggest available block
! quick-fit– best-fit but multiple free-lists (one per block size) # fast allocation!
freeused used freeused usedused
usedused used
internal fragmentation
System-Software WS 04/05
81© P. Reali / M. Corti
Memory ManagementBuddy System (for fast block merging)
! Blocks have size 2k
! Block with size 2i has address x*2i (last i bits are 0)
! Blocks with address x*2i
and (x XOR 1)*2i are buddies (can be merged into a block of size 2i+1)
! buddy = x XOR 2i3264
3232161632816 8 b1 xxxx 0 0000
b2 xxxx 1 00002k+1
2k
2k-1 Merge
Split
System-Software WS 04/05
82© P. Reali / M. Corti
Memory ManagementBuddy System (for fast block merging)
! Problem: only buddies can be merged
! Cascading merge
321616
32816 8321616
32816 8
no buddiesbuddies
32816 8
321616
3232
System-Software WS 04/05
83© P. Reali / M. Corti
Memory ManagementBuddy System (for fast block merging)
! Allocation– allocate(8)
328 168
321616
3232split
split
quickfit
328 168
System-Software WS 04/05
84© P. Reali / M. Corti
"Block size = k*32free-lists for k = 1..9, one list for blocks > 9*32
"Allocate quick-fit, splitting may be required"Free-list management and block-merging done
by the Garbage Collector
Memory ManagementExample: Oberon / Aos
k * 32966432
ALLOCATE(50)
initialstate
k * 32966432 Allocated Block
System-Software WS 04/05
85© P. Reali / M. Corti
Memory ManagementGarbage Collection
Two steps:1. Free block detection
– type-aware! collector is aware of the
types traversed, i.e. know which values are pointers
– conservative! collector doesn’t know
which values are pointers
2. Block Disposal! return unused blocks to the
free-lists
! GC Characteristics– incremental
gc is performed in small steps to minimize program interruption
– moving / copying / compactingblocks are moved around
– generationalblocks are grouped in generations; different treatment or collection priority
! Barriers– read
intercept and check every pointer read operation
– writeintercept and check every pointer write operation
System-Software WS 04/05
86© P. Reali / M. Corti
Memory ManagementGarbage Collection: Reference Counting
❍ Every object has a Referece counter rc❍ rc = 0 ⇒ Object is „Garbage“❍ Problems
● Overhead
● no support for circular structures
❍ Useful for...● Module hierarchies● DAG-Structures (z. B. LISP)
p, q Pointers to Objectq := p
rc
p
rc
qwrite barrier
INC p.rcDEC q.rcIF q.rc = 0 THEN
Collect q^END;q := p
M
A B
C D
rc >= 1
rc >= 1
System-Software WS 04/05
87© P. Reali / M. Corti
Memory ManagementGarbage Collection: Mark & Sweep
❍ Mark-Phase (Garbage Detection)● Compute the Root-set consisting of
$ global pointers (statics) in each module$ local pointers on the stack in each PAF$ temporary pointers in the CPU’s registers
● Traverse the graph of the live objects starting from the root-set with depth-first strategy; mark all reached objects.
❍ Sweep-Phase (Garbage Collection)● Linear heap traversal. Non-marked blocks are inserted into
free-lists.● Optimization: lazy sweeping (sweep during allocation,
allocation gets slower)
System-Software WS 04/05
88© P. Reali / M. Corti
"Run-time support from object-system. Hidden data structures with (compiler generated) information about pointers (metadata).
❍ Conservative approach. Guess which values could be pointers and threat them as such
Memory ManagementGarbage Collection: root-set
off
off1
off2
off2off1
off
ModuleDescriptor
ModuleData
ObjectInstance
TypDescriptor
Type Tag
globalpointer
instancepointer
System-Software WS 04/05
89© P. Reali / M. Corti
Memory ManagementGarbage Collection: Mark with Pointer Rotation/1
Problem:Garbage collection called when free memory is low, but mark may require a lot of memory
Solution:Pointer rotation algorithm (Deutsch, Schorre , Waite)
+ Memory efficient+ iterative
– structures are temporarily inconsistent– non-concurrent– non-incremental
System-Software WS 04/05
90© P. Reali / M. Corti
Memory ManagementGarbage Collection: Mark with Pointer Rotation/2
q pq p
p.link
Simple case: list traversal
System-Software WS 04/05
91© P. Reali / M. Corti
Memory ManagementGarbage Collection: Mark with Pointer Rotation/3
q
p q
p
Generic case: structure traversal
System-Software WS 04/05
92© P. Reali / M. Corti
Memory ManagementGarbage Collection: Memory Compaction
" nextavail Pointer: partition heap between allocated and free space
" Allocate: increment nextavail" Garbace Collector performs memory compaction
nextavail
ALLOC
GC
MS .NET
System-Software WS 04/05
93© P. Reali / M. Corti
Memory ManagementGarbage Collection: Stop & Copy
! Partition heap in from and to regions! Collection:
– traverse objects in from, copy to to– leave forwarding pointer behind– requires read barrier– swap from and to
! Characteristics– copying– incremental– (generational)
IF p is moved THENreplace p with forwarding pointer
END;access p
access p
instrument code with read barrier
System-Software WS 04/05
94© P. Reali / M. Corti
Memory ManagementGarbage Collection: Stop & Copy
from to1
from to2
from to3
to from4
System-Software WS 04/05
95© P. Reali / M. Corti
Memory ManagementGarbage Collection: Concurrent GC
❍ „Stop-and-Go“ Approach
❍ „Incremental“ Approach
Mutator Mutator MutatorGC GC
Mutator
GC
Mutator Mutator Mutator
User Prozess
Real-TimeConstraint
System-Software WS 04/05
96© P. Reali / M. Corti
Memory ManagementGarbage Collection: Tricolor marking
"„Wave-front“ Model
whitenot reached yet,in front of the wave
greybeing traversed,on the wave
blackalready traversed,behind wave
ColorState
System-Software WS 04/05
97© P. Reali / M. Corti
❍ Motator can change pointers at any time● Critical case: black → white
❍ Remedy● Write-Barrier
● color B gray● color W gray
Memory ManagementGarbage Collection: Tricolor marking / Isolation
W
unreachable
B
WriteBarrier
System-Software WS 04/05
98© P. Reali / M. Corti
Memory ManagementGarbage Collection: Backer‘s Treadmill
To-SpaceFrom-Space
Free-Space
Heap: double-linked chain of
objects
curscan
System-Software WS 04/05
99© P. Reali / M. Corti
Memory ManagementGarbage Collection: Backer‘s Treadmill
To-Space From-Space
Free-Space
curscan
conservativeallocation
progressiveallocation
System-Software WS 04/05
100© P. Reali / M. Corti
Memory ManagementGarbage Collection: Backer‘s Treadmill
collect
To-SpaceFrom-Space
Free-Space
curscan
reference
curscancurscan
System-Software WS 04/05
101© P. Reali / M. Corti
Memory ManagementGarbage Collection: Backer‘s Treadmill
❍ State transitions after GC is complete● From-Space + Free-Space → Free-Space● ToSpace → FromSpace
❍ Fragmentation● External: not removed● Internal: depends on
supported block sizes❍ Allocation
● conservative: black● progressive: white
Root Set
x
y
NEW(y)
NEW(x)
curscan
System-Software WS 04/05
102© P. Reali / M. Corti
Memory ManagementGenerational Garbage Collection
"Generations!Expected object life
young ⇒ short life (temp data)old ⇒ long life
!Generations G0, G1, G2
ABCDE
ADFG
AG
HIJ
G2G1
G0 special handling for pointers
across different generations
required
lowG2mediumG1
highG0
GC frequencyGen
collect where it is garbage is most
likely to be found
System-Software WS 04/05
103© P. Reali / M. Corti
Memory ManagementGarbage Collection: Finalization
"Finalization (after-use cleanup)!User-defined routine when object is collected!Establish Consistency
! save buffers! flush caches
!Release Resources! close connections! release file descriptors
!Dangers: !Resurrection of objects: objects added to live structures!Finalization sequence is underfined
System-Software WS 04/05
104© P. Reali / M. Corti
Memory ManagementGarbage Collection: .NET Finalization Example
"Rules:"objects with finalizer belong to
older generation"finalizer only called once
(ReRegisterForFinalize)"FinalizationQueue: live object
with finalizer"FreachableQueue: collected
objects to be finalized"Finalization executed by
different process for security reasons
ABCDE E
BA
garbage FinalizationQueue
ABCDE
EB
A FinalizationQueue
FreachableQueue
GC
thread
System-Software WS 04/05
105© P. Reali / M. Corti
Memory ManagementGarbage Collection: Weak Pointers
" „Weak“ Pointers! Objects referenced only
through a weak pointer can be collected by the GC in case of need
! Used for Caches and Buffers
! Implementation1. Weak Pointers are not
registered to the GC2. Use a weak reference table
(indirect access)
garbagegarbage in use
weak pointer
weak reference
weak reference table
System-Software WS 04/05
106© P. Reali / M. Corti
Memory ManagementGarbage Collection: Weak Pointers Example
! Oberon: internal file list– system must keep track of open files to avoid buffer
duplication– file descriptor must be collected once user has no
more reference to it– use weak pointer in the system (otherwise would
keep file alive!)
System-Software WS 04/05
107© P. Reali / M. Corti
Memory ManagementObject Pools
"Application keeps a pool of preallocated object instances; handles allocation and disposal● Simulation discrete events● Buffers in a file system● Provide dynamic allocation in real-time systemPROCEDURE NewT (VAR p: ObjectT);BEGINIF freeT = NIL THEN NEW(p)ELSE p := freeT; freeT := freeT.next; freeT
ENDEND NewT;
PROCEDURE DisposeT (p: ObjectT);BEGIN p.next := freeT; freeT := pEND DisposeT;
System-Software WS 04/05
108© P. Reali / M. Corti
Garbage Collection, Recap
GC kinds:! compacting! copying! incremental! generationalHelpers:! write barrier! read barrier! forwarding pointer! pointer rotation
Algorithms:! Ref-Count! Mark & Sweep! Stop & Copy! Mark & Copy (.NET)! Baker’s Threadmill
– Dijkstra / Lamport– Steele