This commit is contained in:
Jidong Xiao
2024-04-18 00:10:06 -04:00
parent 8b925b92d2
commit ccbc349db7

View File

@@ -1,4 +1,4 @@
# Lecture 25 --- Garbage Collection & Smart Pointers # Lecture 27 --- Garbage Collection & Smart Pointers
## Todays Lecture ## Todays Lecture
@@ -6,7 +6,7 @@
- 3 Garbage Collection Techniques - 3 Garbage Collection Techniques
- Smart Pointers - 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 - Not everything sitting in memory is useful. Garbage is anything that cannot have any influence on the future
computation. 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. have performance and memory usage disadvantages.
- Today well overview 3 basic techniques for automatic memory management. - Today well overview 3 basic techniques for automatic memory management.
## 25.2 The Node class ## 27.2 The Node class
- For our discussion today, well assume that all program data is stored in dynamically-allocated instances of the - For our discussion today, well 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: 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. 1. Attach a counter to each Node in memory.
2. When a new pointer is connected to that Node, increment the counter. 2. When a new pointer is connected to that Node, increment the counter.
3. When a pointer is removed, decrement the counter. 3. When a pointer is removed, decrement the counter.
4. Any Node with counter == 0 is garbage and is available for reuse. 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. - 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? - 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. - 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, well assume that the program uses only one variable, root, through which it accesses all of the - For simplicity, well 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? - 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). 1. Split memory in half (working memory and copy memory).
2. When out of working memory, stop computation and begin garbage collection. 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. (d) Repeat until scan == free.
(e) Swap the roles of the working and copy memory. (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: Perform stop-and-copy on the following with root = 105:
@@ -112,7 +112,7 @@ scan:
free: 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. 1. Add a mark bit to each location in memory.
2. Keep a free pointer to the head of the free list. 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. (a) Start at the end of memory, and build a new free list.
(b) If a node is unmarked, then its garbage, so hook it into the free list by chaining the left pointers. (b) If a node is unmarked, then its garbage, so hook it into the free list by chaining the left pointers.
## 25.9 Mark-Sweep Exercise ## 27.9 Mark-Sweep Exercise
Lets perform Mark-Sweep on the following with root = 105: Lets perform Mark-Sweep on the following with root = 105:
@@ -142,7 +142,7 @@ free:
stack: stack:
``` ```
## 25.10 Garbage Collection Comparison ## 27.10 Garbage Collection Comparison
- Reference Counting: - Reference Counting:
@@ -166,7 +166,7 @@ stack:
- \- runs the same speed regardless of how much of memory is garbage. - \- 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. 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 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. 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/) [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 Whats a Smart Pointer? ## 27.12 Whats 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 - 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 dont need to and end of its lifetime. The syntax of how we construct smart pointers is a bit different and we dont need to
@@ -225,7 +225,7 @@ void foo() {
- We dont have to call delete! Theres no memory leak or memory error in this code. Awesome! - We dont have to call delete! Theres 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 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. - 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<Polygon>(new Quad(/*...*/)));
polys.clear(); // cleanup is automatic! 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. - 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, - 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 design patterns). You need to understand your application and the different pitfalls when you select the
appropriate implementation. 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 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++ semantics and different design patterns. There are some smart pointers in STL, and also some in Boost (a C++