How to handle many updating objects efficiently in C#?

后端 未结 6 1171
感情败类
感情败类 2021-01-05 00:34

I\'m developing a 2D overhead shooter game using C# and XNA. I have a class that I\'ll call \"bullet\" and need to update many of these instances every fraction of a second.

6条回答
  •  独厮守ぢ
    2021-01-05 01:28

    Your stack based solution is pretty close to a class I wrote to generically do this sort of resource pooling:
    http://codecube.net/2010/01/xna-resource-pool/

    You mentioned that this makes the problem mostly go away, but it still crops up here and there. What's happening is that with this stack/queue based method of pooling, the system will reach a point of stability once you are no longer requesting more new objects than the pool can supply. But if the requests go higher than your previous max # of requested items, it will cause you to have to create a new instance to service the request (thus invoking GC from time to time).

    One way you can side-step this is to go through and pre-allocate as many instances as you think you might need at the peak. That way, you won't have any new allocations (at least from the pooled objects), and the GC won't be triggered :-)

提交回复
热议问题