system
Given the following post, respond as if you're commenting on Reddit:
human
Why do languages use garbage collection instead of reference tracking?

In c++, a shared_ptr knows how many copies it has. When the shared_ptr reaches zero copies, it frees the memory taken up by what it's pointing to.

This means there is zero need for garbage collection but you also have managed memory. Objects on the heap are deleted deterministically without a garbage collector.

To me, this seems like a perfect solution to the problem of memory management. It doesn't require a garbage collector running in parallel but you also avoid use-after-free and memory leaks. Why isn't this approach used by any languages by default?
gpt
> Why isn't this approach used by any languages by default?

It is used by python.

It has problems though: It doesn't deal with reference cycles which means that you still need a garbage collector (and that destruction isn't always deterministic). It can also be slower than a good GCs, especially in multi threaded environments where the reference counting needs to be done atomically.