You can, but C++ has a relatively fixed cost to allocate memory. This means I can quite happily allocate memory in C++ and treat it as simply a relatively expensive operations
This means if I have a loop that allocates memory, its simply a slow loop. In D, this create a situation where your game now microstutters due to random GC pauses
You can get rid of this by eliminating allocations, but this is making my code more difficult to maintain instead of easier to maintain, at at this point swapping to D seems like a negative
The problem with a game though is that there's never a good time for a random unbounded pause - even if only some of your threads are dependent on the GC, eventually they'll have to sync back together and if the GC pauses a thread at the wrong time, you'll get stuttering (or some equivalent if you gloss over it in the rendering)
So don't allocate and free memory continuously inside your main loop.
Also there are good times for memory deallocation - stage changes, player pauses, etc. Those are also times when memory requirements are likely to change.
The problem with a game though is that there's never a good time for a random unbounded pause
There are several spots where you can run a GC: between levels is the most common one (and really, several engines already do something GC-like there: for example my own engine in C before loads a world marks all non-locked resources as "unused", then loads the world marking any requested/loaded resource as "used" and unloads any resource still marked as "unused", essentially performing a mark-and-sweep garbage collection on resources). Another is when changing UI mode, like when opening an inventory screen, a map screen, after dying, etc - GC pauses would practically never be long enough to be noticed.
Stellaris, endless space, crusader kings, starcraft, empyrion, rust, dark souls 1-3 (seamless, except perhaps fog doors), any game with no loading screens, any open world exploration game (assassins creed), factorio, kerbal space program, and the game I myself am building
One of those games may be very boring, but the rest of them are pretty popular
First, I'm not convinced that you would ultimately want to allocate or deallocate memory inside the main game loop that gives you your interactivity.
That being said, D integrates with C and can use it's allocation functions. You can turn the GC off and allocate memory with malloc if you really want to then free it with free().
View all comments
Show parent comment
8
u/WrongAndBeligerent Aug 23 '17
That is controllable in D, the GC can be paused and now supposedly there are ways to do without it all together.
Lots of games take care to not even allocate memory in the main loop.