4 ms·
Except that the GC makes it exactly not viable for games and its one of the biggest problems Unity devs run into. I agree it's a great language, but its not a d
by jabwd 2y ago
Except that the GC makes it exactly not viable for games and its one of the biggest problems Unity devs run into. I agree it's a great language, but its not a do it all.
- lmm 2y agoMany of the top games in recent years have used it, so you've got a funny definition of "not viable".
- johnisgood 2y agoOr roll their own, so they used GC in one way or another.
- neonsunset 2y agoUnity (and its GC) is not representative of the performance you get with CoreCLR. The article discusses ref lifetime analysis that does have relationship with GC, but it does not force you into using one. Byrefs are very special - they can hold references to stack, to GC-owned memory and to unmanaged memory. You can get a pointer to device mapped memory and wrap it with a Span<T> and it will "just work".
- mafuy 2y agoWell, when I worked in Unity I used to compile C# code with the LLVM backend. It was as fast as C++ code would be. So Unity is perhaps an example in favor of C#.
- SeasonalEnnui 2y agoI could argue the opposite - GC makes it more viable for games. "GC is bad" misses too much nuance. It goes like this: developer very quickly and productively gets minimum viable game going using naive C# code. Management and investors are happy with speed of progress. Developers see frame rate stutters, they learn about hot path profiling, gen0/1/2/3 GC & how to keep GC extremely fast, stackalloc, array pooling, Span<T>, native alloc; progressively enhancing quickly until there are no problems. These advanced concepts are quick and low risk to use, and in the case of many of the advanced concepts; what you would be doing in other languages anyway.
- DeathArrow 2y agoThe only reason we might see FPS drop in games, is not because C# and its GC. It's mostly because the poor usage of the graphics pipeline and the lack of optimization. As a former game developer I had to do a lot of optimization so our games run nicely on mobile phones with modest hardware. C# it's plenty fast for game programming.
- SeasonalEnnui 2y agoA good datapoint, thanks. Extending my original point - C# got really good in the last 5 years with regards to performance & low-level features. There might be an entrenched opinion problem to overcome here.
- mycocola 2y agoThat entirely depends on the game. Recent example is Risk of Rain 2, which had frequent hitches caused by the C# garbage collector. Someone made a mod to fix this by delaying the garbage collection until the next load-screen — in other words, controlled memory leakage. The developers of Risk of Rain 2 were undoubtedly aware of the hitches, but it interfered with their vision of the game, and affected users were left with a degraded experience. It's worth mentioning that when game developers scope of the features of their game, available tech informs the feature-set. Faster languages thus enable a wider feature-set.
- n4r9 2y agoApparently that was released in 2019? Both C# and dotnet have had multiple major releases since then, with significant performance improvements.
- Rohansi 2y agoThe GC that Unity is using is extremely bad by today's standards. C# everywhere else has a significantly better GC. In this game's case though they possibly didn't do much optimization to reduce GC by pooling, etc. Unity has very good profiling tools to track down allocations built in so they could have easily found significant sources of GC allocations and reduced them. I work on one of the larger Unity games and we always profile and try to pool everything to reduce GC hitches.
- greener_grass 2y ago> not viable for games > Unity devs run into So it's viable but not perfect
- DeathArrow 2y ago> one of the biggest problems Unity devs run into Unity used Mono. Which wasn't the best C# implementation, performance wise. After Mono changed its license, instead of paying for the license, Unity chose to implement their infamous IL2CPP, which wasn't better. Now they want to use CoreCLR which is miles better than both Mono and IL2CPP.
- Paradigma11 2y agoDoesnt Unity use its own GC or transpiles to C++? Unity on .Net core is more than a year away, no?
- pjmlp 2y agoIt uses the prehistoric Mono GC. Additionally it transpiles IL to C++ due to many targets like consoles, and iDevices, not allowing for a JIT. They also have a C# subset called Burst, which could have been avoided if they were using .NET Core.
- bob1029 2y agoGC in modern .NET runtime is quite fast. You can get very low latency collections in the normal workstation GC mode. Also, if you invoke GC intentionally at convenient timing boundaries (I.e., after each frame), you may observe that the maximum delay is more controllable. Letting the runtime pick when to do GC is what usually burns people. Don't let the garbage pile up across 1000 frames. Take it out every chance you get.
- Traubenfuchs 2y ago> if you invoke GC intentionally at convenient timing boundaries (I.e., after each frame), Manually invoking GC many times per second is a viable approach?
- bob1029 2y agoIt has worked well in my prototypes. There is a reason a GC.Collect method is exposed for use.
- smaudet 2y agoAt least for this instance you have a good idea which objects are "ripe" for collection. There should be some way to specify "collect these, my infra objects don't need to be".
- munificent 2y agoIt can be, yes. You're basically trading off worse throughput for better latency. If you forcibly run the GC every frame, it's going to burn cycles repeatedly analyzing the same still-alive objects over and over again. So the overall performance will suffer. But it means that you don't have a big pile of garbage accumulating across many frames that will eventually cause a large pause when the GC runs and has to visit all of it. For interactive software like games, it is often the right idea to sacrifice maximum overall efficiency for more predictable stable latency.
- neonsunset 2y ago
- pjmlp 2y agoExcept that is a matter of developer skill, and Unity using Mono with its lame GC implementation, as proven by CAPCOM's custom .NET Core fork based engine used for Devil May Cry on the PlayStation 5.
- rafaelmn 2y agoC# has much better primitives for controlling memory layout than Java (structs, reified generics). BUT it's definitely not a language designed for no-gc so there are footguns everywhere - that's why Rider ships special static analysis tools that will warn you about this. So you can keep GC out of your critical paths, but it won't be pretty at that point. But better than Java :D
- neonsunset 2y ago> but it won't be pretty at that point Possibly prettier than C and C++ still. Every time I write something and think "this could use C" and then I use C and then I remember why I was using C# for low-level implementation in the first place. It's not as sophisticated and good of a choice as Rust, but it also offers "simpler" experience, and in my highly biased opinion pointers-based code with struct abstractions in C# are easier to reason about and compose than more rudimentary C way of doing it, and less error-prone and difficult to work with than C++. And building final product takes way less time because the tooling is so much friendlier.
- zigzag312 2y agoGames would need alternative GC optimized for low latency instead of maximum throughput. AFAIK it has been possible to replace the GC with alternative implementation for the past few years, but no one has made one yet. EDIT: Some experimental alternative GC implementations: https://github.com/kkokosa/UpsilonGC https://github.com/kkokosa/UpsilonGC https://www.codeproject.com/Articles/5372791/Implementing-a-Simple-Garbage-Collector-in-Csharp https://www.codeproject.com/Articles/5372791/Implementing-a-...
- lifthrasiir 2y agoAt least for Unity, the actual problem lies in IL2CPP and not C#. I have professionally used C# in real-time game servers and GC was never a big issue. (We did use C++ in the lower layer but only for the availability of Boost.Asio, database connectors and scripting engines.)
- Rohansi 2y agoUnity lets you use either IL2cPP (AOT) or Mono (JIT). Either way it will use Boehm GC which is a lot worse than the .NET GC. If your game servers weren't using Unity then they are using a better GC.
- lifthrasiir 2y agoYeah, we rolled our own server framework in .NET mainly because we were doing MMOs and there were no off-the-shelf frameworks (including Unity's) explicitly designed for that. In fact, I believe this is still mostly true today.
- alkonaut 2y agoIt's hard to use C# without creating garbage. But it's not impossible. Usually you'd just create some arenas for your important stuff, and avoid allocating a lot of transient objects such as enumerators etc. So long as you can generate 0 bytes of allocation each frame, you won't need a GC no matter how many frames you render. The question is only this: does it become so convoluted that you could just as well have used C++?
- jayd16 2y agoEnumerators are usually value types as long as you use the concrete type. Using the interface will box it. You can work around this by simply using List<T> as the type instead of the IEnnumerable. You have to jump through some hoops but it's really not that convoluted and miles easier than good C++.
- alkonaut 2y agoThe problem with it is that you don't know. The fundamental language construct "foreach" is one that may or may not allocate and it's hard for you as a developer to be sure. Many other low level things do this or at least used to (events/boxing/params arrays, ...). I wish there was an attribute in C# that was "[MustNotAllocate]" which files the compilation on known allocations such as these. It's otherwise very easy to accidentally introduce some tiny allocation into a hot loop, and it only manifests as a tiny pause after 20 minutes of runtime.
- neonsunset 2y agoMost often you do know whether an API allocates. It is always possible to microbenchmark it with [MemoryDiagnoser] or profile it with VS or Rider. I absolutely love Rider's dynamic program analysis that just runs alongside me running an application with F5, ideally in release, and then I can go through every single allocation site and decide what to do. Even when allocations happen, .NET is much more tolerant to allocation traffic than, for example, Go. You can absolutely live with a few allocations here and there. If all you have are small transient allocations - it means that live object count will be very low, and all such allocations will die in Gen 0. In scenarios like these, it is uncommon to see infrequent sub-500us GC pauses. Last but not least, .NET is continuously being improved - pretty much all standard library methods already allocate only what's necessary (which can mean nothing at all), and with each release everything that has room for optimization gets optimized further. .NET 9 comes with object stack allocation / escape analysis enabled by default, and .NET 10 will improve this further. Even without this, LINQ for example is well-behaved and can be used far more liberally than in the past. It might sound surprising to many here but among all GC-based platforms, .NET gives you the most tools to manage the memory and control allocations. There is a learning curve to this, but you will find yourself fighting them much more rarely in performance-critical code than in alternatives.
- Rohansi 2y agoUnity has literally the worst implementation of C# out there right now. Not only is it running Mono instead of .NET (Core) but it's also not even using Mono's generational GC (SGen). They have been working on switching from Mono to .NET for years now because Mono isn't being updated to support newer C# versions but it will also be a significant performance boost, according to one of the Unity developers in this area [1]. IL2CPP, Unity's C# to C++ compiler, does not help for any of this. It just allows Unity to support platforms where JIT is not allowed or possible. The GC is the same if using Mono or IL2CPP. The performance of code is also roughly identical to Mono on average, which may be surprising, but if you inspect the generated code you'll see why [2]. [1] https://xoofx.github.io/blog/2018/04/06/porting-unity-to-coreclr/#performance-with-coreclr https://xoofx.github.io/blog/2018/04/06/porting-unity-to-cor... [2] https://www.jacksondunstan.com/articles/4702 https://www.jacksondunstan.com/articles/4702 (many good articles about IL2CPP on this site)
- para_parolu 2y agoI believe Unity switched to net core last year
- Rohansi 2y agoThey did not - it is still a work in progress with no announced target release date. They also have no current plans to upgrade the GC being used by IL2CPP (their C# AOT compiler). https://discussions.unity.com/t/coreclr-and-net-modernization-unite-2024/1519272 https://discussions.unity.com/t/coreclr-and-net-modernizatio...
- jayd16 2y agoThe two biggest engines, Unreal and Unity, use a GC. Unity itself uses C#. C# is viable for games but you do need to be aware of the garbage you make. It's really not that hard to structure a game that pre-allocates and keeps per frame allocs at zero.