lecture17

lecture17 - Lecture Outine Why Automatic Memory Management?...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Prof. Aiken CS 143 Lecture 17 1 Automatic Memory Management Lecture 17 Prof. Aiken CS 143 Lecture 17 2 Lecture Outine • Why Automatic Memory Management? • Garbage Collection • Three Techniques – Mark and Sweep – Stop and Copy – Reference Counting Prof. Aiken CS 143 Lecture 17 3 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 Prof. Aiken CS 143 Lecture 17 4 Type Safety and Memory Management • 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 • Currently, if you want type safety then you must use automatic memory management Prof. Aiken CS 143 Lecture 17 5 Automatic Memory Management • This is an old problem: – studied since the 1950s for LISP • There are well-known techniques for completely automatic memory management • Became mainstream with the popularity of Java Prof. Aiken CS 143 Lecture 17 6 The Basic Idea When an object 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Prof. Aiken CS 143 Lecture 17 7 The Basic Idea (Cont.) • How can we tell whether an object will “never be used again”? – in general, impossible to tell – we will use heuristics • 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 Prof. Aiken CS 143 Lecture 17 8 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 be used – such objects are garbage Prof. Aiken CS 143 Lecture 17 9 Reachability is an Approximation • Consider the program: x new A; y new B x y; if alwaysTrue() then x new A else x.foo() fi • After x y (assuming y becomes dead there) – the first object A is unreachable – 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 Prof. Aiken CS 143 Lecture 17 10 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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

lecture17 - Lecture Outine Why Automatic Memory Management?...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online