I started learning C# and even Java for interest in developing
games. But I know that many are developed with C++, mainly
for not having a Garbage Collector.
This is not necessarily true. Many games are (or were) developed in C++ primarily for portability and performance reasons. But this reality has changed considerably with game engines like Unity3d. Today it is more common to develop the game almost entirely in one of these tools, and use C++ only for critical code (for example, some special crash test that needs to be done, or some calculation of Artificial Intelligence). As already explained to you in other answers, even a language in which you do not have GC vc may have problems if you plan incorrectly the use of memory.
Of course I know that several games are made in C# and Java.
Precisely because of the productivity obtained with tools and/or libraries that use these languages. But I still doubt that the main motivator of use is the existence or not of GC.
I imagine it’s the pause that causes trouble. What I really want to know is how is resolved the pause in the development of a game made in these languages.
I don’t understand what pause you are referring to. Is it the game break, or the interval that GC takes to remove the no longer referenced memory? If you were referring to the first, there is no reason for this to be a problem because during this time no object is created or deleted (supposedly, of course - after all, you may have made mistakes in your pause implementation). If you were referring to the second (which I think most likely), the accepted answer explains everything to you in technical detail but the essence is in: "produce less waste and make the GC work in your favor".
In a game, "produce garbage" is to create objects (i.e., allocate memory) and then throw them away (i.e., fail to reference that memory). It’s a very easy thing to do. For example, imagine a simple game where you shoot planes with an anti-aircraft battery from the ground. The game will constantly instantiate:
- Aeroplanes
- Projectiles launched by anti-aircraft battery
Planes and shells are both destroyed when the first is hit by the second, right? In addition, the aircraft will eventually leave the screen if they are not hit, just as the projectiles will exit the screen if they do not hit a target. If you instantiate new objects from these classes and destroy them when an impact occurs or when they leave the screen, you you won’t have memory leaks (i.e., memory eternally trapped and unusable), and your game will work super well. For simple games where few of these objects are instantiated, you don’t have to worry and can let the GC do its work the way it was designed.
But what if your game is complex enough for you to have many of these objects? In this case, it may not make sense to create and destroy objects continuously, as the load on the GC will be very large and constant (that is, you will be producing a lot of garbage and not making the GC work in your favor). An alternative in such cases is to use "tanks" (pools in English) of pre-created objects that are relocated after use (for example, the same bullet that exits the screen is made invisible and "transported" again to the mouth of the cannon to be fired again).
Leaving pre-created objects helps in performance because it doesn’t overload the GC, but of course memory has its limits and you can’t just let this "tank" grow indefinitely. So relocating objects is usually a good practice.
Finally, this has to be done carefully, considering not only the technical aspects discussed here, but also the player’s experience (which is something very important in this type of software). A classic example is the bullet holes left on the walls of Counterstrike. They are probably objects held in one of these types of "tanks" and are relocated when the "tank" runs out. It works well, but has already caused immersion breakage in players who have literally tried to write on walls with shots (the initial letters faded as the bullet hole objects were relocated). :)
I’m also interested in games and those languages - I don’t understand why "pause" would cause any problems - what pause are you talking about?
– Daniel
@Danielgomes you appear on the set in a machine, sends to another information, the other player will see you to shoot, but before showing starts a collection, the game is delayed because of this, then appears, he takes a shot, only in your machine did not pause, you are no longer there. Should you take the shot? It gets weird. If you should not the other player failed to kill you because the software did not leave and not because of his inability.
– Maniero
The Go language has devoted much effort to lessening the pause caused by the garbage collector so as not to be a problem.
– epx