Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Production Server, Do I collect the garbage?

Tags:

c#

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 server 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.

like image 757
bevacqua Avatar asked May 07 '11 22:05

bevacqua


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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.

like image 163
Novitzky Avatar answered Sep 21 '22 22:09

Novitzky