LESSON 1.08 · 6 STAGES · 2 CONTROLS · 2 CHECKSPRO · PREVIEW
Python memory management
Watch reference counts rise and fall, and see the collector free a cycle.
Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 2 checks, in this lesson and every other.
Go Pro · $8.25/moEXPLAINER · 1.08 · MEMORY BAYCOUNT a 1 · b 2
01
Stage 1 of 6 · Count references
Each reference adds 1 to an object's reference count. Node b has 2: the name b and a.next.
Explain it simply
The function main makes two Node objects, the boxes on the right. Each object keeps a count of the references that point to it. That's its reference count. Node b has two arrows coming in, from the name b and from a.next, so its count is 2.
Node a has a count of 1, from a. Node b has a count of 2, from b and a.next.
frame and nameobjectreference and countreachable, outputunreachableweak reference, freed
Toy: 2 Nodes, 1 weakref · CPython 3.12 rules · ids are stand-ins · interpreter objects left out
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
An object's count is the number of references that point to it.
PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
- 5 more stages and 2 live controls
- 2 mix-up cards and 2 predict-then-reveal checks
- Watch it as a narrated video, in three voices
- Every Pro lesson and capstone · 656 in total
$99 a year, or $15 a month. Cancel any time.