Memory Management: Runtime
Memory Management: Runtime
3:
This produces a stack of activation records:
grow 2:
Why not link list?
push on call, pop on return
1:
0:
1
Some program P needs 3 words Some program Q needs 2 words
top: 4 The manager program calls top: 1 The manager program calls
7:
m.push(3). 7:
m.push(2).
3: 3:
second
2: Q gets this 2: activation record
pointer
1: 1: 4
0: 0:
wasted
Heap implementation
9:
in an array 9:
8:
8:
7:
7:
6:
6: -1
5:
Some program P Size of this block
5: 5
4: requests 4 words
4:
3:
2:
Pointer to next P gets this 3:
first allocated
block pointer 2:
block
1: -1
1:
freeStart: 0 0: 10
2
Some program Q 9:
9: -1 -1
requests 2 words 9: -1
8: 2 8: 2
8: 2 7:
second allocated
Q gets this block
7:
second allocated
7: 6:
pointer second allocated
6: block
6: block 5: 3
4: 5: 3
Size of this block 5: 3
3: 4:
first allocated
4: block
2:
3:
3: 1:
P is done
first allocated
2:
block freeStart: 8 0: 5
2:
1: 8
1:
freeStart: 0 0: 5
freeStart: 8 0: 5
9:
8:
-1
A Problem
2
7:
second allocated
Consider this sequence:
Some program R block
6:
p1=m.allocate(4); -1
requests 1 word p2=m.allocate(4);
5: 3
m.deallocate(p1);
5
4: m.deallocate(p2);
p3=m.allocate(7);
3: 8
R gets this
2: 3
pointer
1:
third allocated The manager needs to coalesce
block 5
freeStart: 2 0: 2 adjacent free blocks 5
8: -1
p1=m.allocate(4);
Keep separate free lists for popular (small) p2=m.allocate(1); 7: 3
block sizes m.deallocate(p1); 6:
second allocated
block
p3=m.allocate(5);
Quick lists, a separate list, blocks are one 5: 2
The final allocation will fail because
size of fragmentation. 4:
eventually) 1: 7
freeStart: 0 0: 5
3
Heap Mechanisms Placement
Many variety Where to allocate a block
Three major issues: Our mechanism: first fit from FIFO free list
– Placement—where to allocate a block Some mechanisms use a similar linked list
– Splitting—when and how to split large blocks of free blocks: first fit, best fit, next fit, etc.
– Coalescing—when and how to recombine Some mechanisms use a more scalable data
Many refinements structure like a balanced binary tree
Splitting Coalescing
When and how to split large blocks When and how to recombine adjacent free
Our mechanism: split to requested size blocks
Sometimes you get better results with less Several varieties:
splitting—just allocate more than requested – No coalescing
or rounding up allocation size to some – Eager coalescing
multiple – Delayed coalescing (as with quick lists)
4
Discarding Impossible Links
To Find Current Heap Links
Depending on the language and
Start with the root set: memory locations outside implementation, we may be able to discard
of the heap with links into the heap {a b c }
– Active activation records (if on the stack)
some locations from the set:
– Static variables, etc. They are not in the active – If they do not point to/into allocated heap blocks (Java,
activation record. but not C)
– If their dynamic type rules out use as heap links
For each memory location in the set, look at the
allocated block it points to, and add all the – If their static type rules out use as heap links (Java, but
not C)
memory locations in that block
Repeat until no new locations are found
5
Three Major Approaches Mark And Sweep
Mark and sweep A mark-and-sweep collector uses current heap
links in a two-stage process:
Copying – Mark: find the live heap links and mark all the heap
blocks linked to by them
Reference counting – Sweep: make a pass over the heap and return unmarked
blocks to the free pool
(activation record) One problem with (activation record) When circle is set
free reference counting: it free to null, the reference
1 misses cycles of 1 counter is
link: link:
garbage. decremented.
free free
Here, a circularly free No reference counter
free
link:
1 linked list is pointed link:
1 is zero, though all
to by circle. blocks are garbage.
free free
the stack the stack
the heap the heap
6
Reference Counting Garbage Collecting Refinements
Problem with cycles of garbage Generational collectors
Problem with performance generally, since – Divide block into generations according to age
the overhead of updating reference counters – Garbage collect in younger generations more
is high often (using previous methods)
One advantage: naturally incremental, with Incremental collectors
no big pause while collecting – Collect garbage a little at a time
– Avoid the uneven performance of ordinary
mark-and-sweep and copying collectors