简体   繁体   English

C#和.Net垃圾收集器性能

[英]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. 我正在尝试用C#和.NET制作游戏,并且我计划实现更新游戏世界中游戏对象的消息。 These messages would be C# reference objects. 这些消息将是C#引用对象。

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. 消息类本身很小,最多只有4或5个成员。

These messages will be generated a couple of times per second for each object in the game world. 对于游戏世界中的每个对象,这些消息将每秒生成几次。

In .NET the garbage collector has 3 generations, generation 0, generation 1 and generation 2. Every Time the GC fails to collect an object in a generation, that object will be promoted to the next generation. 在.NET中,垃圾收集器有3代,第0代,第1代和第2代。每次GC无法在一代中收集对象时,该对象将被提升为下一代。

You could potentially run into issues if your objects are larger than 85kb. 如果对象大于85kb,则可能会遇到问题。 These objects will be automatically stored in the large object heap. 这些对象将自动存储在大对象堆中。 The large object heap will be automatically collected in the following situations: 在以下情况下将自动收集大对象堆:

  • Allocations exceed the large object heaps threshold. 分配超过大对象堆阈值。
  • System is in a low memory situation. 系统处于低内存状态。
  • System.GC.Collect is called on generation 2. 在第2代调用System.GC.Collect

The problem is that when the large object heap is collected, the memory for the objects is deallocated but the LOH is not compacted. 问题是,当收集大对象堆时,对象的内存被释放,但LOH没有被压缩。 Because the LOH is fragmented, you could potentially get SystemOutOfMemory exceptions thrown if there isn't a space big enough for your object on the LOH. 由于LOH是碎片化的,如果没有足够大的空间用于LOH上的对象,则可能会抛出SystemOutOfMemory异常。

Techniques such as object pooling are commonly used to improve performance of the LOH. 诸如对象池之类的技术通常用于改善LOH的性能。 http://en.wikipedia.org/wiki/Object_pool_pattern http://en.wikipedia.org/wiki/Object_pool_pattern

Source: http://msdn.microsoft.com/en-us/magazine/cc534993.aspx 来源: http//msdn.microsoft.com/en-us/magazine/cc534993.aspx

UPDATE: .Net 4.5.1 will allow you to do on-demand compaction of the LOH within your application using the GC.Collect API. 更新: .Net 4.5.1允许您使用GC.Collect API在您的应用程序中按需压缩LOH。

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. 更高版本的GC,但更确切地说是4.5,在0级和1级代码中异步运行。这大大降低了GC的影响。

If your objects are short-lived they should not pass from generation level 0 most of the time. 如果您的对象是短暂的,它们不应该在大多数时间从第0代传递。 Level 0 is the fastest generation to clean up by the GC. 0级是GC清理最快的一代。

Bottom line, I would not consider prematurely optimizing my code for fear of GC performance. 最重要的是,我不会因为担心GC性能而过早地优化我的代码。

Premature optimization is the root of all evil by DonaldKnuth 过早的优化是 DonaldKnuth 所有邪恶的 根源

Personally, i'd recommend this article for a deeper understanding 就个人而言,我建议这篇文章有更深入的了解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM