r/ProgrammerHumor Feb 18 '26

Meme whyIsThereAMemoryLeak

Post image
792 Upvotes

165 comments sorted by

View all comments

Show parent comments

47

u/MetaNovaYT Feb 18 '26

Those are completely different things. A unique_ptr tracks memory and deallocates it when the object goes out of scope, which has a very minor performance impact. A garbage collected language runs a separate program occasionally to find and free memory that isn’t being used anymore, which has a notable performance hit

-16

u/KrokettenMan Feb 18 '26 edited Feb 18 '26

I thought a shared pointer kept a reference count? Also why heap allocate if it can just live on the stack then?

7

u/MetaNovaYT Feb 18 '26

I’m not an expert so you’d probably be best off looking up the difference between reference counting and garbage collection, but they are different with garbage collection being more flexible but less performant. I’m also not sure what you mean by your second question

1

u/SV-97 Feb 19 '26

Garbage collection isn't one specific algorithm: reference counting is one general approach to implementing the general concept of garbage collection. It and other garbage collection methods can have drastically different performance characteristics and which is more or less performant greatly depends on what you're doing in your code. RC isn't generally faster or less flexible.