Automatic Memory Management Noam Rinetzky Schreiber 123A /seminar/seminar1415a.html Semester A. Tuesday, 14:00-16:00. Schreiber 7
Scope Automatic algorithms for automatic memory management aka garbage collection – Sequential GC – Parallel GC – Concurrent GC – Real-time GC Focus on correctness
Programs with dynamic memory Programs manipulate resources – Files – Processes / threads – Connections – Memory malloc() / new() free() / delete() …. GC
Programming with dynamic memory typedef struct Data {int d; struct Data *n} Da; main(){ Da *p1 = (Da*) malloc(sizeof(Da)); p1 d = SECRET_KEY; Da *p2 = (Da*) malloc(sizeof(Da)); p2 d = 0; p1 n = p2; p2 n = null; free(p1); free(p2) }
Common mistakes Double free Memory leaks (no-free) Accessing dangling references Null-dereference Breaking invariants – p1 n = p2; p2 n = null; – p1 n = p2; p2 n = p1;
Undesired outcome… Crashes Incorrect behavior Security vulnerabilities Loss of life Loss of money Loss of reputation Loss of Job
Deallocation Allocation is “easy” “Deletion” is hard Nasty bugs Hard to get right – Defensive programming
Controlled Solutions Manual memory management – Runtime: Monitoring execution environment Catches errors Expensive – Compile-time: Verify memory safety Static analysis – Fully automatic / User-provided annotations Conservative – Problem is undecidable
Automatic Memory Management Exploit global knowledge – Hard to de-allocate based on local reasoning Simplifies code, Reduce coupling, Reduces errors, costs Sensitive & Chaotic (Locality, Program)
GC for the rescue Double free Memory leaks (no-free) Accessing dangling references Null-dereference Breaking invariants – p1 n = p2; p2 n = null; – p1 n = p2; p2 n = p1;
GC Runtime environment recycles memory that will not be used in the future of the execution – Cannot be used = unreachable Pros – Safe – Simple Cons – Runtime overhead – Imprecision (drag)
GC Challenges Unbounded number of resources Complicated data structures Efficiency Precision Correctness Multithreading makes things worse!
Comparing GC Algorithms Safety Throughput Completeness and promptness – Pause Space overhead Language-specific optimizations Scalability and portability
More issues Performance overhead Experimental methodology
Terminology Heap Mutator & Collector Mutator roots References, fields, addresses Liveness, correctness, reachability Allocator
Schedule ChapterTopicDateLesson Chap.1Overview Chap. 2,3Mark-and-Sweep and Mark-compact GC Chap. 4,5Copying GC and reference counting Chap. 6,7Comparing GCs and allocation Chap. 8,9,10Partitioning and generational GC Chap. 11,12Runtime interface and language specific concerns Chap. 13Concurrency preliminaries Chap. 14Parallel GC Chap. 15Concurrent GC Chap. 16Concurrent mark-sweep GC Chap. 17Concurrent copying and compaction GC Chap. 18Concurrent reference-counting Chap. 19Real-time GC
Admin
Requirements You are required to be present in every lesson – unless coordinated ahead with the lecturer Meet me before lecture – Sunday , Schreiber 123A
Requirements Give a 80 minutes talk about his or hers assigned topic. Answer students questions during the talk. Say something original Lead a discussion a summary discussion. Write a short (1 page) summary Participate in the discussions
Grades 70% Presentation 5% Original insight 10% Participation 15% Attendance
Paper Title Names of Authors Your Name + date
Outline of talk Introduction Suggested Solution Evaluation Related work Conclusions Your own conclusions
Introduction Problem area Technical challenged addressed Why is it important What is the main insight How is main insight utilized (high level)
Solution Technical description – Algorithm – Correctness – Complexity Choose key subset of details Use examples + diagrams
Evaluation Experiments Benchmarks Conclusions
Related work What other solutions are out there How do they compare – Pros – Cons
Conclusions What was done Why is it important Novel idea What we learned
Your own conclusion Surprise me