by pancakeguy · 2w ago · 0 comments
Moving GCs free you from the high overhead of malloc/free runtimes, something that us old C and C++ programmers have had decades of experience with, and why our colleagues developed large, complex runtimes such as TCMalloc and jemalloc to remove some of that overhead, even though not nearly as much as moving GCs do.
The reason low-level languages don't use moving collectors despite their superior efficiency is because they must give up on some performance optimisations for the abillity to interact with the hardware or OS directly, and that means that pointers must be stable machine addresses (this is why you need a richer FFI layer in Java). Moving collectors work in many ways like arenas. They don't need to maintain free lists whenever an object is allocated or freed (in fact, they never free any object at all), and memory is freed in bulk. This is not the only performance that low-level languages leave on the table because above everything else they must be low level.
Every single language that can use moving collectors does (it's also a matter of resources; moving collectors are hard to implement, especially the modern pauseless ones).
Sadly, people who are not compiler or memory allocator engineers don't know that, so they believe "popular wisdom" disseminated by other non-experts.
P.S.
Re AI: It's certainly possible that AI will be able to write good, efficient, large software some day, perhaps soon, but if it's able to perform the global program transformations required to replicate the optimisations of a moving collector without one, it won't need the simpler and more local optimisations done by a compiler, and will be able to work better without one, directly in machine code, in which case all compilers and programming languages are also done. In other words, moving GCs will not be a relic of the past before compilers are (or maybe roughly around the same time).
Nonetheless, the title of the paper by Andrew Appel (Garbage Collection Can Be Faster Than Stack Allocation) was just a false statement, which today would be called a clickbait title.
No method of dynamic memory allocation can be faster than stack allocation, if the latter is done correctly.
In Appel's paper, he claimed that if you have tons of memory, than the overhead of freeing memory by the garbage collector can become low, because of its laziness, i.e. the garbage collector will be invoked only when there is a huge amount of memory that must be freed, so the overhead per freed byte will be low, even lower than when stack-allocated memory is freed word-by-word.
However, this comparison is a classic example of a straw man. When memory is abundant, many compilers implement an optimization where stack deallocation is delayed as much as possible, in order to free with a single instruction large amounts of memory. So the same lazy deallocation strategy is also available for stacks, and the difference between a single instruction needed for stack deallocation and a long procedure needed by the garbage collector is enough to ensure that for any realistic amount of available memory, stack allocation is the most efficient.
Obviously, stack allocation is not applicable to everything, which is why other techniques, like reference counts and garbage collectors, are also necessary.
But, this is only assuming we use only the most naive allocation algorithms when managing memory. Yes, it’s true allocating is faster with a GC than malloc. But then you have to remember - most GC languages force way more allocations than necessary. Java is perhaps the worst about this, where almost everything goes on the heap.
You can use arena allocators to put objects close together and get allocations almost for free. And, you can put more objects on the stack next to each other, avoiding allocation cost altogether. In practice, this will greatly outperform a GC, even if you don’t take into account collection cost! What will not outperform a GC is doing a million tiny allocations.
But, in languages like C and C++, you don’t typically do that. A million tiny allocations is a Java thing. That’s why when people compare runtimes they sometimes mess up. Yes, if you implement a Java algorithm verbatim in C++… it will perform poorly. So, don’t.
Also, moving GCs only perform adequately when memory pressure is low. The more memory usage and churn you have, the more moving, compacting, and collecting you have to do. The same is not true of other allocation techniques. A lot of comparisons are not well thought out because they’ll be running at 10% heap utilization. Of course that performs well, it’s likely the GC isn’t even performing many collections. But that’s not true at 90% utilization. Hence why collectors like the G1GC were historically disabled on memory constrained environments.
As for performance differences in practice, it is easy to encounter Java programs, typically either large ones or very concurrent ones, that easily outperform C++ programs by a factor of two or more, just as it's easy to see examples of the opposite, typically in smaller or less concurrent programs. This is what we would expect.
As experienced low-level programmers know, it is quite easy to avoid the operations that are slow in C++ or other low-level languages - heap allocations and dynamic dispatch - when programs are relatively small and/or young. As programs grow larger, it gets more and more expensive to do this because these operations offer high abstraction, the ability to keep changes local. And it is these two operations that the HotSpot JVM was built to optimise.
I gave a talk about exactly that at the last Devoxx: https://youtu.be/vhDwy1eCXbs
This is true of your basic moving collectors. It's generally not true of reference-counting collectors (including Bacon-style cycle detecting collectors). It's also not true of tracing collectors that have a step of sweeping all live and dead objects, such as mark-sweep or mark-compact.
GC is not memory efficient.
If a primary concern is optimizing memory usage, you will avoid GC languages.
You are right, though, that in a very memory-constrained environment, usually embedded devices, you may want to lower memory consumption regardless of anything else, and in such environments moving GCs are probably not what you want (although other kinds of GC could be).
More here: https://youtu.be/xr73mR7ii9M
Rust is not the silver bullet its community is trying to meme it into being. Its approach to memory allocation only works in some cases, the rest of the time you're back to refcounting and the potential for leaks.
For AI it's especially bad because they never memorize full codebase context in the way an experienced dev would, so it all becomes very dependent on how your codebase is structured and how you prompt it. GC just eliminates all those bugs and the associated token cost of trying to find and fix them.
Unless you want AI generated code to be single threaded and not take advantage of multiple cores, some form of collector is still required. If you want to avoid heap fragmentation issues, a compacting collector is generally recommended.
I wouldn't say so. An implementation of the free function could do batching of this kind internally.
Hobby and domain specific languages may still use GC but getting rid of the concept of GC is the right direction, along with removing different ways of doing things in a programming language. Go was right when it forced developers to use only pattern for loops, one pattern for error handling, etc.
I don't think that making programs a lot more complicated because they have to prove a very specific memory management discipline is the right way to go.
With AI we don't need expressive programming languages for humans to read and understand at line-level details. Instead, we need stronger guardrails and fewer building blocks so that AI generates consistent solutions.
GC became poplar only because it was hard for programmers to keep track of memory management in languages like C and C++. And back then I was a huge proponent of GC based languages.
This is just a step along the same path that Go introduced. Many programmers switching from other languages complain about Go's restrictive and seemingly verbose syntax but once the benefits are realized the tradeoffs become worth it.