When allocators are hoarding your precious memory (2021)
The number of arguments I&#x27;ve gotten into with other engineers that &quot;manual memory allocation is deterministic, GCs are not!&quot; is too high.<p>First off, GCs are quite deterministic, unless they are calling rand() someplace. I&#x27;ve walked through GC code under controlled conditions with identical repeated allocations, GCs do the exact same thing under the exact same circumstances every time.<p>Second off, you have to pay the price of allocation and deal with memory fragmentation somewhere. GCs typically pay the price at the tail end (deallocation, compaction) but have absurdly fast allocators (a handful of instructions), where as manual memory allocators pay the price for allocation (finding a slab of memory) up front, and unless you know how to work around the details of your allocator, you end up dealing with fragmentation yourself.<p>FWIW this also means many, many, types of applications can gain a boost from intelligent memory allocation strategies. There is the famous example of the cruise missile that never worried about deallocation because it would explode before it ran out of memory, but scenarios involving bespoke memory management being better than &quot;just use libc!&quot; are actually quite common.<p>Have a stateless microservice? If you know the max size of memory it&#x27;ll use, for each connection that comes in just allocate a slab of memory and when the connection closes free the entire slab, far more efficient than using a GC or any standard allocator. Also having to calculate the max amount of memory a single connection can use is a great way to figure out how much load an instance can handle (depending if you are CPU bound or not).<p>For certain stages of compilation, compilers can get away with not freeing memory and just exiting and letting the OS reclaim everything. (Of course less doable now days with compilers running as background services recompiling code as it is typed, and also whole program optimization has made it so compilers use a lot more memory, so I don&#x27;t know if this strategy is still in use in these modern times!)<p>The tl;dr is that freeing memory is complicated no matter if you have a GC or not. It is something every developer should be thinking about from time to time.