There is No Excuse for Not Having GC

Published at 10:45 on 1 January 2013

About five years ago, I set out to learn C++. I gave up after a few days when I ended up going down the rabbit hole of what’s on the heap, what’s on the stack, and what can be freed when. It was an enormous headache, and nowhere was it clearly and succinctly explained.

And it wasn’t even necessary to worry about. Modern languages like Java, Python, and C# all have garbage collection. Program in one of those and you don’t ever have to care much about memory management. The language subsystem that runs your programs does it all for you.

Given that memory leaks are one of the most common, and one of the most difficult to track down and resolve, classes of bugs, this is a win of simply monumental proportions. It is unfathomable why one would wish to voluntarily relinquish such a boon.

The standard answer to this observation is to point out that there are certain timing-dependent programs (typically ones that access raw devices), where GC gets in the way and can cause lost data.

I don’t think this argument has much validity when it comes to arguing in favor or not providing GC. Rather, it is an argument in favor of providing the programmer with control over GC. All one needs are four basic operations:

  1. Turn automatic GC off.
  2. Turn it back on again.
  3. Perform a GC cycle manually.
  4. GC a single datum manually.

With those, one can do timing-critical programming and not have GC get in the way one bit.

By turning GC off and reclaiming things on a case-by-case basis as needed, one can even use the same memory-management paradigm that primitive, functionally obsolete languages like C++ mandate you to use. Even if you do that, you can still perform occasional invocations of manual GC cycles, and log error messages if anything got garbage collected, and your program will detect its own memory leaks.

About the only rationale I can think of for not having GC at all is that one is writing some sort of embedded code on hardware so limited that there is not memory or CPU cycles available for it. That’s almost certainly a tiny fraction of all programming tasks.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.