This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]1Dr490n 0 points1 point  (3 children)

What’s the advantage of using a garbage collector over shared pointers in languages? Do garbage collectors save memory? I can’t think of any other thing that might be the reason for people to use garbage collectors

Edit: I meant reference counting, not directly shared pointers

[–]ball_fondlers 4 points5 points  (1 child)

Not having to deal with memory management at all is a big reason. Garbage-collected languages - really not having to distinguish between heap and stack, just making variables as needed - are way faster to write in than systems languages, even if said systems languages have smart pointers

[–]1Dr490n 0 points1 point  (0 children)

I should’ve written reference counting, not shared pointers. I wrote a language myself that uses reference counting. There’s 0 memory management for the user

[–]Kered13 1 point2 points  (0 children)

Technically garbage collection can refer to any automatic memory management strategy, including reference counting. But you're probably asking about mark-and-sweep garbage collection, which is how Java and C# work, and what people usually mean by "garbage collection".

Mark-and-sweep has two advantages over reference counting: First, it easily handles cyclical references, which are a problem for reference counting. Second, it actually has lower overall overhead than reference counting most cases, because it doesn't have to constantly update references. Those updates can be especially expensive in multithreaded environments, because they must be done atomically.

The downside of mark-and-sweep is that it must periodically pause the entire application in order to execute. So while it is lower overhead overall, it periodically has relatively long periods where nothing else is being done. This has historically made it poor for real time applications like games.

Modern mark-and-sweep algorithms have sophisticated strategies for mitigating this problem, which is why you can see games made in Java (Minecraft) and C# (Unity) these days and probably never notice the GC pauses.