I'm developing a small .NET utility called montab (https://github.com/faxenoff/montab - taskbar with permanent previews for Windows) and have been trying to minimize its size and CPU/GPU usage as much as possible. The result is a 0.88 Mb binary with near-zero system load.
It would seem that Zero C# is the ideal solution for me. But here’s what I’ve actually found out: I’d have to rewrite absolutely everything, and the lack of a garbage collector when constantly working with a bunch of WinAPI calls is a problem with no solution. I’ve come to realize that this might only be suitable for new applications that don’t use external components.
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.
I wouldn't do it any longer because I value my sanity, but you could. And Mr Claude would probably do it quite happily, too.
> 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.
Ehh, it kinda does, at least from a binary size standpoint. Both for how reflection is handled and the compile metadata that needs to be included in the runtime (may be wrong on the second part but I'm 70% sure that's a cost)
Reflection to do what? Getting/setting fields, calling methods, etc. uses `object` which means boxing, which means allocating. Going AOT also means there is no JIT to compile methods (including generic instances) that were not statically referenced.
I mean, yes, but also, no - you can generate and cache (or return manual ownership to the caller) IL/whatever runtime code that doesn't box for any reflection thing that you need on the fly, assuming the type information is available. Like you make it an explicit API to do the code gen, no JIT required.
Just because the code isn't baked into the binary doesn't mean that it theoretically can't be done at runtime. At least, AFAICT.
It seems like you are contradicting yourself here. Just generate code that doesn't box on the fly, but without a JIT compiler? Boxing isn't even the biggest issue here - you could probably avoid that with a strongly typed reflection API, but I don't know if that makes it less useful.
IMO the big issue is AOT compilers only compile code that is referenced statically. Without a JIT you would have no choice but to error if you try to call something that wasn't compiled in. Compiling everything is not an option because generics.
Comments (137) 32 threads
Earlier commentsIt would seem that Zero C# is the ideal solution for me. But here’s what I’ve actually found out: I’d have to rewrite absolutely everything, and the lack of a garbage collector when constantly working with a bunch of WinAPI calls is a problem with no solution.
I’ve come to realize that this might only be suitable for new applications that don’t use external components.
I wouldn't do it any longer because I value my sanity, but you could. And Mr Claude would probably do it quite happily, too.
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.
Just because the code isn't baked into the binary doesn't mean that it theoretically can't be done at runtime. At least, AFAICT.
IMO the big issue is AOT compilers only compile code that is referenced statically. Without a JIT you would have no choice but to error if you try to call something that wasn't compiled in. Compiling everything is not an option because generics.
Continue this thread — 2 replies
All comments shown.
Couldn't load more comments. Check your connection and try again.