3 ms·
It's a matter of definitions. Technically, reference counting is a form of garbage collection, and it is often discussed in the garbage collection literature.
by simiones 3y ago
It's a matter of definitions. Technically, reference counting is a form of garbage collection, and it is often discussed in the garbage collection literature.
Most commonly though, "garbage collection" refers to global schemes which apply by default to all memory (perhaps with rare exceptions such as pinning).
So, in typical usage, C++ smart pointers, being opt-in, are not considered garbage collection. Languages which do automatic reference counting globally, such as Python or Swift, are indeed considered garbage collection schemes.
- Sohcahtoa82 3y ago> Technically, reference counting is a form of garbage collection, and it is often discussed in the garbage collection literature. This is something I always thought was true, but then I see people talk about reference counting VERSUS garbage collection, when I was taught that reference counting is a subset of garbage collection. In other words, "garbage collection" merely refers to any method of automatic memory management that the programmer doesn't have to think about, and reference counting is merely one method of implementing garbage collecting. Heck, Python uses reference counting, yet the library for directly interacting with the reference counter is called "gc", for "garbage collection".
- simiones 3y agoDifferent terms can have (slightly) different meanings in different contexts. As I said, I think in practice any form of memory management that the language runtime/compiler does for you is considered garbage collection in the colloquial sense, but any such system where you have to use a specific language construct (such as std::shared_ptr or Rc) are not. I'd also note that Python doesn't just do reference counting: it also has a tracing garbage collector that collects unreachable reference cycles. I'm not sure if Swift does something similar.