开发者

C# Production Server, Do I collect the garbage?

I know there's tons of threads about this. And I read a few of them.

I'm wondering if in my case it is correct to GC.Collect();

I have a server for a MMORPG, in production it is online day and night. And the server is restarted every other day to implement changes to the production codebase. Every twenty minutes the server pauses all other threads, and serializes the current game state. This usually takes 0.5 to 4 seconds

Would it be a good idea to GC.Collect(); after serialization?

The server is, obviously, constantly creating and destroying game items.

Would I have a notorious gain in performance or memory optimization / usage?

Should I not manually collect?

I've read about how collecting can be bad if used in the wrong moments or too frequently, but I'm thinking these saves are both a good moment to collect, and not that frequent.

The ser开发者_Go百科ver is in framework 4.0

Update in answer to a comment:

We are randomly experiencing server freezes, sometimes, unexpectedly, the server memory usage will raise increasingly until it reaches a point when the server takes way too long to handle any network operation. Thus, I'm considering a lot of different approaches to solve the issue, this is one of them.


The garbage collector knows best when to run, and you shouldn't force it. It will not improve performance or memory optimization. CLR can tell GC to collect object which are no longer used if there is a need to do that.

Answer to an updated part: Forcing the collection is not a good solution to the problem. You should rather have a look a bit deeper into your code to find out what is wrong. If memory usage grows unexpectedly you might have an issue with unmanaged resources which are not properly handled or even a "leaky code" within managed code.

One more thing. I would be surprise if calling GC.Collect fixed the problem.


Every twenty minutes the server pauses all other threads, and serializes the current game state. This usually takes 0.5 to 4 seconds

If all your threads are suspended already anyway you might as well call the garbage collection, since it should be fairly fast at this point. I suspect doing this will only mask your real problem though, not actually solve it.

We are randomly experiencing server freezes, sometimes, unexpectedly, the server memory usage will raise increasingly until it reaches a point when the server takes way too long to handle any network operation. Thus, I'm considering a lot of different approaches to solve the issue, this is one of them.

This sounds more like you actually are still referencing all these objects that use the memory - if you weren't the GC would run due to the memory pressure and try to release those objects. You might be looking at an actual bug in your production code (i.e. objects that are still subscribed to events or otherwise are being referenced when they shouldn't be) rather than something you can fix by manually taking out the garbage.

If possible in this scenario you should run a performance analysis to see where your bottlenecks are and what part of your code is causing the brunt of the memory allocations.


Could the memory increase be an "attack" by a player with a fake/modified game-client? Is a lot of memory allocated by the server when it accepts a new client connection? Does the server handle bogus incoming data well?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜