24_garbage_collection - CSCI-1200 Data Structures Fall 2010...

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

View Full Document Right Arrow Icon
CSCI-1200 Data Structures — Fall 2010 Lecture 24 — Garbage Collection Review from Lecture 23 Basic exception mechanisms: try / throw / catch STL exception s RAII “Resource Acquisition is Initialization” Structured Exception Handling in the Windows Operating System Google’s C++ Style Guide on Exceptions Today’s Class: Garbage Collection What is garbage ? Not everything sitting in memory is useful. Garbage is anything that cannot have any influence on the future computation. With C++, the programmer is expected to perform explicit memory management . You must use delete when you are done with dynamically allocated memory (which was created with new ). In Java, and other languages with “garbage collection”, you are not required to explicitly de-allocate the memory. The system automatically determines what is garbage and returns it to the available pool of memory. Certainly this makes it easier to learn to program in these languages, but automatic memory management does have performance and memory usage disadvantages. Today we’ll overview 3 basic techniques for automatic memory management. 24.1 The Node class For our discussion today, we’ll assume that all program data is stored in dynamically-allocated instances of the following simple class. This class can be used to build linked lists, trees, and graphs with cycles: class Node { public: Node(char v, Node* l, Node* r) : value(v), left(l), right(r) {} char value; Node* left; Node* right; }; 24.2 Garbage Collection Technique #1: Reference Counting 1. Attach a counter to each Node in memory. 2. When a new pointer is connected to that Node , increment the counter. 3. When a pointer is removed, decrement the counter. 4. Any Node with counter == 0 is garbage and is available for reuse. 2 right: left: value: Node 24.3 Reference Counting Exercise Draw a “box and pointer” diagram for the following example, keeping a “reference counter” with each Node . Node *a = new Node(’a’, NULL, NULL); Node *b = new Node(’b’, NULL, NULL); Node *c = new Node(’c’, a, b); a = NULL; b = NULL; c->left = c; c = NULL; Is there any garbage?
Background image of page 1

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

View Full Document Right Arrow Icon
24.4 Memory Model Exercise In memory, we pack the Node instances into a big array. In the toy example below, we have only enough room in memory to store 8 Node s, which are addressed 100 107. 0 is a NULL address. For simplicity, we’ll assume that the program uses only one variable, root , through which it accesses all of the data. Draw the box-and-pointer diagram for the data accessible from root = 105. address 100 101 102 103 104 105 106 107 value a b c d e f g h left 0 0 100 100 0 102 105 104 right 0 100 103 0 105 106 0 0 root: 105 What memory is garbage?
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.

{[ snackBarMessage ]}

Page1 / 6

24_garbage_collection - CSCI-1200 Data Structures Fall 2010...

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