lec26 - CS 3110 Recitation 21 Automatic Garbage Collection...

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

View Full Document Right Arrow Icon
CS 3110 Recitation 21 Automatic Garbage Collection Garbage In OCaml programs (and in most other programming languages), it is possible to create garbage : allocated space that is no longer usable by the program. For example, consider this code: let x =[[1;2;3];[4]] in let y = [2]::List.tl x in y Here the variable x is bound to a list of two elements, each of which is itself a list. Then the variable y is bound to a list that drops the first element of x and adds a different first element and the function returns this new list. Since x is never used again that first list is inaccessible, or garbage. Reachability Any boxed value created by OCaml can become garbage. This includes tuples, records, strings, arrays, lists and function closures as well as most user-defined data types. Most garbage collectors are based on the idea of reclaiming whole blocks that are no longer reachable from a set of roots , which are pointers into the heap that are assumed to always be accessible. The roots of a given computation consist of pointers that appear in the environment, plus the pointer to the currently computed result. A block of memory is reachable from the roots if there is a direct pointer to that block among the roots, or if there is a pointer to that block in another block that is reachable from the roots. Looking a memory more abstractly, we see that the memory heap is simply a directed graph in which the nodes are blocks of memory and the edges are the pointers between these blocks. So reachability can be computed as a graph traversal. Explicit vs. automatic garbage collection There are two basic strategies for dealing with garbage: explicit garbage collection by the programmer, and automatic garbage collection built into the language run-time system. Explicit garbage collection is provided by languages like C and C++. There is a way to explicitly deallocate (or "free") allocated memory when it is expected that that memory is about to become garbage. Languages like Java and OCaml provide automatic garbage collection : the system automatically identifies blocks of memory that can never be used again by the program, and reclaims their space for use by later allocations. Automatic garbage collection offers the advantage that the programmer does not have to worry about when to deallocate a given block of memory. In languages like C the need to explicitly manage memory complicates any code that allocates data on the heap, and is a significant burden on the programmer. Worse, if the programmer fails to deallocate properly, bugs are introduced into the program that are hard to find: If the programmer neglects to deallocate some garbage, it creates a memory leaks in which some
Background image of page 1

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

View Full DocumentRight Arrow Icon
allocated memory can never again be reused. This is a program for long-running programs which will tends to grow in size until they consume all of memory. If the programmer is too aggressive and deallocates a block of memory that is still in use, this creates
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.

This note was uploaded on 10/25/2009 for the course PHYS 2214 at Cornell.

Page1 / 8

lec26 - CS 3110 Recitation 21 Automatic Garbage Collection...

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