Garbage collector, C malloc and performance

I recently read very old blog post by Joel Spolsky Back to Basics from 2002 (side effect of reading about floating-point numbers, you know how this work). He stated that C code that use malloc can suffer for problems very similar that program using garbage collector, which was surprising news to me. This is no secret that in Julia, Go, etc., less GC means better performance and good written Julia function at second call, after JIT-compilation in first, should spend “close to 0% time” in GC.

At the same time, I’m looking for materials about comparison of efficiency of Julia GC to C malloc in both speed and memory usage (and other things about which existence I don’t know) at current stage of development. I just don’t know where to start, will someone help me enter this topic?

1 Like

For the convenience of other interested people, this is the most relevant paragraph:

That opens another whole can of worms: memory allocators. Do you know how malloc works? The nature of malloc is that it has a long linked list of available blocks of memory called the free chain . When you call malloc , it walks the linked list looking for a block of memory that is big enough for your request. Then it cuts that block into two blocks — one the size you asked for, the other with the extra bytes, and gives you the block you asked for, and puts the leftover block (if any) back into the linked list. When you call free , it adds the block you freed onto the free chain. Eventually, the free chain gets chopped up into little pieces and you ask for a big piece and there are no big pieces available the size you want. So malloc calls a timeout and starts rummaging around the free chain, sorting things out, and merging adjacent small free blocks into larger blocks. This takes 3 1/2 days. The end result of all this mess is that the performance characteristic of malloc is that it’s never very fast (it always walks the free chain), and sometimes, unpredictably, it’s shockingly slow while it cleans up. (This is, incidentally, the same performance characteristic of garbage collected systems, surprise surprise, so all the claims people make about how garbage collection imposes a performance penalty are not entirely true, since typical malloc implementations had the same kind of performance penalty, albeit milder.)


I am not sure that this question can be phrased in such a general way. How a C program uses malloc is crucial. In any case, modern GC implementations for nontrivial programs (that actually need to allocate) are quite hard to beat with manual memory management.

Whenever that is possible. Sometimes it isn’t, or not without a lot of complications that are just not worth it.