Search code examples
c#garbage-collection

C# and .Net Garbage collector performance


I am trying to make a game in C# and .NET, and I was planning to implement messages that update the game objects in the game world. These messages would be C# reference objects.

I want this approach because doing it this way would be easier to send them over a network if I want the game to be multiplayer.

But if I have a lot of messages, won't it be quite stressful for the garbage collector? And won't that affect gameplay? The message classes themselves are quite small with 4 or 5 members at most.

These messages will be generated a couple of times per second for each object in the game world.


Solution

  • The GC in later versions, but more precisely 4.5, runs asynchronously for generations level 0 and 1. This has highly reduced the impact of GC.

    If your objects are short-lived they should not pass from generation level 0 most of the time. Level 0 is the fastest generation to clean up by the GC.

    Bottom line, I would not consider prematurely optimizing my code for fear of GC performance.

    Premature optimization is the root of all evil by DonaldKnuth

    Personally, i'd recommend this article for a deeper understanding