From ccbc349db7645f3f2d4b16c9c849b7a6371f800d Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Thu, 18 Apr 2024 00:10:06 -0400 Subject: [PATCH] 25 to 27 --- lectures/27_garbage_collection/README.md | 32 ++++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lectures/27_garbage_collection/README.md b/lectures/27_garbage_collection/README.md index 6bd42be..8d73a1a 100644 --- a/lectures/27_garbage_collection/README.md +++ b/lectures/27_garbage_collection/README.md @@ -1,4 +1,4 @@ -# Lecture 25 --- Garbage Collection & Smart Pointers +# Lecture 27 --- Garbage Collection & Smart Pointers ## Today’s Lecture @@ -6,7 +6,7 @@ - 3 Garbage Collection Techniques - Smart Pointers -## 25.1 What is Garbage? +## 27.1 What is Garbage? - Not everything sitting in memory is useful. Garbage is anything that cannot have any influence on the future computation. @@ -18,7 +18,7 @@ Certainly this makes it easier to learn to program in these languages, but autom have performance and memory usage disadvantages. - Today we’ll overview 3 basic techniques for automatic memory management. -## 25.2 The Node class +## 27.2 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: @@ -34,14 +34,14 @@ public: }; ``` -## 25.3 Garbage Collection Technique #1: Reference Counting +## 27.3 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. -## 25.4 Reference Counting Exercise +## 27.4 Reference Counting Exercise - Draw a “box and pointer” diagram for the following example, keeping a “reference counter” with each Node. @@ -57,7 +57,7 @@ c = NULL; - Is there any garbage? -## 25.5 Memory Model Exercise +## 27.5 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 Nodes, 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 @@ -71,7 +71,7 @@ data. Draw the box-and-pointer diagram for the data accessible from root = 105. - What memory is garbage? -## 25.6 Garbage Collection Technique #2: Stop and Copy +## 27.6 Garbage Collection Technique #2: Stop and Copy 1. Split memory in half (working memory and copy memory). 2. When out of working memory, stop computation and begin garbage collection. @@ -81,7 +81,7 @@ data. Draw the box-and-pointer diagram for the data accessible from root = 105. (d) Repeat until scan == free. (e) Swap the roles of the working and copy memory. -## 25.7 Stop and Copy Exercise +## 27.7 Stop and Copy Exercise Perform stop-and-copy on the following with root = 105: @@ -112,7 +112,7 @@ scan: free: ``` -## 25.8 Garbage Collection Technique #3: Mark-Sweep +## 27.8 Garbage Collection Technique #3: Mark-Sweep 1. Add a mark bit to each location in memory. 2. Keep a free pointer to the head of the free list. @@ -125,7 +125,7 @@ free: (a) Start at the end of memory, and build a new free list. (b) If a node is unmarked, then it’s garbage, so hook it into the free list by chaining the left pointers. -## 25.9 Mark-Sweep Exercise +## 27.9 Mark-Sweep Exercise Let’s perform Mark-Sweep on the following with root = 105: @@ -142,7 +142,7 @@ free: stack: ``` -## 25.10 Garbage Collection Comparison +## 27.10 Garbage Collection Comparison - Reference Counting: @@ -166,7 +166,7 @@ stack: - \- runs the same speed regardless of how much of memory is garbage. It must touch all nodes in the mark phase, and must link together all garbage nodes into a free list. -## 25.11 Practical Garbage Collection Methodology in C++: Smart Pointers +## 27.11 Practical Garbage Collection Methodology in C++: Smart Pointers Garbage collection looks like an attractive option both when we are quickly drafting a prototype system and also when we are developing big complex programs that process and rearrange lots of data. @@ -185,7 +185,7 @@ we can do? Yes, we can use Smart Pointers to gain some of the features of garbag [http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/](http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/) -## 25.12 What’s a Smart Pointer? +## 27.12 What’s a Smart Pointer? - The goal is to create a widget that works just like a regular pointer most of the time, except at the beginning and end of its lifetime. The syntax of how we construct smart pointers is a bit different and we don’t need to @@ -225,7 +225,7 @@ void foo() { - We don’t have to call delete! There’s no memory leak or memory error in this code. Awesome! -## 25.13 So, What are the Advantages of Smart Pointers? +## 27.13 So, What are the Advantages of Smart Pointers? - With practice, smart pointers can result in code that is more concise and elegant with fewer errors. Why? ... - With thoughtful use, smart pointers make it easier to follow the principles of RAII and make code exception safe. In the auto_ptr example above, if DoSomething throws an exception, the memory for object p will be properly deallocated when we leave the scope of the foo function! This is not the case with the original version. @@ -256,7 +256,7 @@ polys.push_back(shared_ptr(new Quad(/*...*/))); polys.clear(); // cleanup is automatic! ``` -## 25.14 Why are Smart Pointers Tricky? +## 27.14 Why are Smart Pointers Tricky? - Smart pointers do not alleviate the need to master pointers, basic memory allocation & deallocation, copy constructors, destructors, assignment operators, and reference variables. - You can still make mistakes in your smart pointer code that yield the same types of memory corruption, @@ -265,7 +265,7 @@ segmentation faults, and memory leaks as regular pointers. design patterns). You need to understand your application and the different pitfalls when you select the appropriate implementation. -## 25.15 What are the Different Types of Smart Pointers? +## 27.15 What are the Different Types of Smart Pointers? Like other parts of the C++ standard, these tools are still evolving. The different choices reflect different ownership semantics and different design patterns. There are some smart pointers in STL, and also some in Boost (a C++