|
> Why is GC needed? We always used to have to manage our own memory per app. Track what you use and release it when you're done. I used to write Windows apps using Win32 calls all day long with no GC. The short answer, especially for the 1990s-2000s, was developer-productivity. No-one wants to write destructors and reason about all the unpredictable ways a deallocation might fail, and more besides, for a LoB system that needs to be deployed on Monday and will only be in-use for a few weeks. Today, that reason still applies, but another (oft-underconsidered) reason is the ability to safely (and correctly) move objects around in-memory, which permits heap defragmentation or even moving individual objects, or larger object-graphs, to different physical RAM locations to best benefit its current CPU workers in a NUMA system, for example; this could even be generalized out to non-volatile memory storage, if that ever goes mainstream. In an existing portable C program you simply can’t do that and will have to recycle your entire process if malloc fails due to fragmentation. ——- Separately, I find that GCs are all tightly-coupled to their corresponding compiler and runtime env; fundamental design decisions of its standard library will be based on how its GC works (and rarely vice-versa), and that extends to programming language-design itself (e.g. ARC needs the compiler to add ref-counting logic; languages with GC/ARC will then intentionally eschew language-features and keywords only needed in manual-nemory-mgmt-only languages (e.g. C# and Java lack a `delete` keyword or built-in reinterpret-cast operator). Having a non-ARC GC means a std library can have a user-friendly API where functions can return entire object-graphs and then forget about it; in a C-style env you instead need to make endless design-decisions 1) expose object-graphs as C structs using raw pointers - or use opaque types that use handles instead of pointers and forcing the calling application to perform everything through some object traversal API; 2) who is responsible for allocating memory for the returned graph? The caller - or callee? If the caller, how do you supply size information first before you supply the actual data? …and without having to do the same operation twice? If it’s a callee-allocated arena or pool then how can the consumer safely/correctly pick a subset of all allocated objects they want to keep around longer than the initial pool? 3) if you anticipate C++ consumers of your C-style API is it worth supporting smart-pointers and/or platform-managed memory? Etc etc. 4) will it work if your API .so is statically-linked in a program using a shared cstdlib? I’m mot dunking on C specifically, but most manual-memory-management only works well when the lifetime of allocated memory exactly matches program scope lifetime (not just lexical-scope, and not just “autos”/stack-allocation either), which means each separate unit-of-work in your program needs to have a bound and deterministic lifetime of its own (e.g. a HTTP request/response). Whereas this falls apart when your program needs to gracefully handle unbound and/or non deterministic memory usage (e.g. an RTS game using a malloc’d struct for each in-game unit… and the player is amassing a Zerg rush); or just any kind of user-interactive desktop application - I know you said you were happy with Win32’s memory API (with-or-without ATL), but compare a 2000s-era Win32 program like AIM or Winamp with a 2026 desktop abomination like Discord.exe or even MS Office today: there are so many concurrently-running operations, all with criss-crossing concerns with their own shared-state - if I were a dev manager on those products I’d only permit manual-memory management if the pull-request included a Z3 proof of correctness. |