I have created server application that has to process some data (that is quite heavy process) before it starts listening clients. In the result it allocates memory (approximately 30MB) that will never be used but once.
Will it give me a benefit to force garbage collection? Or let GC do its work?
Also I have to use .NET 3.5 and as far as I know garbage collection will interrupt thread that’s why I came to forcing Garbage Collector.
OK look, garbage collection is still just a heuristic, it’s trying to do the best guess it can in general, but it may be possible that you have a legitimate claim at knowing better than it at particular times.
Take for example a game with a lot of assets. When you switch to a new zone, you pop up a loading screen and start loading your new data, releasing references to the old data. It won’t get collected yet because you haven’t hit your threshold, but you might hit it soon as you’re starting to create your little matrices every frame to present your game, and then you’ll get a (possibly noticeable) stutter as you’re unloading hundreds of megs of data and moving everything else around to compact the queues.
Now you can instead force a collect after the new assets were loaded, since the user is already in “idle” mode, staring at your progress bar, he won’t notice the little stutter and your game will seem smoother overall afterwards.
The real trick then becomes knowing when to interfere and when not to. When in doubt, don’t — that collection will happen regardless, and making it happen more often will only make your application seem more stuttery. You need something to “hide” it, like a long task that locks your application with a progress bar that somehow generates a lot of finalized objects — really a game loading screen is the only thing I can come up with, but YMMV.