Lesson 20 of 26
Memory without a garbage collector
Every program has to give memory back when it no longer needs it. Python and Rust both do this for you, in very different ways.
- CPython keeps a reference count on every object. When the count drops to 0, the object is freed
right away. Objects that point at each other in a circle never reach 0, so a separate cycle collector (the
gcmodule) looks for them from time to time. - Rust has neither. Every value has one owner (lesson 13), and the compiler puts the "free it" step where
the owner ends: at its closing
}. Nothing runs in the background, and there is nothing to tune.
You can watch it happen. Python calls __del__ when it frees an object; Rust calls drop from
the Drop trait. The examples use both to print a line when something is freed.
Watching memory being freed
Both print the same thing, for different reasons. CPython frees an object the moment its reference count
reaches 0. Rust frees a value at the } where its owner ends, decided when the program is compiled.
The timing of __del__ is a CPython detail (PyPy, for example, runs it later); the timing of
drop is a Rust guarantee.
class Buffer:
def __init__(self, name):
self.name = name
print(f"open {name}")
def __del__(self):
print(f"free {self.name}")
def work():
temp = Buffer("temp")
print("working")
big = Buffer("big")
work()
print("back from work")
del big
print("end")struct Buffer {
name: String,
}
impl Buffer {
fn new(name: &str) -> Buffer {
println!("open {name}");
Buffer { name: String::from(name) }
}
}
impl Drop for Buffer {
fn drop(&mut self) {
println!("free {}", self.name);
}
}
fn work() {
let _temp = Buffer::new("temp");
println!("working");
}
fn main() {
let big = Buffer::new("big");
work();
println!("back from work");
drop(big);
println!("end");
}open big
open temp
working
free temp
back from work
free big
endMoving a Vec doesn't copy its items
A Vec has two parts: a small header on the stack (a pointer, the length and the capacity:
24 bytes on a 64-bit machine) and the items on the heap. let b = a; copies only the small header
and makes b the owner. The items stay where they are. .clone() asks for new heap memory and
copies every item. as_ptr() gives the address of the items, so we can compare.
a = [1, 2, 3]
b = a
print("assign:", "same data" if b is a else "new data")
c = b.copy()
print("copy:", "same data" if c is b else "new data")fn main() {
let a = vec![1, 2, 3];
let items = a.as_ptr();
let b = a;
println!("assign: {}", if b.as_ptr() == items { "same data" } else { "new data" });
let c = b.clone();
println!("copy: {}", if c.as_ptr() == b.as_ptr() { "same data" } else { "new data" });
}assign: same data
copy: new dataBox: a value on the heap, with one owner
Rust must know the size of every value when it compiles. A node that contains the next node would contain
itself forever, so it has no size. Python never has this problem, because every attribute is just a reference to an
object somewhere on the heap. In Rust you ask for that with Box: a Box<Node> is a
pointer (8 bytes) to a Node on the heap, and the box owns it. When the box is dropped, the node is freed
too.
from dataclasses import dataclass
@dataclass
class Node:
value: int
next: "Node | None"
items = Node(1, Node(2, Node(3, None)))
total = 0
node = items
while node is not None:
total += node.value
node = node.next
print(total)struct Node {
value: i32,
next: Option<Node>,
}
fn main() {
let items = Node {
value: 1,
next: Some(Node { value: 2, next: Some(Node { value: 3, next: None }) }),
};
let mut total = 0;
let mut node = Some(&items);
while let Some(n) = node {
total += n.value;
node = n.next.as_ref();
}
println!("{total}");
}6Where the Python analogy breaks
__del__is not a promise. CPython runs it as soon as the reference count reaches 0, but objects in a cycle wait forgc, and other Pythons such as PyPy run it whenever their collector gets there. Rust'sdropruns at the}, every time.let _ = value;drops the value at once;let _name = value;keeps it until the}. In Python,_is an ordinary name.- No per-object overhead. A Rust
i32is 4 bytes, and a struct is its fields side by side. Every Python object carries a reference count and a type pointer, so even the integer1takes 28 bytes in 64-bit CPython. - Leaks are allowed. Rust promises you never use memory after it is freed, not that everything gets freed:
Rccycles (next lesson) andstd::mem::forgetleak without any error. - You can't call
x.drop()yourself; that is a compile error. Calldrop(x), which takes ownership, soxcan't be used afterwards.
Cheat card
| Python | Rust |
|---|---|
del x | drop(x); |
def __del__(self): ... | impl Drop for X { fn drop(&mut self) { ... } } |
# freed when the refcount reaches 0 | // freed at the owner's closing } |
gc.collect() | // no garbage collector |
b = a # same list, two names | let b = a; // moves the header, the items stay |
b = a.copy() | let b = a.clone(); // new heap memory |
class Node:
next: "Node | None" | struct Node { next: Option<Box<Node>> } |
sys.getsizeof(x) | std::mem::size_of::<T>() |
_ = make() # a normal name | let _ = make(); // dropped at once |
← Reading real code Shared ownership: Rc, RefCell and Weak →