6.172...heap memory in use at any time by a program is m. if the heap is managed by a bfl allocator,...

86
!"##$ %&'&( ! "#) +)$#) +, -./01 6.172 Performance Engineering of Software Systems © 2008-2018 by the MIT 6.172 Lecturers LECTURE 11 Storage Allocation Julian Shun 1

Upload: others

Post on 03-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

!"##$%&'&(!"#)*+)$#)*+,*-./01

6.172 Performance Engineering of Software Systems

© 2008-2018 by the MIT 6.172 Lecturers

LECTURE 11 Storage Allocation

Julian Shun

1

Page 2: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

!"##$%&'&(!"#)*+)$#)*+,*-./01

© 2008-2018 by the MIT 6.172 Lecturers

STACKS

2

Page 3: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Stack Allocation

Array and pointer

used unused

!"

#

Allocate x bytes !"$%&$'( )*+,)-$!"$. '(

© 2008-2018 by the MIT 6.172 Lecturers 3

Page 4: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Stack Allocation

Array and pointer

used unused

!"

#

!"#$%#&' $%&'$()!")* +,

Allocate x bytes

Should check for stack overflow.

© 2008-2018 by the MIT 6.172 Lecturers 4

Page 5: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Stack Allocation

Array and pointer

used unused

!"

#

!"$%&$'( !"#$!%&'(&) *+

Allocate x bytes

Should check for stack overflow.

© 2008-2018 by the MIT 6.172 Lecturers 5

Page 6: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Stack Deallocation

Array and pointer

used unused !

"#

Allocate x bytes Free x bytes "#$%&$'( "#$)&$'(

*+,-*.$"#$% '(

© 2008-2018 by the MIT 6.172 Lecturers 6

Page 7: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Stack Deallocation

Array and pointer

used unused

!"

#

Allocate x bytes Free x bytes !"#$%#&'

Should check for

!"$%&$'( )*+,)-$!"$. '(

stack underflow.

© 2008-2018 by the MIT 6.172 Lecturers 7

Page 8: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Stack Storage Array and pointer

used unused

!"

#

Allocate x bytes Free x bytes !"*+,*-. !"*/,*-.

012304*!"*+ -.

!Allocating and freeing take "(1) time.!Must free consistent with stack discipline.! Limited applicability, but great when it works!!One can allocate on the call stack using $%%&'$(),

but this function is deprecated, and the compiler ismore efficient with fixed-size frames.

© 2008-2018 by the MIT 6.172 Lecturers 8

Page 9: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Stacks and Heaps

Image is in the public domain. Image is in the public domain.

Stack Heap

© 2008-2018 by the MIT 6.172 Lecturers 9

Page 10: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

!"##$%&'&(!"#)*+)$#)*+,*-./01

© 2008-2018 by the MIT 6.172 Lecturers

FIXED-SIZE HEAP ALLOCATION

10

Page 11: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Heap Allocation* C provides malloc() and free(). C++ provides new and delete.

Unlike Java and Python, C and C++ provide no garbage collector. Heap storage allocated by the programmer must be freed explicitly. Failure to do so creates a memory leak. Also, watch for dangling pointers and double freeing.

Memory checkers (e.g., AddressSanitizer, Valgrind) can assist in finding these pernicious bugs.

*Do not confuse with a heap data structure.© 2008-2018 by the MIT 6.172 Lecturers 11

Page 12: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Allocation

! used used used

"#$$

Free list

used

! Every piece of storage has the same size! Unused storage has a pointer to next unused block

Bitmap mechanism ! Bit for each block saying whether or not it is free! Bit tricks for allocation

© 2008-2018 by the MIT 6.172 Lecturers 12

Page 13: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Allocation

! used used used

"#$$

Free list

used

Allocate 1 object %&'&"#$$( "#$$&'&"#$$)*+$%,( #$,-#+&%(&

© 2008-2018 by the MIT 6.172 Lecturers 13 13

Page 14: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Allocation

! used used used

"#$$ *

Free list

used

Allocate 1 object !"#"$%&&' "#$$%&%"#$$'()$*+, #$+-#)%*,%

© 2008-2018 by the MIT 6.172 Lecturers 14

Page 15: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Allocation

! used used used

"#$$ %

Free list

used

Allocate 1 object

Should check "#$$&,'&-.//.

%&'&"#$$( !"##$%$!"##&'(#)*+ #$)*#+&%(&

© 2008-2018 by the MIT 6.172 Lecturers 15

Page 16: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Allocation

! used used used

"#$$ % garbage

Free list

used

pointer Allocate 1 object %&'&"#$$( "#$$&'&"#$$)*+$%,( !"#$!%&'(

© 2008-2018 by the MIT 6.172 Lecturers 16

Page 17: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Allocation

! used used used

"#$$

Free list

used used

Allocate 1 object %&'&"#$$( "#$$&'&"#$$)*+$%,( #$,-#+&%(&

© 2008-2018 by the MIT 6.172 Lecturers 17

Page 18: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Deallocation

! used used used

"#$$

% Free list

used

Allocate 1 object free object %%&'&"#$$( "#$$&'&"#$$)*+$%,( #$,-#+&%(&

%)*+$%,&'&"#$$( "#$$&'&%(&

© 2008-2018 by the MIT 6.172 Lecturers 18

Page 19: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Deallocation

! used used used

"#$$

% Free list

used

Allocate 1 object free object %%&'&"#$$( "#$$&'&"#$$)*+$%,( #$,-#+&%(&

!"#$%!&'(')*%%+ "#$$&'&%(&

© 2008-2018 by the MIT 6.172 Lecturers 19

Page 20: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Deallocation

! used used used

"#$$

% Free list

used

Allocate 1 object free object %%&'&"#$$( "#$$&'&"#$$)*+$%,( #$,-#+&%(&

%)*+$%,&'&"#$$( !"##$%$&'

© 2008-2018 by the MIT 6.172 Lecturers 20 20

Page 21: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Fixed-Size Deallocation

! used used used

"#$$

Free list

used

Allocate 1 object free object %%&'&"#$$( "#$$&'&"#$$)*+$%,( #$,-#+&%(&

%)*+$%,&'&"#$$( "#$$&'&%(&

© 2008-2018 by the MIT 6.172 Lecturers 21

Page 22: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Free Lists

! used used used

"#$$

Free list

used

!Allocating and freeing take "(1) time.!Good temporal locality.! Poor spatial locality due to external fragmentation

— blocks distributed across virtual memory —which can increase the size of the page table andcause disk thrashing.!The translation lookaside buffer (TLB) can also be a

problem.

© 2008-2018 by the MIT 6.172 Lecturers 22

Page 23: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Mitigating External Fragmentation ! Keep a free list (or bitmap) per disk page.! Allocate from the free list for the fullest page.! Free a block of storage to the free list for the page

on which the block resides.! If a page becomes empty (only free-list items), the

virtual-memory system can page it out withoutaffecting program performance.

! 90-10 is better than 50-50:

>

Probability that 2 random accesses hit the same page = .9!.9 + .1!.1 = .82 versus .5!.5 + .5!.5 = .5

© 2008-2018 by the MIT 6.172 Lecturers 23

Page 24: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

!"##$%&'&(!"#)*+)$#)*+,*-./01

© 2008-2018 by the MIT 6.172 Lecturers

VARIABLE-SIZE HEAP ALLOCATION

24

Page 25: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Variable-Size Allocation Binned free lists ! Leverage the efficiency of free lists.!Accept a bounded amount of internal fragmentation.

0 1 2

!

!

Bin k holds memoryblocks of size 2k.

© 2008-2018 by the MIT 6.172 Lecturers 25

Page 26: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Allocation for Binned Free Lists Allocate % If bin k = "lg x# is nonempty, return a x bytes block.

% Otherwise, find a block in the next larger nonempty bin k& > k, split it up into blocks of sizes 2k&-1, 2k&-2, …, 2k, 2k, and distribute the pieces.

0 1 2 3 4 $

© 2008-2018 by the MIT 6.172 Lecturers

Example x = 3 ! "lg x# = 2. Bin 2 is empty.

26

Page 27: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Allocation for Binned Free Lists Allocate " If bin k = #lg x$ is nonempty, return a x bytes block.

" Otherwise, find a block in the next larger nonempty bin k% > k, split it up into blocks of sizes 2k%-1, 2k%-2, …, 2k, 2k, and distribute

© 2008-2018 by the MIT 6.172 Lecturers

0 1 2

4 3

!

the pieces. Example x = 3 & #lg x$ = 2. Bin 2 is empty.

27

Page 28: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Allocation for Binned Free Lists Allocate " If bin k = #lg x$ is nonempty, return a x bytes block.

" Otherwise, find a block in the next larger nonempty bin k% > k, split it up into blocks of sizes 2k%-1, 2k%-2, …, 2k, 2k, and distribute the pieces.

0 1 2 3 4 !

© 2008-2018 by the MIT 6.172 Lecturers

return

Example x = 3 & #lg x$ = 2. Bin 2 is empty.

28

Page 29: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Allocation for Binned Free Lists Allocate " If bin k = #lg x$ is nonempty, return a x bytes block.

" Otherwise, find a block in the next larger nonempty bin k% > k, split it up into blocks of sizes 2k%-1, 2k%-2, …, 2k, 2k, and distribute the pieces.*

Example 0 x = 3 & #lg x$ = 2. 1 Bin 2 is empty. 2

3 4 !

*If no larger blocks exist, ask thereturn OS to allocate

29more memory.

© 2008-2018 by the© 2008 MIT 6.172 Lecturers 2018 by the MIT 6.172 Lecturers

Page 30: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Storage Layout of a Program

high address

virtual memory

dynamically allocated

stack

bss

data

text

heap

initialized to 0 at program start

read from disk

code low address © 2008-2018 by the MIT 6.172 Lecturers 30

Page 31: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

How Virtual is Virtual Memory?

Q. Since a 64-bit address space takes over acentury to write at a rate of 4 billion bytes persecond, we effectively never run out of virtualmemory. Why not just allocate out of virtualmemory and never free?

A. External fragmentation would be horrendous!The performance of the page table woulddegrade tremendously leading to diskthrashing, since all nonzero memory must bebacked up on disk in page-sized blocks.

Goal of storage allocators Use as little virtual memory as possible, and try to keep the used portions relatively compact.

© 2008-2018 by the MIT 6.172 Lecturers 31

Page 32: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Analysis of Binned Free Lists

Theorem. Suppose that the maximum amount of heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation request for a block of size x consumes 2⌈lg x⌉ ≤ 2x storage. Thus, the amount of virtual memory devoted to blocks of size 2k is at most 2M. Since there are at most lg M free lists, the theorem holds. ■

⇒ In fact, BFL is Θ(1)-competitive with the optimalallocator (assuming no coalescing).

© 2008-2018 by the MIT 6.172 Lecturers 32

Page 33: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Coalescing

Binned free lists can sometimes be heuristically improved by splicing together adjacent small blocks into a larger block. ● Clever schemes exist for finding adjacent blocks

efficiently — e.g., the “buddy” system — but theoverhead is still greater than simple BFL.

● No good theoretical bounds exist that prove theeffectiveness of coalescing.

● Coalescing seems to reduce fragmentation inpractice, because heap storage tends to bedeallocated as a stack (LIFO) or in batches.

© 2008-2018 by the MIT 6.172 Lecturers 33

Page 34: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

!"##$%&'&(!"#)*+)$#)*+,*-./01

© 2008-2018 by the MIT 6.172 Lecturers

GARBAGE COLLECTION BY REFERENCE COUNTING

34

Page 35: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Garbage Collectors Idea ∙ Free the programmer from freeing objects.∙ A garbage collector identifies and recycles the

objects that the program can no longer access.∙GC can be built-in (Java, Python) or do-it-yourself.

35© 2008-2018 by the MIT 6.172 Lecturers 35

Page 36: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Garbage Collection

Terminology ●Roots are objects directly accessible by the

program (globals, stack, etc.).● Live objects are reachable from the roots by

following pointers.●Dead objects are inaccessible and can be

recycled.

How can the GC identify pointers? ● Strong typing.● Prohibit pointer arithmetic (which may slow down

some programs).

© 2008-2018 by the MIT 6.172 Lecturers 36 36

Page 37: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Reference Counting

Keep a count of the number of pointers referencing each object. If the count drops to 0, free the dead object.

root

2 1

1

2

1

3

root

root

© 2008-2018 by the MIT 6.172 Lecturers 37

Page 38: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Reference Counting

Keep a count of the number of pointers referencing each object. If the count drops to 0, free the dead object.

root

2 1

1

2

1

3

root

root

© 2008-2018 by the MIT 6.172 Lecturers 38

Page 39: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Reference Counting

Keep a count of the number of pointers referencing each object. If the count drops to 0, free the dead object.

root

2 1

1

2

1

3

root

root

0

3

© 2008-2018 by the MIT 6.172 Lecturers 39

Page 40: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Reference Counting

Keep a count of the number of pointers referencing each object. If the count drops to 0, free the dead object.

root

2 1

1

3

1

3

0

root

root

© 2008-2018 by the MIT 6.172 Lecturers 40

Page 41: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Reference Counting

Keep a count of the number of pointers referencing each object. If the count drops to 0, free the dead object.

root

2 1

1

2

1

3

root

root

0

0

2

© 2008-2018 by the MIT 6.172 Lecturers 41

Page 42: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Reference Counting

Keep a count of the number of pointers referencing each object. If the count drops to 0, free the dead object.

2 1

1

2

1

3

root

root

root

0

0

2

© 2008-2018 by the MIT 6.172 Lecturers 42

Page 43: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Limitation of Reference Counting

Problem A cycle is never garbage collected!

root

2 1

1

3

1

1

root

root

© 2008-2018 by the MIT 6.172 Lecturers 43

Page 44: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Limitation of Reference Counting

Problem A cycle is never garbage collected!

root

1 1

1

2

3

1 root

root

© 2008-2018 by the MIT 6.172 Lecturers 44

Page 45: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Limitation of Reference Counting

Problem A cycle is never garbage collected!

2 1

1

3

1

1

root

root

root Uncollected garbage stinks! Nevertheless, reference counting

works well for acyclic structures. 45© 2008-2018 by the MIT 6.172 Lecturers

Page 46: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

!"##$%&'&(!"#)*+)$#)*+,*-./01

© 2008-2018 by the MIT 6.172 Lecturers 47

MARK-AND-SWEEP GARBAGE COLLECTION

46

Page 47: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Graph Abstraction

Idea Objects and pointers form a directed graph G = (V, E). Live objects are reachable from the roots. Use breadth-first search to find the live objects.

!"#$%! &"'($)*!$%#""+%&(($) &,-.#/ 0$12 3456363%78$&(2

9$3:;3$&,-.#/ 0$<2

=>*:3$%7$?0$#($)6$0$@356363%7(2 !"#$%! &"' such that %68&(" A($)*!$%&,-.#/ 00$<($) &,-.#/ 0$12 3456363%78$&(2

9$9$9 FIFO queue 7

>3.@ +.*:

© 2008-2018 by the MIT 6.172 Lecturers 47

Page 48: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 48

Page 49: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 49

Page 50: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

!,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 50

Page 51: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 51

Page 52: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 52

Page 53: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 53

Page 54: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 54

Page 55: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 55

Page 56: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 56

Page 57: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 57

Page 58: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 58

Page 59: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & % ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 59

Page 60: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & % ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 60

Page 61: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & % ( ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 61

Page 62: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & % ( ,

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 62

Page 63: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & % ( ,

Done!

)&"' -"*.

© 2008-2018 by the MIT 6.172 Lecturers 63

Page 64: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Mark-and-Sweep

Mark stage: Breadth-first search marked all of the live objects.

Sweep stage: Scan over memory to free unmarked objects.

Mark-and-sweep doesn’t deal with fragmentation

© 2008-2018 by the MIT 6.172 Lecturers 64

Page 65: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

!"##$%&'&(!"#)*+)$#)*+,*-./01

© 2008-2018 by the MIT 6.172 Lecturers

STOP-AND-COPY GARBAGE COLLECTION

65

Page 66: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Breadth-First Search

!

" #

$

%

&

'

(

)

*

+

! # $ ' & % ( ,

Observation All live vertices are placed in contiguous storage in ,.

© 2008-2018 by the MIT 6.172 Lecturers 66

Page 67: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Copying Garbage Collector

FROM space

next allocation

dead

live

unused

© 2008-2018 by the MIT 6.172 Lecturers 67

Page 68: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Copying Garbage Collector

FROM space

next allocation

dead

live

unused

© 2008-2018 by the MIT 6.172 Lecturers 68

Page 69: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Copying Garbage Collector

FROM space

next allocation

dead

live

unused

© 2008-2018 by the MIT 6.172 Lecturers 69

Page 70: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Copying Garbage Collector

FROM space

next allocation

dead

live

unused

© 2008-2018 by the MIT 6.172 Lecturers 70

Page 71: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Copying Garbage Collector

FROM space

next allocation

dead

live

unused

© 2008-2018 by the MIT 6.172 Lecturers 71

Page 72: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Copying Garbage Collector

FROM space

next allocation

dead

live

unused

© 2008-2018 by the MIT 6.172 Lecturers 72

Page 73: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Copying Garbage Collector

FROM space

next allocation

dead

live

unused

When the FROM space is “full,” copy live storage using BFS with the TO space as the FIFO queue.

© 2008-2018 by the MIT 6.172 Lecturers 73

Page 74: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Copying Garbage Collector

FROM space

next allocation

dead

live

unused

When the FROM space is “full,” copy live storage using BFS with the TO space as the FIFO queue.

allocation

TO space

next

© 2008-2018 by the MIT 6.172 Lecturers 74

Page 75: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Updating Pointers

Since the FROM address of an object is not generally equal to the TO address of the object, pointers must be updated. ∙When an object is copied to the TO space, store a

forwarding pointer in the FROM object, whichimplicitly marks it as moved.

∙When an object is removed from the FIFO queue inthe TO space, update all its pointers.

© 2008-2018 by the MIT 6.172 Lecturers 75

Page 76: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Example

FROM

TO

!"#$ %#&'

Remove an item from the queue.

© 2008-2018 by the MIT 6.172 Lecturers 76

Page 77: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Example

FROM

TO

!"#$ %#&'

Remove an item from the queue.

© 2008-2018 by the MIT 6.172 Lecturers 77

Page 78: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Example

FROM

TO

!"#$ %#&'

Enqueue adjacent vertices.

© 2008-2018 by the MIT 6.172 Lecturers 78

Page 79: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Example

FROM

TO

!"#$ %#&'

Enqueue adjacent vertices. Place forwarding pointers in FROM vertices.

© 2008-2018 by the MIT 6.172 Lecturers 79

Page 80: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Example

!"#$ %#&'

FROM

TO

Update the pointers in the removed item to refer to its adjacent items in the TO space.

© 2008-2018 by the MIT 6.172 Lecturers 80

Page 81: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Example

!"#$ %#&'

FROM

TO

Update the pointers in the removed item to refer to its adjacent items in the TO space.

© 2008-2018 by the MIT 6.172 Lecturers 81 82

Page 82: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Example

FROM

TO

!"#$ %#&'

Linear time to copy and update all vertices.

© 2008-2018 by the MIT 6.172 Lecturers 82 83

Page 83: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

When Is the FROM Space “Full”?

used FROM

heap

! Request new heap space equal to the used space,and consider the FROM space to be “full” whenthis heap space has been allocated.

! The cost of garbage collection is thenproportional to the size of the new heap space !amortized O(1) overhead, assuming that the userprogram touches all the memory allocated.

! Moreover, the VM space required is O(1) timesoptimal by locating the FROM and TO spaces indifferent regions of VM where they cannotinterfere with each other.

© 2008-2018 by the MIT 6.172 Lecturers 83

Page 84: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Dynamic Storage Allocation

Lots more is known and unknown about dynamic storage allocation. Strategies include ● buddy system,● variants of mark-and-sweep,● generational garbage collection,● real-time garbage collection,● multithreaded storage allocation,● parallel garbage collection,● etc.

© 2008-2018 by the MIT 6.172 Lecturers 84

Page 85: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

Summary

● Stack: most basic form of storage and is veryefficient when it works

● Heap is the more general form of storage● Fixed-size allocation using free lists● Variable-sized allocation using binned free lists● Garbage collection – reference counting, mark-

and-sweep, stop-and-copy● Internal and external fragmentation● You will look at storage allocation in Homework 6

and Project 3

© 2008-2018 by the MIT 6.172 Lecturers 85

Page 86: 6.172...heap memory in use at any time by a program is M. If the heap is managed by a BFL allocator, the amount of virtual memory consumed by heap storage is O(M lg M). Proof. An allocation

MIT OpenCourseWare https://ocw.mit.edu

6.172 Performance Engineering of Software Systems Fall 2018

For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.

86