Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Automatic Memory Management
Lecture 27
Dr. Sean Peisert – ECS 142 – Spring 2009
1
Monday, June 1, 2009
Status
• Project 3 back sometime in the middle of this week.
• Project 4 due this Friday, 11:55pm
• Regular office hours this week
• Wednesday: Static Analysis for Security
• Friday: Final Exam Review, 9a-11a, Olson 105
2
Monday, June 1, 2009
Why Automatic Memory Management?
• Storage management is still a hard problem in modern programming
• C and C++ programs have many storage bugs
• forgetting to free unused memory
• dereferencing a dangling pointer
• overwriting parts of a data structure by accident
• and so on...
• Storage bugs are hard to find
• a bug can lead to a visible effect far away in time and program text from the source
3
Monday, June 1, 2009
Type Safety and Memory Management
• Some storage bugs can be prevented in a strongly typed language
• e.g., you cannot overrun the array limits
• Can types prevent errors in programs with manual allocation and deallocation of memory?
• Some fancy type systems (linear types) were designed for this purpose but they complicate programming significantly
• If you want type safety then you must use automatic memory management
4
Monday, June 1, 2009
Automatic Memory Management
• This is an old problem:
• Studied since the 1950s for LISP
• There are several well-known techniques for performing completely automatic memory management
• Until recently they were unpopular outside the Lisp family of languages
• just like type safety used to be unpopular
• Now in Java, scripting languages, etc...
5
Monday, June 1, 2009
The Basic Idea
• When an object that takes memory space is created, unused space is automatically allocated
• In Cool, new objects are created by new X
• After a while there is no more unused space
• Some space is occupied by objects that will never be used again
• This space can be freed to be reused later
6
Monday, June 1, 2009
The Basic Idea (Cont.)• How can we tell whether an object will “never be used
again”?
• In general it is impossible to tell
• We will have to use a heuristic to find many (not all) objects that will never be used again
• Observation: a program can use only the objects that it can find:
let x : A ← new A in { x ← y; ... }
• After x ← y there is no way to access the newly allocated object
7
Monday, June 1, 2009
Garbage
• An object x is reachable if and only if:
• A register contains a pointer to x, or
• Another reachable object y contains a pointer to x
• You can find all reachable objects by starting from registers and following all the pointers
• An unreachable object can never by referred by the program
• These objects are called garbage
8
Monday, June 1, 2009
Reachability is an Approximation
• Consider the program: x ← new A; y ← new B; x ← y; if always True() then x ← new A else x.func() fi
• After x ← y (assuming y becomes dead there) • The object A is not reachable anymore • The object B is reachable (through x) • Thus B is not garbage and is not collected • But object B is never going to be used
• “Not reachable” doesn’t mean not used. It means we can’t access it any longer.
9
Monday, June 1, 2009
Tracing Reachable Values in Coolc
• In coolc, the only register is the accumulator
• it points to an object
• and this object may point to other objects, etc.
• The stack is more complex
• each stack frame contains pointers
• e.g., method parameters
• each stack frame also contains non-pointers
• e.g., return address
• if we know the layout of the frame we can find the pointers in it
10
Monday, June 1, 2009
A Simple Example•
• In Coolc we start tracing from acc and stack
• they are called the roots
• Note that B and D are not reachable from acc or the stack
• Thus we can reuse their storage
11
!
Prof. Su ECS 142 Lecture 18 7
!"#$%&'()$*+#&$,-./012
! "#$%&'(%$)%*)++%$,)*,)-%'(%#./)&*%$0++%1()2)-.)%34)5%'6'0(789 :(%6)()-'+%0*%04%0;<#440.+)%*#%*)++
9 =)%$0++%,'2)%*#%34)%'%,)3-04*0&%*#%>0(5%;'(?%@(#*'++A%#./)&*4%*,'*%$0++%()2)-%.)%34)5%'6'0(
! B.4)-2'*0#(C%'%<-#6-';%&'(%34)%#(+?%*,)#./)&*4%*,'*%0*%&'(%>0(5C%%%%%%%%%%%%%%+)*%D%C%E%!%()$%E%0(%F%D%!%?G%HHH%I
9 E>*)-%D%!%?%*,)-)%04%(#%$'?%*#%'&&)44%*,)%()$+?'++#&'*)5%#./)&*
Prof. Su ECS 142 Lecture 18 8
3&45&6#
! E(%#./)&*%D%04%-)'&,'.+)%0>%'(5%#(+?%0>C9 E%-)604*)-%&#(*'0(4%'%<#0(*)-%*#%DJ%.4
9 E(#*,)-%-)'&,'.+)%#./)&*%?%&#(*'0(4%'%<#0(*)-%*#%D
! K#3%&'(%>0(5%'++%-)'&,'.+)%#./)&*4%.?%4*'-*0(6>-#;%-)604*)-4%'(5%>#++#$0(6%'++%*,)%<#0(*)-4
! E(%3(-)'&,'.+)%#./)&*%&'(%()2)-%.?%-)>)--)5.?%*,)%<-#6-';9 L,)4)%#./)&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 18 12
A8#=#/0'$.B$3&45&6#$-.88#)0(./
! V2)-?%6'-.'6)%&#++)&*0#(%4&,);)%,'4%*,)>#++#$0(6%4*)<4TH E++#&'*)%4<'&)%'4%())5)5%>#-%()$%#./)&*4
UH =,)(%4<'&)%-3(4%#3*C
'A M#;<3*)%$,'*%#./)&*4%;06,*%.)%34)5%'6'0(@6)()-'++?%.?%*-'&0(6%#./)&*4%-)'&,'.+)%>-#;%'4)*%#>%1-##*7%-)604*)-4A
.A S-))%*,)%4<'&)%34)5%.?%#./)&*4%(#*%>#3(5%0(%@'A
! W#;)%4*-'*)60)4%<)->#-;%6'-.'6)%&#++)&*0#(.)>#-)%*,)%4<'&)%'&*3'++?%-3(4%#3*
Monday, June 1, 2009
Elements of Garbage Collection
• Every garbage collection scheme has the following steps
• Allocate space as needed for new objects
• When space runs out: 1. Compute what objects might be used again
(generally by tracing objects reachable from a set of “root” registers)
2. Free the space used by objects not found in (1)
• Some strategies perform garbage collection before the space actually runs out
12
Monday, June 1, 2009
Mark and Sweep [McCarthy 1960]
• When memory runs out, GC executes two phases
• the mark phase: traces reachable objects
• the sweep phase: collects garbage objects
• Every object has an extra bit: the mark bit
• reserved for memory management
• initially the mark bit is 0
• set to 1 for the reachable objects in the mark phase
13
Monday, June 1, 2009
Mark and Sweep Example
14
!
Prof. Su ECS 142 Lecture 18 13
!"#$%"&'%()**+%,!-."#/01%23456
! "#$%&'$'()*&)+%,&(+-.&/0&$1$2+-$,&-3(4#5,$,6 -#$&'5)7&4#5,$8&-)52$,&)$52#59:$&(9;$2-,
6 -#$&,3$$4&4#5,$8&2(::$2-,&<5)95<$&(9;$2-,
! =>$)*&(9;$2-,&5%&$1-)5&9?-8&-#$&'5)7&9?-6 )$,$)>$@&A()&'$'()*&'5%5<$'$%-
6 ?%?-?5::*&-#$&'5)7&9?-&?,&B
6 ,$-&-(&C&A()&-#$&)$52#59:$&(9;$2-,&?%&-#$&'5)74#5,$
Prof. Su ECS 142 Lecture 18 14
!"#$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(A&/0&5:<()?-#',
! D&,$)?(+,&4)(9:$'&3?-#&-#$&'5)7&4#5,$6 ?-&?,&?%>(7$@&3#$%&3$&5)$&(+-&(A&,452$
6 *$-&?-&%$$@,&,452$&-(&2(%,-)+2-&-#$&-(@(&:?,-
6 -#$&,?R$&(A&-#$&-(@(&:?,-&?,&+%9(+%@$@&,(&3$&25%%(-)$,$)>$&,452$&A()&?-&5&4)?()?
Monday, June 1, 2009
The Mark Phaselet todo = { all roots } while todo ≠ ∅ do pick v ∈ todo
todo ← todo - { v } if mark(v) = 0 then (* v is unmarked yet *) mark(v) ← 1 let v1,...,vn be the pointers contained in v todo ← todo ∪ {v1,...,vn} fi od
15
Monday, June 1, 2009
The Sweep Phase
• The sweep phase scans the heap looking for objects with mark bit 0
• these objects have not been visited in the mark phase
• they are garbage
• Any such object is added to the free list
• The objects with a mark bit 1 have their mark bit reset to 0
16
Monday, June 1, 2009
The Sweep Phase (Cont.)
/* sizeof(p) is the size of block starting at p */ p ← bottom of heap while p < top of heap do if mark(p) = 1 then mark(p) ← 0 else add block p...(p+sizeof(p)-1) to freelist fi p ← p + sizeof(p) od
17
Monday, June 1, 2009
Details
• While conceptually simple, this algorithm has a number of tricky details
• this is typical of GC algorithms
• A serious problem with the mark phase
• it is invoked when we are out of space
• yet it needs space to construct the todo list
• the size of the todo list is unbounded so we cannot reserve space for it a priori
18
Monday, June 1, 2009
Mark and Sweep: Details
• The todo list is used as an auxiliary data structure to perform the reachability analysis
• There is a trick that allows the auxiliary data to be stored in the objects themselves
• pointer reversal: when a pointer is followed it is reversed to point to its parent
• Similarly, the free list is stored in the free objects themselves
19
Monday, June 1, 2009
Mark and Sweep. Evaluation
• Space for a new object is allocated from the new list
• a block large enough is picked
• an area of the necessary size is allocated from it
• the left-over is put back in the free list
• Mark and sweep can fragment the memory
• Advantage: objects are not moved during GC
• no need to update the pointers to objects
• works for languages like C and C++
20
Monday, June 1, 2009
Another Technique: Stop and Copy
• Memory is organized into two areas
• Old space: used for allocation
• New space: used as a reserve for GC
• The heap pointer points to the next free word in the old space
• Allocation just advances the heap pointer
21
old space new space
heap ptr.
Monday, June 1, 2009
Stop and Copy Garbage Collection
• Starts when the old space is full
• Copies all reachable objects from old space
• into new space
• garbage is left behind
• after the copy phase the new space uses less space than the old one before the collection
• After the copy the roles of the old and new spaces are reversed and the program resumes
22
Monday, June 1, 2009
Stop and Copy Garbage Collection Example
23
!
Prof. Su ECS 142 Lecture 18 19
!"#$%"&'%()**+,%-*."/01
! "#$%&'('%)*+&%*+%,+$(%-+%-.%-,/*)*-01%(-&-+&0,2&,0$%&'%3$04'05%&#$%0$-2#-6*)*&1%-.-)1+*+
! "#$0$%*+%-%&0*27%&#-&%-))'8+%&#$%-,/*)*-01%(-&-&'%6$%+&'0$(%*.%&#$%'69$2&+%&#$5+$):$+; 3'*.&$0%0$:$0+-)<%8#$.%-%3'*.&$0%*+%4'))'8$(%*&%*+0$:$0+$(%&'%3'*.&%&'%*&+%3-0$.&
! =*5*)-0)1>%&#$%40$$%)*+&%*+%+&'0$(%*.%&#$%40$$'69$2&+%&#$5+$):$+
Prof. Su ECS 142 Lecture 18 20
!"#$%"&'%()**+2%34"05"./6&
! =3-2$%4'0%-%.$8%'69$2&%*+%-))'2-&$(%40'5%&#$.$8%)*+&; -%6)'27%)-0?$%$.',?#%*+%3*27$(
; -.%-0$-%'4%&#$%.$2$++-01%+*@$%*+%-))'2-&$(%40'5%*&
; &#$%)$4&A':$0%*+%3,&%6-27%*.%&#$%40$$%)*+&
! B-07%-.(%+8$$3%2-.%40-?5$.&%&#$%5$5'01
! C(:-.&-?$<%'69$2&+%-0$%.'&%5':$(%(,0*.?%DE; .'%.$$(%&'%,3(-&$%&#$%3'*.&$0+%&'%'69$2&+
; 8'07+%4'0%)-.?,-?$+%)*7$%E%-.(%EFF
Prof. Su ECS 142 Lecture 18 21
7&6.8*#%9*:8&/;5*,%(.6+%"&'%<6+=
! B$5'01%*+%'0?-.*@$(%*.&'%&8'%-0$-+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?3'*.&$0%8$%7.'8%*&%8-+%-)0$-(1%2'3*$(
Monday, June 1, 2009
Implementation of Stop and Copy
• We need to find all the reachable objects, as for mark and sweep
• As we find a reachable object we copy it into the new space
• And we have to fix all pointers pointing to it!
• As we copy an object we store in the old copy a forwarding pointer to the new copy
• when we later reach an object with a forwarding pointer we know it was already copied
24
Monday, June 1, 2009
Implementation of Stop and Copy
• We still have the issue of how to implement the traversal without using extra space
• The following trick solves the problem:
• partition the new space in three contiguous regions
25
copied and scanned copied empty
copied objects whose pointer fields were followed and fixed
copied objects whose pointer fields were not followed
start scan alloc
Monday, June 1, 2009
Stop and Copy Example
26
Monday, June 1, 2009
Before Garbage Collection
27
!
Prof. Su ECS 142 Lecture 18 25
!"#$%"%&'(')*&+*,+-'*#+(&.+/*#0+1/*&'23
! "#$%&'(($)*+#$&)#$'%%,#$-.$)-/$&-$'01(#0#2&&)#$&3*+#3%*($/'&)-,&$,%'24$#5&3*$%1*6#
! 7)#$.-((-/'24$&3'68$%-(+#%$&)#$13-9(#0:; 1*3&'&'-2$&)#$2#/$%1*6#$'2$&)3##$6-2&'4,-,%$3#4'-2%
6-1'#<$*2<$%6*22#<
%6*2
6-1'#<$-9=#6&%$/)-%#$1-'2.'#(<%$/#3#$.-((-/#<*2<$.'5#<
6-1'#<$-9=#6&%$/)-%#$1-'2$.'#(<%$/#3#$>?7.-((-/#<
#01&@6-1'#<
*((-6%&*3&
Prof. Su ECS 142 Lecture 18 26
-'*#+(&.+/*#02+45("#$%+163
A B C D E3--& F 2#/%1*6#
! B#.-3#$4*39*4#$6-((#6&'-2
%&*3&%6*2*((-6
Prof. Su ECS 142 Lecture 18 27
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$H:$C-1@$&)#$-9=#6&%$1-'2&#<$9@$3--&%$*2<%#&$.-3/*3<'24$1-'2%$I<-&&#<$*33-/J
A
%&*3&%6*2
*((-6
Prof. Su ECS 142 Lecture 18 28
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$K:$E-((-/$&)#$1-'2$'2$&)#$2#5&,2%6*22#<$-9=#6&$IAJ; 6-1@$&)#$1-'2&#<$-9=#6&%$I=,%&$C$'2$&)'%$6*%#J
; .'5$&)#$1-'2$'2$A
; %#&$.-3/*3<'24$1-'2
A
%6*2*((-6
C
%&*3&
Prof. Su ECS 142 Lecture 18 29
-'*#+(&.+/*#02+45("#$%+183
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$ICJ; 6-1@$&)#$1-'2&#<$-9=#6&%$IE$'2$&)'%$6*%#J
A
%6*2*((-6
C E
%&*3&
Prof. Su ECS 142 Lecture 18 30
-'*#+(&.+/*#02+45("#$%+193
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$IEJ; &)#$1-'2&#<$-9=#6&$IAJ$/*%$*(3#*<@$6-1'#<L$G#&$&)#1-'2$%*0#$*%$&)#$.-3/*<'24$1-'2
A
%6*2*((-6
C E
%&*3&
Monday, June 1, 2009
Step 1: Copy objects pointed to by roots and set forwarding ptrs. (dotted arrows)
28
!
Prof. Su ECS 142 Lecture 18 25
!"#$%"%&'(')*&+*,+-'*#+(&.+/*#0+1/*&'23
! "#$%&'(($)*+#$&)#$'%%,#$-.$)-/$&-$'01(#0#2&&)#$&3*+#3%*($/'&)-,&$,%'24$#5&3*$%1*6#
! 7)#$.-((-/'24$&3'68$%-(+#%$&)#$13-9(#0:; 1*3&'&'-2$&)#$2#/$%1*6#$'2$&)3##$6-2&'4,-,%$3#4'-2%
6-1'#<$*2<$%6*22#<
%6*2
6-1'#<$-9=#6&%$/)-%#$1-'2.'#(<%$/#3#$.-((-/#<*2<$.'5#<
6-1'#<$-9=#6&%$/)-%#$1-'2$.'#(<%$/#3#$>?7.-((-/#<
#01&@6-1'#<
*((-6%&*3&
Prof. Su ECS 142 Lecture 18 26
-'*#+(&.+/*#02+45("#$%+163
A B C D E3--& F 2#/%1*6#
! B#.-3#$4*39*4#$6-((#6&'-2
%&*3&%6*2*((-6
Prof. Su ECS 142 Lecture 18 27
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$H:$C-1@$&)#$-9=#6&%$1-'2&#<$9@$3--&%$*2<%#&$.-3/*3<'24$1-'2%$I<-&&#<$*33-/J
A
%&*3&%6*2
*((-6
Prof. Su ECS 142 Lecture 18 28
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$K:$E-((-/$&)#$1-'2$'2$&)#$2#5&,2%6*22#<$-9=#6&$IAJ; 6-1@$&)#$1-'2&#<$-9=#6&%$I=,%&$C$'2$&)'%$6*%#J
; .'5$&)#$1-'2$'2$A
; %#&$.-3/*3<'24$1-'2
A
%6*2*((-6
C
%&*3&
Prof. Su ECS 142 Lecture 18 29
-'*#+(&.+/*#02+45("#$%+183
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$ICJ; 6-1@$&)#$1-'2&#<$-9=#6&%$IE$'2$&)'%$6*%#J
A
%6*2*((-6
C E
%&*3&
Prof. Su ECS 142 Lecture 18 30
-'*#+(&.+/*#02+45("#$%+193
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$IEJ; &)#$1-'2&#<$-9=#6&$IAJ$/*%$*(3#*<@$6-1'#<L$G#&$&)#1-'2$%*0#$*%$&)#$.-3/*<'24$1-'2
A
%6*2*((-6
C E
%&*3&
Monday, June 1, 2009
Step 2: Follow the ptr in the next unscanned object-Copy the ptd objects (C)-Fix the ptr in A-set forwarding ptr
29
!
Prof. Su ECS 142 Lecture 18 25
!"#$%"%&'(')*&+*,+-'*#+(&.+/*#0+1/*&'23
! "#$%&'(($)*+#$&)#$'%%,#$-.$)-/$&-$'01(#0#2&&)#$&3*+#3%*($/'&)-,&$,%'24$#5&3*$%1*6#
! 7)#$.-((-/'24$&3'68$%-(+#%$&)#$13-9(#0:; 1*3&'&'-2$&)#$2#/$%1*6#$'2$&)3##$6-2&'4,-,%$3#4'-2%
6-1'#<$*2<$%6*22#<
%6*2
6-1'#<$-9=#6&%$/)-%#$1-'2.'#(<%$/#3#$.-((-/#<*2<$.'5#<
6-1'#<$-9=#6&%$/)-%#$1-'2$.'#(<%$/#3#$>?7.-((-/#<
#01&@6-1'#<
*((-6%&*3&
Prof. Su ECS 142 Lecture 18 26
-'*#+(&.+/*#02+45("#$%+163
A B C D E3--& F 2#/%1*6#
! B#.-3#$4*39*4#$6-((#6&'-2
%&*3&%6*2*((-6
Prof. Su ECS 142 Lecture 18 27
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$H:$C-1@$&)#$-9=#6&%$1-'2&#<$9@$3--&%$*2<%#&$.-3/*3<'24$1-'2%$I<-&&#<$*33-/J
A
%&*3&%6*2
*((-6
Prof. Su ECS 142 Lecture 18 28
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$K:$E-((-/$&)#$1-'2$'2$&)#$2#5&,2%6*22#<$-9=#6&$IAJ; 6-1@$&)#$1-'2&#<$-9=#6&%$I=,%&$C$'2$&)'%$6*%#J
; .'5$&)#$1-'2$'2$A
; %#&$.-3/*3<'24$1-'2
A
%6*2*((-6
C
%&*3&
Prof. Su ECS 142 Lecture 18 29
-'*#+(&.+/*#02+45("#$%+183
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$ICJ; 6-1@$&)#$1-'2&#<$-9=#6&%$IE$'2$&)'%$6*%#J
A
%6*2*((-6
C E
%&*3&
Prof. Su ECS 142 Lecture 18 30
-'*#+(&.+/*#02+45("#$%+193
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$IEJ; &)#$1-'2&#<$-9=#6&$IAJ$/*%$*(3#*<@$6-1'#<L$G#&$&)#1-'2$%*0#$*%$&)#$.-3/*<'24$1-'2
A
%6*2*((-6
C E
%&*3&
Monday, June 1, 2009
Step 3: Follow the ptr in the next unscanned object (C)-copy the pointed objects (F)
30
!
Prof. Su ECS 142 Lecture 18 25
!"#$%"%&'(')*&+*,+-'*#+(&.+/*#0+1/*&'23
! "#$%&'(($)*+#$&)#$'%%,#$-.$)-/$&-$'01(#0#2&&)#$&3*+#3%*($/'&)-,&$,%'24$#5&3*$%1*6#
! 7)#$.-((-/'24$&3'68$%-(+#%$&)#$13-9(#0:; 1*3&'&'-2$&)#$2#/$%1*6#$'2$&)3##$6-2&'4,-,%$3#4'-2%
6-1'#<$*2<$%6*22#<
%6*2
6-1'#<$-9=#6&%$/)-%#$1-'2.'#(<%$/#3#$.-((-/#<*2<$.'5#<
6-1'#<$-9=#6&%$/)-%#$1-'2$.'#(<%$/#3#$>?7.-((-/#<
#01&@6-1'#<
*((-6%&*3&
Prof. Su ECS 142 Lecture 18 26
-'*#+(&.+/*#02+45("#$%+163
A B C D E3--& F 2#/%1*6#
! B#.-3#$4*39*4#$6-((#6&'-2
%&*3&%6*2*((-6
Prof. Su ECS 142 Lecture 18 27
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$H:$C-1@$&)#$-9=#6&%$1-'2&#<$9@$3--&%$*2<%#&$.-3/*3<'24$1-'2%$I<-&&#<$*33-/J
A
%&*3&%6*2
*((-6
Prof. Su ECS 142 Lecture 18 28
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$K:$E-((-/$&)#$1-'2$'2$&)#$2#5&,2%6*22#<$-9=#6&$IAJ; 6-1@$&)#$1-'2&#<$-9=#6&%$I=,%&$C$'2$&)'%$6*%#J
; .'5$&)#$1-'2$'2$A
; %#&$.-3/*3<'24$1-'2
A
%6*2*((-6
C
%&*3&
Prof. Su ECS 142 Lecture 18 29
-'*#+(&.+/*#02+45("#$%+183
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$ICJ; 6-1@$&)#$1-'2&#<$-9=#6&%$IE$'2$&)'%$6*%#J
A
%6*2*((-6
C E
%&*3&
Prof. Su ECS 142 Lecture 18 30
-'*#+(&.+/*#02+45("#$%+193
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$IEJ; &)#$1-'2&#<$-9=#6&$IAJ$/*%$*(3#*<@$6-1'#<L$G#&$&)#1-'2$%*0#$*%$&)#$.-3/*<'24$1-'2
A
%6*2*((-6
C E
%&*3&
Monday, June 1, 2009
Step 4: Follow the ptr in the next unscanned object (F)-The ptd object (A) was already copied. Set the ptr the same as the fwding ptr
31
!
Prof. Su ECS 142 Lecture 18 25
!"#$%"%&'(')*&+*,+-'*#+(&.+/*#0+1/*&'23
! "#$%&'(($)*+#$&)#$'%%,#$-.$)-/$&-$'01(#0#2&&)#$&3*+#3%*($/'&)-,&$,%'24$#5&3*$%1*6#
! 7)#$.-((-/'24$&3'68$%-(+#%$&)#$13-9(#0:; 1*3&'&'-2$&)#$2#/$%1*6#$'2$&)3##$6-2&'4,-,%$3#4'-2%
6-1'#<$*2<$%6*22#<
%6*2
6-1'#<$-9=#6&%$/)-%#$1-'2.'#(<%$/#3#$.-((-/#<*2<$.'5#<
6-1'#<$-9=#6&%$/)-%#$1-'2$.'#(<%$/#3#$>?7.-((-/#<
#01&@6-1'#<
*((-6%&*3&
Prof. Su ECS 142 Lecture 18 26
-'*#+(&.+/*#02+45("#$%+163
A B C D E3--& F 2#/%1*6#
! B#.-3#$4*39*4#$6-((#6&'-2
%&*3&%6*2*((-6
Prof. Su ECS 142 Lecture 18 27
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$H:$C-1@$&)#$-9=#6&%$1-'2&#<$9@$3--&%$*2<%#&$.-3/*3<'24$1-'2%$I<-&&#<$*33-/J
A
%&*3&%6*2
*((-6
Prof. Su ECS 142 Lecture 18 28
-'*#+(&.+/*#02+45("#$%+173
A B C D E3--& F
! G$K:$E-((-/$&)#$1-'2$'2$&)#$2#5&,2%6*22#<$-9=#6&$IAJ; 6-1@$&)#$1-'2&#<$-9=#6&%$I=,%&$C$'2$&)'%$6*%#J
; .'5$&)#$1-'2$'2$A
; %#&$.-3/*3<'24$1-'2
A
%6*2*((-6
C
%&*3&
Prof. Su ECS 142 Lecture 18 29
-'*#+(&.+/*#02+45("#$%+183
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$ICJ; 6-1@$&)#$1-'2&#<$-9=#6&%$IE$'2$&)'%$6*%#J
A
%6*2*((-6
C E
%&*3&
Prof. Su ECS 142 Lecture 18 30
-'*#+(&.+/*#02+45("#$%+193
A B C D E3--& F
! E-((-/$&)#$1-'2$'2$&)#$2#5&$,2%6*22#<-9=#6&$IEJ; &)#$1-'2&#<$-9=#6&$IAJ$/*%$*(3#*<@$6-1'#<L$G#&$&)#1-'2$%*0#$*%$&)#$.-3/*<'24$1-'2
A
%6*2*((-6
C E
%&*3&
Monday, June 1, 2009
!
Prof. Su ECS 142 Lecture 18 31
!"#$%&'(%)#$*+%,-&.$/0%123
!""#
$ %&'()*+(,'*(,-./#*-0*1&#/*,22"(*1)*,!)*3"')
$ %1,0*#/)*!"2)*"4*#/)*+0,()+*,'3*!)+-5)*#/)0!".!,5
6
+(,',22"(
7 8')1*+0,()
Prof. Su ECS 142 Lecture 18 32
450%!"#$%&'(%)#$*%6/7#89"5.
1/&2)*+(,'*9:*,22"(*3"*****2)#*;*<)*#/)*"<=)(#*,#*+(,'*0"&'#)!*****4"!*),(/*0"&'#)!*0*("'#,&')3*&'*;*3"*********4&'3*;>*#/,#*0*0"&'#+*#"*********&4*;>*&+*1&#/"-#*,*4"!1,!3&'.*0"&'#)!***************("0?*;>*#"*')1*+0,()*@-03,#)*,22"(*0"&'#)!A***************+)#*B+#*1"!3*"4*"23*;>*#"*0"&'#*#"*#/)*')1*("0?***************(/,'.)*0*#"*0"&'#*#"*#/)*')1*("0?*"4*;>*********)2+)***************+)#*0*&'*;*)C-,2*#"*#/)*4"!1,!3&'.*0"&'#)!*********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<&',!?*#!))*'"3)*@1&#/*,*2)4#*,'3*!&./#4&)23+AR
G I/-+*1)*(,''"#*#)22*1/)!)*,22*#/)*0"&'#)!+*,!)
Prof. Su ECS 142 Lecture 18 36
)#';08<&"9<0%B&8C&70%)#//0D"9#'
$ N-#*&#*&+*;E*#"*<)*("'+)!J,#&J)SG P4*,*5)5"!?*1"!3*2""E+*2&E)*,*0"&'#)!*&#*&+("'+&3)!)3*,*0"&'#)!$ &#*5-+#*<)*,2&.')3
$ &#*5-+#*0"&'#*#"*,*J,2&3*,33!)++*&'*#/)*3,#,*+).5)'#
G 622*+-(/*0"&'#)!+*,!)*4"22"1)3*,'3*1)*"J)!)+#&5,#)#/)*!),(/,<2)*"<=)(#+
$ N-#*1)*+#&22*(,''"#*5"J)*"<=)(#+*<)(,-+)*1)(,''"#*-03,#)*0"&'#)!+*#"*#/)5G H/,#*&4*1/,#*1)*#/"-./#*#"*<)*,*0"&'#)!*&+*,(#-,22?,'*,(("-'#*'-5<)!R
Done.
-Scan caught up with alloc-Swap the role of the spaces and resume the program
32
Monday, June 1, 2009
The Stop and Copy Algorithm
33
while scan <> alloc do let O be the object at scan pointer for each pointer p contained in O do find O’ that p points to if O’ is without a forwarding pointer copy O’ to new space (update alloc pointer) set 1st word of old O’ to point to the new copy change p to point to the new copy of O’ else set p in O equal to the forwarding pointer fi end for increment scan pointer to the next object od
Monday, June 1, 2009
Stop and Copy. Details.
• As with mark and sweep, we must be able to tell how large is an object when we scan it
• And we must also know where are the pointers inside the object
• We must also copy any objects pointed to by the stack and update pointers in the stack
• This can be an expensive operation
34
Monday, June 1, 2009
Stop and Copy. Evaluation
• Stop and copy is generally believed to be the fastest GC technique
• Allocation is very cheap
• Just increment the heap pointer
• Collection is relatively cheap
• Especially if there is a lot of garbage
• Only touch reachable objects
• But some languages do not allow copying (C, C++)
35
Monday, June 1, 2009
Why Doesn’t C Allow Copying?
• Garbage collection relies on being able to find all reachable objects
• And it needs to find all pointers in an object
• In C or C++ it is impossible to identify the contents of objects in memory
• E.g., how can you tell that a sequence of two memory words is a list cell (with data and next fields) rather than a binary tree node (with a left and right fields)?
• Thus we cannot tell where all the pointers are
36
Monday, June 1, 2009
Conservative Garbage Collection
• But it is ok to be conservative in identifying reachable objects:
• If a memory word looks like a pointer it is considered a pointer
• it must be aligned
• it must point to a valid address in the data segment
• All such pointers are followed and we overestimate the reachable objects
• But we still cannot move objects because we cannot update pointers to them
• What if what we thought to be a pointer is actually an account number?
37
Monday, June 1, 2009
Reference Counting [Collins 1960]
• Rather that wait for memory to be exhausted, try to collect an object when there are no more pointers to it
• Store in each object the number of pointers to that object
• This is the reference count
• Each assignment operation has to manipulate the reference count
38
Monday, June 1, 2009
Implementation of Reference Counting
• new returns an object with a reference count of 1
• If x points to an object then let rc(x) refer to the object’s reference count
• Every assignment x ← y must be changed:
rc(y) ← rc(y) + 1
rc(x) ← rc(x) - 1 if(rc(x) == 0) then mark x as free
x ← y
39
Monday, June 1, 2009
Reference Counting. Evaluation
• Advantages:
• Easy to implement
• Collects garbage incrementally without large pauses in the execution
• Disadvantages:
• Manipulating reference counts at each assignment is very slow
• Cannot collect circular structures
40
Monday, June 1, 2009
Garbage Collection. Evaluation
• Automatic memory management avoids some serious storage bugs
• But it takes away control from the programmer
• e.g., layout of data in memory
• e.g., when is memory deallocated
• Most garbage collection implementation stop the execution during collection
• not acceptable in real-time applications
41
Monday, June 1, 2009
Garbage Collection. Evaluation
• Garbage collection is going to be around for a while
• Researchers are working on advanced garbage collection algorithms:
• Concurrent: allow the program to run while the collection is happening
• Generational: do not scan long-lived objects at every collection (JVM uses this kind)
• Parallel: several collectors working in parallel
42
Monday, June 1, 2009
Work on Project 4!
43
Monday, June 1, 2009