176 points | 41h ago | Discuss on Hacker News | Back to Radar
Also, does it ship with GUI libraries?
GC is not surrendering control. That seems to be HN's #1 bug bear. The sensation that someone or something is taking control away from you. The proper way to frame this is that you can actually focus on the original problem you set out to solve. Risking memory leaks is crazy when it's not something the customer is demanding. Even in gaming I think the GC is fine. There are ways to spread the impact across frames and bound the amount of time spent collecting.
If your clever scheme wouldn't work with a GC-managed heap, it probably won't work with one managed by hand either.
But it really depends on what you're doing. You can use the wrong tool for a job, but you can also use the right tool. It depends on the job.
One does not have to live with the tradeoffs that C#'s default GC makes. One just needs to understand how the GC works and how it is controlled.
I'm not saying manage memory manually. You can switch to a language that is reference counted. Then at least that abstraction guarantees deterministic deallocation.
That comes with another set of trade-offs such as having to pay attention to atomic reference counts, making sure there's no cycles, maybe even treating some graphs as arenas to avoid chained deallocations, but the abstraction itself is designed to be deterministic and is more easily controlled.
And at least with GC, you are guaranteed to surrender the possibility of double-free and use-after-free.
That is a False Dichotomy
> GC is literally surrendering control.
it would seem that no GC is in the other direction, literally not surrendering control.
Manual memory management is not the only alternative to GC; RAII & smart pointers require, for the overwhelming majority of uses, no more thought than C#'s `new Foo` does, and don't require the concerns about manually free, use-afte-free, etc. that come with "manual memory".
Especially in multi-threaded settings, smart pointers perform poorly (compared to GC) due to requiring atomics on hot paths, and the alternative (epochs, hazard pointers, RCU) is orders of magnitude more complicated to get exactly right than just using GC.
This also explains why historically the most interesting concurrent data structures were first implemented in Java, not C++.
eg. Java got a decent concurrent hash table WAY before anything else - in 2003 vs. the 2006 initial release of Intel TBB, and the performance was not as good for all use cases. C++ performance did not catch up until ~2017 with the release of folly::ConcurrentHashMap!
Agreed that it’s no longer a bottleneck for games; profile—and-optimize approaches are very effective and the tooling has never been better.
To be fair, it is also probably the number one cause of long hitches in poorly optimized UE games, and also the main contributor to class of use-after-free bugs (persisted native pointers and references to objects that weren't properly added to the object graph, though goes away with proper usage of weak object pointers and the like) so it is very much a double-edged-sword.
I am one of those people that like non GC'd languages also. The concept of the GC Pause bothers me
But my preference and "bother" are not business strategies. For most purposes, and in my professional life, garbage collection is a boon.
For my hobby programming I choose real time domains so I can flex my Rust coding for fun and not pointlessly.
Or rather GC done explicit.
You control when and where and how garbage gets collected in various buckets and disposed off.
Without the performance hit of fully automatic GC.
> Risking memory leaks is crazy when it's not something the customer is demanding.
Memory leaks have NEVER been a significant source of bugs.
Memory corruption has always been the problem (lack of memory safety, ie. operating in perpetual unsafe block), not the memory leaks.
Memory leaks are relatively trivial to track and account for in comparison.
Arenas avoid most of the performance problems of malloc/free.
I think the bigger problem with arenas and manual management is memory lifetime is very hard to get right, particularly in the context of concurrent operations. It also cuts away algorithms you can do.
Sure, but they're not fully general, which is why you also need malloc/free for things that don't work well in arenas (or write the equivalent of malloc/free on top of arenas, which is what you need to do in Goose: https://github.com/aardappel/goose/blob/master/bench/kostya/...).
Also, arenas can be an even better fit for languages with good GCs than languages without a GC.
That's counterintuitive to me. I could see them being useful if you need off heap allocations, but on the heap it seems like they'd tend to be harmful rather than helpful. The only benefit they'd seem to have is forcing the colocation of memory. But they have the big downside of (likely) ending up promoting a bunch of memory into old gen.
They're just symptoms of managing the lifetime of memory incorrectly. Same solution for both - either better (safe) explicit lifetime management or GC.
Arenas don't fundamentally solve leaks nor corruption.
(I'm excluding resource leaks from the definition of memory leaks, to not muddy the discussion)
> They're just symptoms of managing the lifetime of memory incorrectly
No, they aren't. They are completely different class of a bug.
And have little to do with memory lifetime management.
Memory corruption happens, because certain language(s) make extensive use of unsafe constructs and doesn't have as little as basic built in container types (like friggin arrays or even strings) that do boundschecking by default, so you can overrun buffers and everything - left, right and center. And that's exactly what happens!
So C and to some extent C++ (with additional hundreds of footguns of all stripes)
The biggest benefit of languages like C# - more than anything, in addition to overall better ergonomics - is that they are memory safe first and foremost, and not inherently because of GC.
What you're talking about is spatial safety, but just having that does not end memory corruption.
Not having temporal safety eventually leads to the same outcomes and issues (search the CVE lists for UAF exploits...)
Temporal safety can only be achieved via GC or explicit safe lifetime management.
It would be interesting to see an official time-bounded GC feature where you could manually trigger a collection with a specific time budget, such as 5ms. The GC would collect a portion of objects and defer the rest to the next cycle. The runtime could still perform automatic GC pauses.
Nice POC, especially on MS-DOS but nothing truly relevant in the long run, I’m afraid
That said, I don't see how this project fits in all that nicely.
I commented this one explicitly because that zero c# platform pitched here is just the language syntax you know and then manual new/delete just like in c++
Thanks. This made me smile. Remember Mike Acton worked on Unity for a while. Look him up. See if he doesn’t care about memory. (I am not Mike).
I feel like some of the derisive statements you are making is coming from a decade old negative impression of some C# shop in a random town cranking out basic CRUD apps.
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.
An approach that doesn't need runtime would be to allow reflection for types provided at compile time (say, with a config file). There are many ways to approach reflection, from none to some to full. This appears to have chosen none. It could have chosen some, or potentially even full.
Native AOT is the exact opposite, it tries to preserve as much compatibility as possible to "normal" CLR dotnet and the BCL (it's the dotnet equivalent to JVM and JCL in Java world).
Bflat and this project take away everything from dotnet and only leaves C# behind, mostly incompatible to existing libraries and frameworks, no GC and very basic memory management.
You can also distribute apps without .NET runtime with NativeAOT.
I'm sure there are people who want a C# without GC, but for me it's just... sufficient enough.
Hell, I'll be bold and say, if there's a worthwhile AI credit sink, it would be just adding it all properly as a bolt on or similar to NativeAOT.
There's also C++ and although it is a monstrosity the practical thing would be to use that.
If you want a serious alternative to C++ I don't see it in C# without GC. On the other hand I have seen a lot of C++ code where normal C# would be much better suited.
True, but can't you work with a subset of C++ that you consider sane?
Even without unsafe we can allocate objects once and just reuse them. But why would you? C# is the go to for interfacing with native code and unmanaged memory. ( C# is way ahead of Java tho JDK 25 is beginning to catch up). I miss C#.
Figure out what parts of your app needs to be “downcoded” and optimize that. Enjoy bump allocators and plentiful libraries for the rest of it.
I wrote graphics applications in C# that used 3GB of memory back when we had to tell XP, at boot, to not use half the 32bit address space for itself. Most of it was in unmanaged space. C# is awesome.
Look, I'm all-in on CLR/C#, and one of my 'apps' is a 24/7 industrial camera monitoring system, and it has been running for the past, like, 3 months, on .NET 10 with "GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency" and "stackalloc" where needed, and...
Its performance, handling 8x ~800Mb/s streams has been... absolutely okay: 0 bytes over-allocated in the past 90 days (which is the last time my CoreOS-deployment auto-rebooted: all nodes are behind a VPN and outbound-only, so the latest OMG-CRITICAL-CVE doesn't really apply), so...
"If it works, it works"
It's always the fault of the arrow and never the archer.
Hell, I actually wrote one once; it was an Elixir app port to Akka.NET, but for performance and business requirements, due to various large broadcast messages of updates (as well as a policy/setup of killing an IIS worker if it hit over a certain limit,) I actually had to manually set GCHeapHardLimit via runtimeconfig; We would produce a lot of LOH stuff and somehow the way the allocator behaved as far as lack of compaction by default and the way server GC segments work on multicore, well it caused the process to frequently go over the IIS limit and kick the process.
Once we set GCHeapHardLimit (cause that magically enables LOH compaction,) all of those problems went away. Thing was smooth as silk after that (Well, at least until they added Dynatrace APM to the servers which caused a problem on every failover update of the SQL Backend...)
Just use Rust.
Keep in mind a large portion of time NativeAOT is actually SLOWER than going through RyuJIT with the only definitive benefit being smaller binaries, and it's not like you can't do Span<T> and ref structs in modern C# for manual low level memory control already.
So my question is just... why?
https://migeel.sk/blog/2023/12/08/building-bare-metal-bootab...
I would not want to build a long term project on top of this, as maintaining a fork of the .NET runtime is somewhat labor intensive.
But that's also the main limitation, it's kind of a worse version of Rust, with very limited memory management and no ecosystem.
If your goal is to distribute small, single-file executables, the size difference can be an order of magnitude.
The difference in startup time can be similarly significant, which is important in many applications.
Finally, the introduction of Native AOT has motivated the development of fast compile-time alternatives to many things in both the framework and third-party libraries that traditionally relied on comparatively slow runtime code generation and/or reflection, which benefit AOT and JIT alike.
"what is this useful for" - You want to write native code without a runtime or GC but you also don't want to deal with C++ with its arcane rules, edge cases, an awful dependency management story and complexity in general.
Rust is a good candidate for this as well, although it has quite a learning curve.
"you can already do this in standard C#" - Yes but the ecosystem assumes GC so you need to write everything yourself. There are some low allocation APIs but definitely not for everything. The concept of "C# but even lower level" is something which has a place, IMO. Just like how you can technically add assembly to C#, either by linking it with nativeAOT or emitting at runtime but it's not built-in support for inline assembly, you know.
I have not yet tested out Beef, but it seems to be more in line with what you'd want from a native C#-like language.
Random forced pauses are the only problem that comes from GC, otherwise it's great for throughput and just a nice feature, as a sibling comment says. No GC languages do not make code magically faster, they still have malloc/free. However, GC is surrendering control over when a program may be paused.
In C# you get most of that by calling `GC.Collect()` when you know you've finished loading and should be in a steady state. (I've seen non-games do it after application start as well, though in an enterprise application with full Server GC and plenty of RAM it feels more like a superstitious call than a necessary one.) Then in your "idle state" you can just try to avoid creating garbage. Games often use object pools aggressively, making sure most objects it needs are LOH/Gen2 and a few other "no allocation"/stack allocation-only approaches in the middle of a level.
You can try to ask the GC nicely with `GC.TryStartNoGCRegion()` during a section of your code but it's not necessarily a good idea and a lot of caveats apply.
But also, a lot of what you are asking for is sort of already what the default GC is optimized for by being a generational mark-and-sweep GC. It's tuned for bursts like application startup generating a lot of quick garbage that never leaves Gen0 and fast "arena sweeps". It's tuned for eventually reaching an Idle state where most of your objects are long-lived and hanging out in Gen2 and very rarely even checked to be collected.
With Pooling/Arenas already semi-established, you can do lots of stuff with pools/arenas. Where the challenge comes, is how much of Corelib (and normal apps) use strings in places, and you can't get around that without a lot of DIY. The "foostring" u8 const conversion helps for some cases but doesn't cover nearly enough cases.
Just using C, Rust, Zig, or whatever is probably nearly always the better option. Those languages are designed to work without GC and provide various well established ways to manage memory.
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.
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.
I do not understand the reasoning of changing the license of .NET from MIT to AGPL.
Comments are loaded live from Hacker News and are not stored by Mid or Real.