Ways of Performance in .NET - Part1

book.anytao.com | Anytao in chinese 

[Anytao]Ways of Performance in .NET - Part1

Published 30 May 2008, by Anytao
© 2008 Anytao.com, A fun world for making tech as art. 

I'm anytao, Visual C# MVP of Microsoft, and come from China.

Contents

21 different ways about performance in .NET

How to implement the rules to improve the performance

 
Introduction

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.

Performance Rules

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();
 
    //Create WeakReference
    WeakReference wr = new WeakReference(mc);
    //Remove the strong reference
    mc = null;
 
    if (wr.IsAlive)
    {
        //Weak Reference change into a strong reference
        //object can be used again
        mc = wr.Target as MyClass;
    }
    else
    {
        //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;
    try
    {
        fd = new FileDealer(new IntPtr(), new ManagedRes());
        fd.Read();
    }
    finally
    {
        if(fd != null)
            fd.Dispose();
    }
}

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()))
    {
        fd.Read();
    }
}

Of course, using is best.

Another rules will come soon...:-)

Tao | Inside Necessary .NET

www.anytao.com  | Blog: http://anytao.cnblogs.com/

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.

Published Friday, May 30, 2008 2:08 AM by anytao
Filed under: , , ,

Comments

# re: Ways of Performance in .NET - Part1

Thursday, May 29, 2008 1:48 PM by anytao

I'm the first reader of myself.

# re: Ways of Performance in .NET - Part1

Thursday, May 29, 2008 2:15 PM by jillzhang

i'm the next one!

# re: Ways of Performance in .NET - Part1

Thursday, May 29, 2008 4:53 PM by chandana

thanks, very informative

# re: Ways of Performance in .NET - Part1

Thursday, May 29, 2008 9:33 PM by yyliuliang

good

look forward to more :)

# re: Ways of Performance in .NET - Part1

Thursday, May 29, 2008 9:47 PM by anytao

These days, I'll post the last articles about this theme:)

# re: Ways of Performance in .NET - Part1

Thursday, May 29, 2008 11:22 PM by hjf1223

I'm here

# re: Ways of Performance in .NET - Part1

Friday, May 30, 2008 9:31 AM by wingoo

:)

# re: Ways of Performance in .NET - Part1

Thursday, July 03, 2008 10:23 AM by clingingboy

I'm here:)

hoho,

this' my first time reply a comment in here.

# re: Ways of Performance in .NET - Part1

Thursday, July 03, 2008 10:59 PM by dean

chinese?

# re: Ways of Performance in .NET - Part1

Thursday, July 03, 2008 11:15 PM by anytao

Yes, I'm from Beijing.

# re: Ways of Performance in .NET - Part1

Thursday, September 25, 2008 7:04 AM by baobao

wo zhi hui yong pin yin!

# 我的2008,专注而行

Tuesday, December 30, 2008 11:10 AM by Anytao

气象更新,总有小结。多事之秋,随着寒风而过,虽有很多记忆难眠,但未来还在挺进。对2008做个小结,就像一本年历到了该翻过的时候,应该擦擦干净为来年打个好头。2008,对国家和世界来说,都无疑是一个多事之年,这已无须一届书生多言,留在心里的记忆应该是永恒的。千言万语,唯有祝福在此,擦亮双眼继续技术之路的多味人生。

# re: Ways of Performance in .NET - Part1

Wednesday, December 31, 2008 12:42 AM by Jun1st

dude, you do need to have your English improved.

"A good performance system is one of the important score in the users’ survey"--Typical Chinglish

I try to point some issues out for you, but It seems a refactor is necessary.Each sentence has one or two grammer issues

# re: Ways of Performance in .NET - Part1

Wednesday, December 31, 2008 1:20 AM by anytao

I try my best and hope change in future.

# re: Ways of Performance in .NET - Part1

Sunday, February 22, 2009 5:04 PM by smlfrykkuy

P0tSDN  <a href="afyabqilnuhg.com/.../a>, [url=http://eepvvggetrwg.com/]eepvvggetrwg[/url], [link=http://gyrwvdcjjkfb.com/]gyrwvdcjjkfb[/link], http://wpekazyhcjxb.com/

Leave a Comment

(required) 
(required) 
(optional)
(required)