2 ms·
Being able to disable blocking gen 2 GCs for a period of time will be really useful for games and multimedia applications. You can disable the blocking GC durin
by kevingadd 14y ago
Being able to disable blocking gen 2 GCs for a period of time will be really useful for games and multimedia applications. You can disable the blocking GC during your update/render logic, then turn it back on while your main thread is blocked on vertical sync!
Being able to do this kind of scheduling aware GC would be a big boon for browser environments too. I've been pushing browser developers to do this and hopefully having working examples of it in other environments will help motivate them - see https://bugzilla.mozilla.org/show_bug.cgi?id=730113 https://bugzilla.mozilla.org/show_bug.cgi?id=730113 for one example of how this might be done.
- tinco 14y agoThat is not a very good idea, a gc could easily take more time than your vsync, even if you didn't leak anything. It's smarter to profile your game and make sure it doesn't leak any memory, and perhaps use this feature to disable the GC altogether until play is interrupted.
- kevingadd 14y agoHow exactly do you propose 'not leaking memory' in runtime environments where everything creates garbage? In .NET you at least have structs, but other environments (like JS as I gave as an example) make it impossible or near-impossible to be garbage-free. Controlling the timing of your GCs is a win there, even if a long GC can make you miss a frame. Half a GC happening during vsync instead of none of it is still a win because the hitch will be smaller.
- tinco 14y agoWether the language has structs or not has nothing to do with leaking garbage. I think it's even possible to not leak memory in js, though I haven't tried that. It is absolutely not true that everything in .net creates garbage. If you leak memory in your render/update loops then that's a problem. You prevent it by never calling new and never using syntax that implicitly allocates (like foreach). Anyways, the half GCs still happen even if you enable the no gc mode. Going for the small hitch instead of for no hitch at all seems to be setting the bar too low for me. (no matter how small the hitch, if it consistently happens players will notice it and your game experience will be seriously degraded) edit: just checked out your profile, you obviously know more of what creates garbage in js than I do, so I'll believe you if you say everything creates garbage :)
- mbq 14y agoYou may end up writing C...
- tinco 14y agoIf you regard watching your memory allocations as a C thing, then perhaps. When you build real time video games this is just how it goes, whatever language you work in. You need to make sure everything is allocated _before_ you enter the game loop, wether it's C or C#. Even when you feel bothering with those details is like programming C, you still have classes, namespaces, a sane compiling and linking system and a terrific standard library. Which in my opinion makes it infinitely more comfortable than coding in C.
- mbq 14y agoNote that the usually the OOP system heavily promotes short-lived memory allocations and the standard library is build around it.
- kevingadd 14y agoForeach in C# doesn't always create garbage, FYI. Enumerators can be structs - List<T>'s is, for example. It's certainly possible to write an application that doesn't structurally create garbage, but doing so isn't a universal win. Garbage-free coding patterns are more verbose, harder to get right, and often slower than relying on the GC. The reason a small hitch is better than no hitch is that if your game is running faster than your target framerate, hitches can be small enough to actually hide between frames - and control over when the GC occurs can allow you to reliably hide your GCs there.
- profquail 14y agoStructs can implement IEnumerator<T> as you mentioned, but they need to be boxed before any interface methods they implement can be called. So, foreach in C# does always create at least a tiny bit of garbage -- but in the case above it's likely to be reclaimed very quickly.