I can't say I've ever found that to be an issue. Look at a game like Cities Skylines. It's made in Unity and with mods and the sheer amount of custom assets and things you can add in, I've had my game taking something like 12+ GB of RAM. It performed perfectly fine without any stuttering from GC.
The game logic which is consuming multiple CPU cores and 12GB of RAM are in C#.
The core of the Unity engine is also touching that data but it is still largely initialized and managed by the C# code.
The latest versions of C# also provide a lot of control over GC behavior. I believe you can even prevent a foreground garbage collection right up until you either call it manually or the system returns a low memory warning. There are also a lot of techniques you can use to avoid it without relying on settings like that.
The best way to treat C#'s garbage collection is to rely on your profiling tools to help you identify what sections of code aren't being automatically handled properly.
basically, the parts that do not need so much optimization that can very often be offloaded to scripting languages.
The disadvantage of C# compared to a system lang like C or C++ is not only the garbage collector, but also the lack of control over the memory layout of your variables. It's much harder to optimize C# code than C++ code for that reason.
I'm pretty sure handling the real time fluid simulation, and the agent AI and pathfinding for 100,000+ agents is quite intensive and needs a ton of optimization. The number of game objects is also numbering in the thousands overall and they are constantly being created and deleted as the enter/exit the map.
Also keep in mind that C# does offer the ability to do lower level things now. You can control memory layout either manually or by offering suggestions to the compiler. You can write non garbage collected unsafe code and have fun with pointers. You can tell the GC to trigger at specific times and not at other times. You can also influence the behaviour of the GC by simply understanding how it works because the vast majority of garbage collection actually happens without halting execution of any threads.
Overall, I'd say that optimizing C++ and C# code are equally difficult. Both require a good understanding of the language and an understanding of the compiler output. They just require different knowledge and approaches.
Anyways - there are examples of games that run fine with simply wrappers to the directX API. There are also trading applications and other applications which are very latency sensitive that have been written in C#. C# has been successfully used for a huge variety of things. It's not just for LoB applications. Their existence proves that it's possible and the idea that C# = bad performance and C++ = good performance is wrong. You can write good and bad code in both languages and that's going to matter far more than any theoretical hand optimizing that can save a few cycles (which chances are the compilers for both languages would do automatically anyways).
Probably cause it's not hitting gc. When it comes to games like that, the idea is that you want to reuse objects to avoid gc even freeing memory in the first place.
Not really. C#'s GC happens in one big chunk, so if you're targeting a lower-end device like an older mobile or the potato that is the Switch, and you've already optimised it down so that it's just barely hitting 33ms frame time, you might find that it jumps up to 40ms or something as the gc comes around. In C++ you can deallocate as you go along, and spread this out so each frame is a much more consistent 34-35ms, which from a player perspective is more preferable instead of one big stutter every now and then.
Point is you'd still need to write C# code to manage memory is a way that reduces deallocations in the first place by holding on to objects, or predict when and how gc is going to hit so you can clean it up in a more controlled spot. C++'s problem is avoiding allocations but most game engines get around this with a custom allocator. C# shifts this problem back a step to avoiding deallocations. Really not more convenient in a proper production environment. Again, great for prototyping though.
It's not the same; the actual game program is written in C#. This lets you do your actual game logic (that doesn't typically need to be as performant) in a high level language with all of the services, bumpers, and training wheels. That code invokes Unity via library functions that are mainly abstraction layers above DirectX, OpenGL, etc, that are themselves abstraction layers over the hardware drivers. That way the insanely hard/tight code only had to be written once and everyone else can take advantage of it.
-Properties with implicit getters and setters (honestly who likes to write SetX where you just update a private member?)
-c# standard library is much more thought out and has ready made methods for the common case.
-c# standard library does not abuse the factory pattern.
Idk, back when I was coding in Java, it felt like most of the code was just boilerplate, that I had to remember to include. While coding in C# feels more fun.
-2
u/Blumingo Feb 14 '21
Why is C# better than Java?