[Anytao]Ways of Performance in .NET - Part1
|I'm anytao, Visual C# MVP of Microsoft, and come from China.|
21 different ways about performance in .NET
How to implement the rules to improve the performance
What is a good software product? Operation Process, User Experience, Security and Performance are all the necessary factors. A good performance system is one of the important score in the users’ survey. In the stock exchange center, there is tens of thousands of data exchange running in the stocker’s hand. Steadying running and effective performance is a must. At the same time, performance is also concerned in small software system, slow product always lose your face for customers.
Therefore, performance is important in system design. The influence in performance goes in different way, such as hardware, database, and software design. In this article, I’ll take attention to performance killer in .NET, list all general performance problem as items, including rules, habits, and syntax etc. According to all the performance item, you’ll get basic knowledge about how to improve your software from CLR and .NET Framework.
However, all the improvement items are on the basis of .NET foundation, not application area, such as Website Performance and Tuning, database improvement. OK, start our performance travel in our .NET world.
Item1: Use Dispose taking the place of Finalize in unmanaged resources collection process.
About the collection process of unmanaged resources are so different from manage resources in CLR. Basically, there are two way to collect them: Dispose & Finalize. Finalize have no exact implement time when running collection and also have performance loss in finalizing queue mechanism. In another hand, Dispose model gives programmer more control rights. We can control the exact run time and have no loss in other factors.
So, dispose model is recommended.
Item2: Select proper garbage collector: Workstation GC and Server GC.
.NET CLR implement two garbage collectors, different collector have different algorithm, design for different CPU: Workstation GC is used to handle the single CPU System as default in CLR, Server GC is used to handle the muti-CPU Server System. Thence, you can’t server a Workstation GC in muti-CPU in result of performance loss and unfit a high throughout capacity in parallel model.
Select proper GC is a key element in performance.
Item3: Use WeakReference for large object.
WeakReference is one of effective way to improve large object performance. Generally speaking, WeakReference is an intermediate state for an object. It means a WeakReference object can be collect by GC when it become garbage, and also can be invoke by application when necessary. That’s impossible for common object. Usually, large object need more memory to create. WeakReference will give a chance to GC when lack of memory, and also use this object as normal before its collection. It makes a possible for time and space.
In .NET, WeakReference class is used for this process. Target attribute refers to the large object and generate a strong reference to a variable. Here is the usage about WeakReference.
public void WeakRef()
MyClass mc = new MyClass();
WeakReference wr = new WeakReference(mc);
//Remove the strong reference
mc = null;
//Weak Reference change into a strong reference
//object can be used again
mc = wr.Target as MyClass;
//Create a new object to mc
mc = new MyClass();
Item4: Implement resource collection in using block.
The best practice of Dispose Model for resource collection is using block. It’s simplicity and elegance in syntax. And make sure to implement the dispose() method finally. The following is a non-using block expression for resource collectin:
public static void Main()
FileDealer fd = null;
fd = new FileDealer(new IntPtr(), new ManagedRes());
if(fd != null)
Using block give a more effective way to carry out this request as follows:
public static void Main()
using(FileDealer fd = new FileDealer(new IntPtr(), new ManagedRes()))
Of course, using is best.
Another rules will come soon...:-)
Tao | Inside Necessary .NET
Tao Wang is Senior Developer working on a chinese company and responsible for project framework design, software develop and management. Tao is proficient with CLR Essential, good command of ASP.NET、ADO.NET、XML、SQL Server, and skilled in object-oriented, design pattern. Here is my new book about .NET inside
|My new book Inside Necessary .NET|
See book.anytao.com in detail.
© 2008 Anytao.com
This posting is provided "AS IS" with no warranties, and confers no rights.