Automatic Memory Management – Lecture Slides | ECS 142, Study Guides, Projects, Research of Computer Science

Material Type: Project; Professor: Peisert; Class: Compilers; Subject: Engineering Computer Science; University: University of California - Davis; Term: Spring 2009;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 09/17/2009

koofers-user-z2j-1
koofers-user-z2j-1 🇺🇸

10 documents

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Automatic Memory Management – Lecture Slides | ECS 142 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Automatic Memory Management

Lecture 27

Dr. Sean Peisert – ECS 142 – Spring 2009 1

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

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

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

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

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

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

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 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]

  • 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

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$$

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

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

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

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