← Back to context

Comment by illuminator83

5 months ago

malloc and free aren't overhead.

If I'm reading a file, then opening and closing a file is not overhead either. But if I'm reading a file and some convoluted system in my own process is slapping some 3000% extra work on top because it needs to asynchronously track opened files and close them at arbitrary time later - that is overhead and creates bugs.

Which is one reason why many GC languages end up with bad emulations of an almost overhead free pattern like "try-with-resources" in Java and "IDisposable" in C# etc.

It is not correct to think of garbage collection as malloc + free + GC overhead. For example, allocating memory is much faster in many GC designs than a call to malloc (it can just be a pointer bump on the fast path).

I think you are assuming that a garbage collector calls malloc and free and then does a bunch of additional work on top of that to keep track of memory allocations, but that's not really how it works.