



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Professor: Peisert; Class: Compilers; Subject: Engineering Computer Science; University: University of California - Davis; Term: Spring 2009;
Typology: Study Guides, Projects, Research
1 / 43
This page cannot be seen from the preview
Don't miss anything!




































Dr. Sean Peisert – ECS 142 – Spring 2009 1
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
Type Safety and Memory Management
4
Automatic Memory Management
There are several well-known techniques for performing completely automatic memory management Now in Java, scripting languages, etc... 5
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
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
Tracing Reachable Values in Coolc
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
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 9 9 N3%#./)&%N%04%()2)-%6#0(6%#%.)%34) 9 0>%$)%O(#$% <#0()-4%0(% Prof. Su ECS 142 Lecture 18 11 :$@(=;8#$A<&=;8# ! :(%M##+&%$)%4'-%-'&0(6%>-#;%'&&%'(5%4'&O 9 ,)?%'-)%&'++)5%,)%-## ! Q#)%,'%N%'(5%R%'-)%(#%-)'&,'.+)%>-#;%'&& #-%,)%4'&O ! L,34%$)%&'(%-)34)%,)0-%4#-'6) E N M S-';)%T S-';)%U '&& R V WX A8#=#/0'$.B$ ! V2)-?%6'-.'
#++#$0(6%4* TH E++#&')%4< UH =,)(%4<'& 'A M#;<3) @6)()-'+ 4)%#>%1- .A S-))%,) ! W#;)%4-') .)>#-)%*,)%
Mark and Sweep [McCarthy 1960]
Every object has an extra bit: the mark bit set to 1 for the reachable objects in the mark phase 13
Mark and Sweep Example 14 Prof. Su ECS 142 Lecture 18 14
)((- D E 0 F = G A)$$ B B^ B^ B^ B^ B )((- D E 0 F = G A)$$ C B^ C^ B^ B^ C DA-$)&'5) )((- D^ E 0 F = G A)$$ B B^ B^ B^ B^ B DA-$)&,3$$
16
The Sweep Phase (Cont.)
17
Mark and Sweep: Details
pointer reversal : when a pointer is followed it is reversed to point to its parent
Mark and Sweep. Evaluation
Mark and sweep can fragment the memory works for languages like C and C++ 20