Hacker News new | ask | show | jobs
by buybackoff 11 days ago
For C# as is, what I really would appreciate is a frozen GC mode. Not zero GC, similar to Epsilon GC in Java. After initial state loading and forced GC, I would like to be able to apply a SustainedNoGC mode, that just does not even try to collect, no scanning, no pauses at all, it can only allocate. And notifications (an event) after some X bytes allocated, or just a reliable way to monitor the allocated size. The existing modes such as SustainedLowLatency or NoGCRegion are good but not enough. For some low latency workloads it's OK to allocate many GBs and then pause explicitly.

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.

2 comments

TBH, at least in my head, I feel like what it needs more is a 'string-ish type that can be used in a pool'.

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.

What about Span<char> over char[]? Or a tiny struct wrapper with the length. Or do you mean some deeper runtime support?
Ehh, it's hard to pin down fully, at least at this moment;

The Runtime Support thing is definitely part of it; things have improved over time but there is still a gap as far as stuff needing a string where a poolable thing could be better; ADO.NET comes to mind, especially when we consider how big some queries can get with ORMs and what a reasonably complex (or outright jank) data model can require. There's others, but that is the major case (And TBH It shouldn't be that hard to solve from an API standpoint, right? just have a default virtual impl that does the String conversion on the normal methods...)

But also, the elephant in the room to where we can do 'u8' on a literal that becomes a ReadOnlySpan/ReadOnlyMemory of bytes. It's a smell for the lack of a proper UTF8 string holder in general.

Can't you just initialize and then do a NoGCRegion? It would give you everything except the monitoring I think.
It does not work the way I described. It is only for short periods and is not a guarantee, and has overheads for repeated calls.
.NET has supported custom garbage collectors for many years, so if you're willing to accept that all heap-allocated objects will live as long as your process, implementing a no-op garbage collector should be a straightforward exercise.
It's far from straightforward. And I need a special mode, eg during application start it may generate a lot of garbage, I want that collected, but then continue with idle GC. With an option to collect overnight, or when too much is allocated, but in controlled way.
This pattern isn't far from what a videogame sometimes needs to run a level: load a lot of data, generate a lot of garbage, try to collect all the garbage after load but before gameplay, then keep the GC from running again.

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.