1 / 61

Garbage Collection Introduction and Overview

Garbage Collection Introduction and Overview. Christian Schulte Programming Systems Lab Universität des Saarlandes, Germany schulte@ps.uni-sb.de. Purpose of Talk. Explaining basic concepts terminology Garbage collection… …is simple …can be explained at a high-level Organization.

jendayi
Download Presentation

Garbage Collection Introduction and Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Garbage Collection Introduction and Overview Christian Schulte Programming Systems Lab Universität des Saarlandes, Germany schulte@ps.uni-sb.de

  2. Purpose of Talk • Explaining basic • concepts • terminology • Garbage collection… • …is simple • …can be explained at a high-level • Organization

  3. Purpose of Talk • Explaining basic • concepts • terminology (never to be explained again) • Garbage collection… • …is simple • …can be explained at a high-level • Organization

  4. Overview • What is garbage collection • objects of interest • principal notions • classic examples with assumptions and properties • Discussion • software engineering issues • typical cost • areas of usage • why knowledge is profitable • Organizational • Material • Requirements

  5. Overview • What is garbage collection • objects of interest • principal notions • classic examples with assumptions and properties • Discussion • software engineering issues • typical cost • areas of usage • why knowledge is profitable • Organizational • Material • Requirements

  6. Garbage Collection… …is concerned with the automatic reclamation of dynamically allocated memory after its last use by a program

  7. Garbage Collection… • dynamically allocated memory …is concerned with the automatic reclamation of dynamically allocated memory after its last use by a program

  8. Garbage Collection… • dynamically allocated memory • last use by a program …is concerned with the automatic reclamation of dynamically allocated memory after its last use by a program

  9. Garbage Collection… • dynamically allocated memory • last use by a program • automatic reclamation …is concerned with the automatic reclamation of dynamically allocated memory after its last use by a program

  10. Garbage collection… • Dynamically allocated memory • Last use by a program • Examples for automatic reclamation

  11. Kinds of Memory Allocation static int i; void foo(void) { int j; int* p = (int*) malloc(…); }

  12. Static Allocation • By compiler (in text area) • Available through entire runtime • Fixed size static int i; void foo(void) { int j; int* p = (int*) malloc(…); }

  13. Automatic Allocation • Upon procedure call (on stack) • Available during execution of call • Fixed size static int i; void foo(void) { int j; int* p = (int*) malloc(…); }

  14. Dynamic Allocation • Dynamically allocated at runtime (on heap) • Available until explicitly deallocated • Dynamically varying size static int i; void foo(void) { int j; int* p = (int*) malloc(…); }

  15. Dynamically Allocated Memory • Also: heap-allocated memory • Allocation: malloc, new, … • before first usage • Deallocation: free, delete, dispose, … • after last usage • Needed for • C++, Java: objects • SML: datatypes, procedures • anything that outlives procedure call

  16. Getting it Wrong • Forget to free (memory leak) • program eventually runs out of memory • long running programs: OSs. servers, … • Free to early (dangling pointer) • lucky: illegal access detected by OS • horror: memory reused, in simultaneous use • programs can behave arbitrarily • crashes might happen much later • Estimates of effort • Up to 40%! [Rovner, 1985]

  17. p Nodes and Pointers • Node n • Memory block, cell • Pointer p • Link to node • Node access: *p • Children children(n) • set of pointers to nodes referred by n n

  18. Mutator • Abstraction of program • introduces new nodes with pointer • redirects pointers, creating garbage

  19. Shared Nodes • Nodes referred to by several pointers • Makes manual deallocation hard • local decision impossible • respect other pointers to node • Cycles instance of sharing

  20. Garbage collection… • Dynamically allocated memory • Last use by a program • Examples for automatic reclamation

  21. Last Use by a Program • Question: When is node M not any longer used by program? • Let P be any program not using M • New program sketch: Execute P; Use M; • Hence: M used  P terminates • We are doomed: halting problem! • So “last use” undecidable!

  22. Safe Approximation • Decidable and also simple • What means safe? • only unused nodes freed • What means approximation? • some unused nodes might not be freed • Idea • nodes that can be accessed by mutator

  23. Reachable Nodes root • Reachable from root set • processor registers • static variables • automatic variables (stack) • Reachable from reachable nodes

  24. Summary: Reachable Nodes • A node n is reachable, iff • n is element of the root set, or • n is element of children(m) and m is reachable • Reachable node also called “live”

  25. MyGarbageCollector • Compute set of reachable nodes • Free nodes known to be not reachable • Known as mark-sweep • in a second…

  26. Reachability: Safe Approximation • Safe • access to not reachable node impossible • depends on language semantics • but C/C++? later… • Approximation • reachable node might never be accessed • programmer must know about this! • have you been aware of this?

  27. Garbage collection… • Dynamically allocated memory • Last use by a program • Examples for automatic reclamation

  28. Example Garbage Collectors • Mark-Sweep • Others • Mark-Compact • Reference Counting • Copying • skipped here • read Chapter 1&2 of [Lins&Jones,96]

  29. The Mark-Sweep Collector • Compute reachable nodes: Mark • tracing garbage collector • Free not reachable nodes: Sweep • Run when out of memory: Allocation • First used with LISP [McCarthy, 1960]

  30. Allocation node* new() { if (free_pool is empty) mark_sweep(); …

  31. Allocation node* new() { if (free_pool is empty) mark_sweep(); return allocate(); }

  32. The Garbage Collector void mark_sweep() { for (r in roots) mark(r); …

  33. The Garbage Collector void mark_sweep() { for (r in roots) mark(r); … all live nodes marked

  34. Recursive Marking void mark(node* n) { if (!is_marked(n)) { set_mark(n); … } }

  35. Recursive Marking void mark(node* n) { if (!is_marked(n)) { set_mark(n); … } } nodes reachable from n marked

  36. Recursive Marking void mark(node* n) { if (!is_marked(n)) { set_mark(n); for (m in children(n)) mark(m); } } i-th recursion: nodes on path with length i marked

  37. The Garbage Collector void mark_sweep() { for (r in roots) mark(r); sweep(); …

  38. The Garbage Collector void mark_sweep() { for (r in roots) mark(r); sweep(); … all nodes on heap live

  39. The Garbage Collector void mark_sweep() { for (r in roots) mark(r); sweep(); … all nodes on heap live and not marked

  40. Eager Sweep void sweep() { node* n = heap_bottom; while (n < heap_top) { … } }

  41. Eager Sweep void sweep() { node* n = heap_bottom; while (n < heap_top) { if (is_marked(n)) clear_mark(n); else free(n); n += sizeof(*n); } }

  42. The Garbage Collector void mark_sweep() { for (r in roots) mark(r); sweep(); if (free_pool is empty) abort(“Memory exhausted”); }

  43. Assumptions • Nodes can be marked • Size of nodes known • Heap contiguous • Memory for recursion available • Child fields known!

  44. Assumptions: Realistic • Nodes can be marked • Size of nodes known • Heap contiguous • Memory for recursion available • Child fields known

  45. Assumptions: Conservative • Nodes can be marked • Size of nodes known • Heap contiguous • Memory for recursion available • Child fields known

  46. Mark-Sweep Properties • Covers cycles and sharing • Time depends on • live nodes (mark) • live and garbage nodes (sweep) • Computation must be stopped • non-interruptible stop/start collector • long pause • Nodes remain unchanged (as not moved) • Heap remains fragmented

  47. Variations of Mark-Sweep • In your talk…

  48. Implementation • In your talk…

  49. Efficiency Analysis • In your talk…

  50. Comparison • In your talk…

More Related