Reference counting
Reference counting is a memory-management technique that tracks references to an object or value and can reclaim it when its reference count reaches zero.
C++ Concurrency in Action, Second Edition
Reference counting is a memory-management technique that tracks how many references point to an object and reclaims it when the count reaches zero.
- How it is used
- Uses per-node access counts to address safe reclamation in concurrent data structures.
- How deep it goes
- Explains per-node counts through stack implementations and warns that shared_ptr operations are not guaranteed to be lock-free.
- Examples
- Compares stack implementations using shared_ptr, atomic_shared_ptr, and split reference counts.
- What you can do after
- Apply reference counting to stack-node reclamation using shared pointers or split counts.
Read how C++ Concurrency in Action, Second Edition explains reference counting →
Python How-To
Reference counting is an automatic memory-management technique that tracks how many references identify each object and releases an object when its count reaches zero.
- How it is used
- Explains how Python automatically releases objects that no longer have references.
- How deep it goes
- Defines increments, decrements, and destruction with code, but defers cyclic-reference handling to gc.
- Examples
- Uses Task instances, reassignment, dictionary and list references, sys.getrefcount, del, and an overridden __del__ method.
- What you can do after
- Diagnose/debug with it by observing reference-count changes and object destruction in custom class instances.
Code Like a Pro in Rust
Reference counting is a memory-management technique that tracks the number of references to an object and reclaims it when that count reaches zero.
- How it is used
- Enables shared ownership when data must be used across threads or stored in multiple structures.
- How deep it goes
- Explains counter increments, decrements, and release at zero, while leaving custom pointer implementation as an exercise.
- Examples
- Illustrates shared data in a Vec and HashMap and a doubly linked list using Rc, RefCell, and Box.
- What you can do after
- Apply shared ownership using Rust’s Rc or Arc, selecting the pointer for single-threaded or multithreaded use.
Read how Code Like a Pro in Rust explains reference counting →
Python Concurrency with asyncio
A memory-management technique that tracks the number of active references to each object and reclaims an object when that count reaches zero.
- How it is used
- Explains why CPython uses the global interpreter lock and why threads cannot safely modify shared reference counts concurrently.
- How deep it goes
- Defines incrementing, decrementing, reclamation, and a lost-update race, without detailing broader garbage-collection internals.
- Examples
- Uses integers, dictionaries, lists, and a two-thread reference-count increment race to make the mechanism concrete.
- What you can do after
- Evaluate how reference-count updates contribute to CPython's thread-safety constraints and the global interpreter lock.
Read how Python Concurrency with asyncio explains reference counting →
Also covered in
- Learn Rust in a Month of Lunches
- Rust in Action: Systems programming concepts and techniques
- Latency
- JavaScript in DepthIn early access (MEAP)
- The Quick Python Book, Fourth Edition
What to know first
- referenceA reference is an indirect value, binding, or relationship that identifies or provides access to another object, value, resource, or source.
- lockingLocking is a synchronization technique that controls concurrent access to a shared resource or protected operation.
- smart pointerA smart pointer is a pointer-like object that manages or mediates access to a referenced resource, often providing ownership, lifetime, or safety behavior.
Related concepts
- Rcused by · Rc is Rust’s single-threaded reference-counted smart pointer for shared ownership of a value.
- circular referencecontrasts with · A circular reference is a cycle of direct or indirect references among objects, components, or resources, in which following the references eventually leads back to something already encountered.
- CPythonused by · CPython is the reference implementation of the Python programming language, written primarily in C and providing its interpreter and runtime.
Questions about reference counting
What is reference counting?
Reference counting is a memory-management technique that tracks references to an object or value and can reclaim it when its reference count reaches zero.
Which Manning books explain reference counting?
C++ Concurrency in Action, Second Edition, Python How-To, Code Like a Pro in Rust and Python Concurrency with asyncio.
What should I know before learning reference counting?
Manning books that teach reference counting usually introduce reference, locking and smart pointer first.
What is the difference between reference counting and circular reference?
Reference counting is a memory-management technique that tracks references to an object or value and can reclaim it when its reference count reaches zero. A circular reference is a cycle of direct or indirect references among objects, components, or resources, in which following the references eventually leads back to something already encountered.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



