Best Answer Mario Vernari, 11 November 2013 - 12:55 PM
Hello Sean.
Although you cannot avoid the garbage collection process, you may write the code in a way to minimize its duration.
There's nothing difficult behind the trick, you should think what's the goal of the GC: clean up the no more useful data from the heap (ram). Thus, you should favor the endless and the long-life objects, other than the create-and-destroy pattern. It's much closer to the C/C++ programming practices, I guess.
You may take a look at some functions of my repository.
http://cetdevelop.co...lpers.Format.cs
These functions aren't particularly complex, and they could be written in a shorter yet elegant way. However, there's a pretty decent attention about the GC and the efficiency. If you check the "ToHex" function, for instance, there's no string concatenation, which is very memory intensive. Instead, there's an initial estimation of the final string length, and everything will spin around a (fixed-size) char array. So, not only the performance is far better, but the GC overhead is irrelevant.
Please, note that a relevant GC activity could lead to an out of memory exception, due the very high fragmentation and the relative inability to release useless blocks of memory. That is, the reliability of your app is better when the GC is minimal.
Ciao.
Go to the full post