Lurker News

Comments (137) 32 threads

raincole6 replies#
C# already provides many tools for your performance-critical code though. You can use pointers in unsafe. You can allocate unmanaged memory with Marshal. You can avoid copy with Span. You can even manually vectorise with Vector.IsHardwareAccelerated and such.

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.

Waraqa3 replies#
NativeAOT can't compile for DOS.
raincole#
I... guess so? But the last time I had users who use DOS was in my previous life. NativeAOT supports iOS though, which is a reasonable target for the current-life me.
dragonwriter#
Which would be a problem if I needed to write code today and send it back to the 1990s to be run, but I have about as much need to compile to DOS as I need to produce punch cards to load into ENIAC.
to11mtm1 reply#
I know it's a bit contrived as far as making it work, but doesn't SeeSharpSnake [0] have a DOS compile option?

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.
[0] - https://github.com/MichalStrehovsky/SeeSharpSnake

reverseblade2#
DOS yes, 386: no
samuelknight#
NativeAOT gets rid of JIT not GC. The language is rich enough that you can avoid making garbage though.
randomNumber71 reply#
> I'm sure there are people who want a C# without GC

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.

DeathArrow#
>There's also C++ and although it is a monstrosity the practical thing would be to use that.

True, but can't you work with a subset of C++ that you consider sane?

jeremyjh2 replies#
Also people expect when they use a C# compiler that existing programs and libraries written in C# will be usable, but this doesn’t appear to support the same standard library APIs. So why use C# at all instead of a language with an ecosystem that has the properties you want?
DANmode1 reply#
Such as?
jeremyjh2 replies#
C++, Rust or Zig

Continue this thread — 2 replies

dragonwriter#
I mean, unless you don’t need any memory to be released during a run, everything needs to be rewritten to use memory management through arenas, which is a whole new memory management paradigm even for most people used to languages which require either manual memory management or highly specific lifetime specifications. So, yeah, no nontrivial existing code is usable even before you consider that none of the libraries supplied by the runtime that most code relies on are available.
lowbloodsugar1 reply#
.NET runtime also has generational GC and escape analysis (so objects can be allocated on the stack if they have a known lifetime). Generational GC essentially give you a super fast bump allocator for short lived objects: faster than malloc.

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.

pjmlp#
Having to spend quite some time with Node.js nowadays, I miss both Java and C# regularly.
a3w2 replies#
Isn’t marshaling about network representation, so the same concept as javas Parcelable tries to solve ?
chews#
sorta.. if you're thinking gRPC and Protobuf, but because they're on the same machine and often in the same process, it's a pointers-wizzeled interface all provided by the layers it was built on... then it just maps types, responses, and their interfaces. P/Invoke is wizardry.
raincole1 reply#
In C# (and generally in programming) marshaling is the conversion between your typical memory representation (in C# it's your object) and the other ways (be it C struct, C string, utf8 string, or whatever received from socket.) that's why AllocGlobal is under Marshal. Not saying one should use it though... the only ones sane programmers would touch is probably to handle C structs returned from native code.
jasomill#
System.Marshal is a sort of catch-all for functions used to interact with unmanaged code, including things like Win32 error handling and COM in addtion to type conversion and unsafe memory access in general.
bob102911 replies#
The GC is a feature. Not a bug to be eliminated.

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.

andai2 replies#
GC is literally surrendering control.

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.

jayd163 replies#
It's only surrendered control if you refuse to understand it and how to control it.
unrented7977#
Exactly. You have a shocking amount of control over the GC if you bother to learn. Surrendering control is a choice the programmer makes by not understanding their tools.
lilbigdoot2 replies#
There's still limits on that control.

Continue this thread — 2 replies

generichuman#
The (Tracing) Garbage Collector abstraction isn't really designed to be controlled though. You can understand the implementation and write code against that, but at that point, why not switch to something that you can genuinely control?

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.

nayuki1 reply#
Don't fall for the illusion that manual memory is the pinnacle of control. Because raw pointers cannot be moved, you can end up with a lot of external fragmentation - i.e. holes between your allocations. Also, the bookkeeping cost of malloc() and free() are not free, which also means that the latency of calling those functions is unpredictable. There's a reason why embedded developers are very hesitant to use malloc()/free() for hard real-time programs with time and memory constraints.

And at least with GC, you are guaranteed to surrender the possibility of double-free and use-after-free.

worik4 replies#
> Don't fall for the illusion that manual memory is the pinnacle of control.

That is a False Dichotomy

Continue this thread — 4 replies

Jooror#
I think some devs have painful memories of early GCs, compounded by the qualities associated with “corporate” codebases of the time. I would imagine younger programmers are writing more managed code than C and skew towards having no complaints about GCs.

Agreed that it’s no longer a bottleneck for games; profile—and-optimize approaches are very effective and the tooling has never been better.

999900000999#
Arguably without GC C# becomes a different language. But if you’re really comfortable with C# and can’t handle unexpected lag spikes I can see this being useful.
jameshart#
Exactly. The problem People have with GC is not garbage collection per se, it is garbage collection pressure. The solution to which isn’t doing away with the GC, it is managing and controlling allocation.
dafelst#
Indeed - even Unreal Engine uses GC for all its internal object management. It's not a language level thing, more a runtime entity thing, but it is there. It simplifies object lifetime management quite a bit.

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.

wk_end1 reply#
Garbage collection is a feature but it's also a trade-off, like most things in engineering. IMO it's a worthwhile trade-off more often than a lot of people think (though a lot of people don't think enough about it). But there's also times that it's not worthwhile, and we shouldn't pretend otherwise.
worik1 reply#
> IMO it's a worthwhile trade-off more often than a lot of people think

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.

WorldMaker3 replies#
Today's GC languages with multi-threaded generational mark and sweep generally don't "GC pause". You can see slow downs if you hit a worst case, like accidentally churning your large object heap and/or long-lived generation too fast, but even then it is rarely a "stop the world" pause but a subtle competition for CPU cycles and memory bus but other parts of your application like network I/O might not even notice.

Continue this thread — 3 replies

Johanx642 replies#
Arenas, etc is GC done right.

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.

pron1 reply#
Generational moving GCs work much like arenas, and they work without the limitations of arenas and without the performance hit of malloc/free. That's not to say arenas aren't very efficient and useful, but few languages make them convenient and composable enough (Zig), and even then they need to be used with care.
cogman103 replies#
> and without the performance hit of malloc/free.

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.

Continue this thread — 3 replies

jaen3 replies#
Memory leaks and memory corruption are two sides of the same thing.

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)

Continue this thread — 3 replies

Continue this thread — 4 more replies

YuechenLi7 replies#
So... What does this have over using NativeAOT which already exists?

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 and ref structs in modern C# for manual low level memory control already.
So my question is just... why?

Continue this thread — 7 replies

hollowonepl9 replies#
I’m not sure, the whole point of this language, standard libraries and runtime is built around GC. Nobody from C# world is educated or prepared to manually manage memory. This is just bare metal C++ with a lot of it stripped away and just nicer/cleaner syntax that does not inherit so much from C and POSIX archeology.

Nice POC, especially on MS-DOS but nothing truly relevant in the long run, I’m afraid

Continue this thread — 9 replies

Load more comments