Tips for ASP.NET Application Performance Enhancement

 

Hi All,

Though I have written about this some time back, Performance and Scalability stand next to Security in the case of Applications and is the most sought after topic wherever people raise queries.

I have compiled a set of Tips and Tricks to improve the performance of ASP.NET Applications in particular, where I have drilled down and explored a lot and have faced crunch situations from which I keep learning.

These tips can be followd during deploying ur app on the production server

A Foreword is that, these are general tips and are not exhaustive and its always better to share any new thoughts whenever we arrive at them, which will benefit the rest of the community.

Now, lets drill down into the Tips :)

In the Application Web.Config File

1. Set debug=false under compilation as follows:-

<compilation defaultLanguage="c#" debug="false">

When you create the application, by default this attribute is set to "true" which is very useful while developing. However, when you are deploying your application, always set it to "false"

Setting it to "true" requires the pdb information to be inserted into the file and this results in a comparatively larger file and hence processing will be slow.

Therefore, always set debug="false" before deployment.

2. Turn off Tracing unless until required.

Tracing is one of the wonderful features which enable us to track the application's trace and the sequences. However, again it is useful only for developers and you can set this to "false" unless you require to monitor the trace logging. You can turn off tracing as follows:-

<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>

3. Turn off Session State, if not required.

ASP.NET Manages session state automatically. However, in case you dont require Sessions, disabling it will help in improving the performance.

You may not require seesion state when ur pages r static or whn u dont need to store infor captured in the page.

You can turn off session state as follows:-

<sessionstate timeout="20" cookieless="false" mode="Off" stateconnectionstring="tcpip=127.0.0.1:42424" sqlconnectionstring="data source=127.0.0.1;Trusted_Connection=no">

While developing using Visual Studio.NET

1. Select the Release mode before making the final Build for your application. This option is available in the Top Frame just under the Window Menu option. By default, the Mode is Debug

There are several things happening when you Build/Re-Build applications in the Debug Mode. First of all, it creates an additional PDB File under your BIN directory. This holds all the Debug information.

Secondly, the Timeout is very high since you require higher time out frequency while debugging such that your process hangs on until you get to the exact break point where error is there.

So, selecting Release Mode will greatly improve the performance of the application when u deploy.

General Methods to improve the Performance

1. Disable ViewState as and when not required.
ViewState is a wonderful technique which preserves the state of your form and controls. However, its a overhead since all the information needs to be stored in the viewstate and particularly if you are building applications which target Dial Up Internet Connection Users, ViewState can make your application very slow. In case you dont require viewstate, disable it.

You can disable it at different levels ex., for Page, Control etc., by setting
EnableViewState="false"

2. Avoid Frequent round trips to the Database.
Calls made to Database can be quite expensive in terms of response time as well as resources and it can be avoided by using Batch Processing.

Make calls to Database as mininal as possible and make them last even lesser time. Use of DataAdapter wherever applicable is very useful since, it automatically opens and closes Connection whenever required and doesnt require user to explicitly open the connection.

A number of connections opened and not closed adequately can directly influence in performance slow down.

3. Avoid Throwing Exceptions.
Exceptions are a greate way to handle errors that occur in your application logic. However, throwing exceptions is a costly resource and must be avoided. Use specific exceptions and use as minimal as possible to avoid resource overhead.

For example, catching a SQLException is better when you expect only those kind of exceptions instead of a generic Exception.

4. Use Caching to improve the performance of your application.
OutputCaching enables your page to be cached for specific duration and can be made invalid based on various paramters that can be specified. The Cache exists for the duration you specify and until that time, the requests do not go to the server and are served from the Cache.

Do not assign cached items a short expiration. Items that expire quickly cause unnecessary turnover in the cache and frequently cause more work for cleanup code and the garbage collector.

In case you have static as well as dynamic sections of your page, try to use Partial Caching (Fragment Caching) by breaking up your page into user controls and specify Caching for only those Controls which are more-or-less static.

For more details regarding caching look into
ASP.NET Caching Features

5. Use appropriate Authentication Mechanism.
The Authentication Mechanism you choose determines the cost associated with it and hence select the appropriate mechanism. An informal but useful order is as follows:-

Authentication Modes

1. None
2. Windows
3. Forms
4. Passport

6. Validate all Input received from the Users.
User Input is Evil and it must be thoroughly validated before processing to avoid overhead and possible injections to your applications. Use Client Side Validations as much as possible. However, do a check at the Server side too to avoid the infamous Javascript disabled scenarios.

7. Use Finally Method to kill resources.
In your Try..Catch.. Block, always use the Finally method to close Open connections, Open DataReaders, Files and other resources such that they get executed independent of whether the code worked in Try or went to Catch.

The Finally method gets executed independent of the outcome of the Block.

8. The String and Stringbuilder Magic.
Perhaps the most ignored type in .NET is the stringbuilder. I am sure many of us are not even aware of Stringbuilder and its advantage over string (atleast I didnt know for 1 year :))

String is Evil when you want to append and concatenate text to your string. In other words, if you are initially creating a string say s = "Hello". Then you are appending to it as s = s + " World"; You are actually creating two instances of string in memory. Both the original as well as the new string will be stored in the memory. For that matter, all the activities you do to the string are stored in the memory as separate references and it must be avoided as much as possible.

Use StringBuilder which is very useful in these kind of scenarios. For the example above, using a StringBuilder as s.Append(" World"); which only stores the value in the original string and no additional reference is created.

9. Avoid Recursive Functions / Nested Loops
These are general things to adopt in any programming language, which consumes lot of memory. Always avoid Nested Loops, Recursive functions, to improve performance.

Having said that, proper functions and call backs do increase the performance instead of having a huge chunk of lines of code in single method.

The above are just pointers to improve the performance of your application and are just illustrative. There are many more ways in which you can improve the performance of your applications. I havent dealt with IIS and SQL Server side tips to improve performance which I would explain in my forthcoming articles.

The above are my view and a collective response from various resources and you are welcome to share your views / corrections if any.

Cheers and Happy Programming !!!

posted @ Thursday, June 02, 2005 11:48 AM

Print

Comments on this entry:

# re: Tips for ASP.NET Application Performance Enhancement

Left by Rajeev Narang at 7/22/2005 10:13 PM
Gravatar
Awesome List. I am bookmarking this page.

-Rajeev

# re: Tips for ASP.NET Application Performance Enhancement

Left by heather at 8/9/2005 1:10 PM
Gravatar
Very nice, just what I was looking for.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Dinesh at 10/13/2005 6:56 AM
Gravatar
very usefull for ma, thanks.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Kamath at 11/21/2005 3:21 AM
Gravatar
Excellent tips. Thanks for sharing.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Anand at 12/30/2005 2:21 AM
Gravatar
Amazing,delighted me,thanks

# re: Tips for ASP.NET Application Performance Enhancement

Left by viral at 4/13/2006 8:13 AM
Gravatar
Great, I have applied some tricks and it works great. Thanks

# re: Tips for ASP.NET Application Performance Enhancement

Left by Jamal Afroz at 8/20/2006 1:05 PM
Gravatar
Thanks.....I was really worried about it.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Ala Salameh at 12/7/2006 2:53 PM
Gravatar
Thants, for wanderfull Tips

# re: Tips for ASP.NET Application Performance Enhancement

Left by Srikanth at 3/6/2007 5:19 AM
Gravatar
Thanks, Very useful

# thank you

Left by coucou at 8/20/2007 9:37 PM
Gravatar
thank for for this helpful tips.
is there a way to increase the application timeout? (not sessions).

# re: Tips for ASP.NET Application Performance Enhancement

Left by Zahid Kakar at 12/30/2007 6:22 PM
Gravatar
thanks, Very Usefull Article, we are looking forward to have such article about IIS and SQL Server.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Gurmeet at 1/10/2008 6:27 PM
Gravatar
Grate job..

# re: Tips for ASP.NET Application Performance Enhancement

Left by Rajaprathap at 2/15/2008 6:06 PM
Gravatar
Great Article!... Many Thanks. :)

# re: Tips for ASP.NET Application Performance Enhancement

Left by Nazre at 5/2/2008 6:07 AM
Gravatar
But my site is still very slow plz help it using ajax controls http://ignouhelpline.info

# re: Tips for ASP.NET Application Performance Enhancement

Left by Rajnish at 8/29/2008 12:13 AM
Gravatar
Great Article . really it is good for developer .

# re: Tips for ASP.NET Application Performance Enhancement

Left by PC at 9/17/2008 2:37 AM
Gravatar
thanks, Very Usefull Article

# re: Tips for ASP.NET Application Performance Enhancement

Left by Ajit at 10/7/2008 7:17 PM
Gravatar
All tips are very nice and useful specially for fresher and those who have less experience.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Mukeri at 10/18/2008 5:14 PM
Gravatar
Really help full

# re: Tips for ASP.NET Application Performance Enhancement

Left by aswath at 12/29/2008 3:35 PM
Gravatar
thanks, it is really good.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Azhar Saiyed at 1/23/2009 4:48 PM
Gravatar
It is nice,
It is very useful to improve our web performance.
I would like to call to this point as Bullet points.

# re: Tips for ASP.NET Application Performance Enhancement

Left by shrikant at 2/21/2009 2:03 PM
Gravatar
I implement this tricks in my application and my application speed is increased.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Venkey at 3/16/2009 5:06 PM
Gravatar
Nice Job very useful

# re: Tips for ASP.NET Application Performance Enhancement

Left by Dobedani at 3/27/2009 10:35 PM
Gravatar
Hi there,
May I add something, please? I am storing a web control in the Application object and a Dictionary in the Session object of my latest site. My site was awfully slow, esp. those pages where I was accessing the web control. When I added try finally blocks to my methods where I set the relevant references to null and called System.GC.Collect(), the performance greatly improved. HTH
Grtz, Dobedani

# re: Tips for ASP.NET Application Performance Enhancement

Left by Saif ur rehman saifi at 4/14/2009 9:56 AM
Gravatar
it is very useful tips for the asp.net developers,
especially debug="false"
authentication mode and
caching usage
these are very excellant.
thanks a lot,
ur's
Saifi

# re: Tips for ASP.NET Application Performance Enhancement

Left by .net development at 6/25/2009 2:37 AM
Gravatar
Great Tips! Really these tips are very useful for me. Thanks for sharing very useful information.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Nisha at 7/7/2009 7:37 AM
Gravatar
Nice Collection, thanks for sharing them :)

# re: Tips for ASP.NET Application Performance Enhancement

Left by Narinder at 9/21/2009 1:41 AM
Gravatar
Thanks, It is really good.............

# re: Tips for ASP.NET Application Performance Enhancement

Left by narinder at 9/21/2009 1:57 AM
Gravatar
hello sir ,

I was browsing your web site and saw capthch in your web page which is exactly I want to use in my web site on which I am working right now .So, can you provide me your captcha code if you can then please mail me at this mail ID "narinder_kumar_goyal@yahoo.com"
I will be very thankful to you ....

# re: Tips for ASP.NET Application Performance Enhancement

Left by mario oyunları at 9/21/2009 10:36 AM
Gravatar
Dictionary in the Session object of my latest site. My site was awfully slow, esp. those pages where I was accessing the web control. When I added try finally blocks to my methods where I set the relevant references to null and called System

# re: Tips for ASP.NET Application Performance Enhancement

Left by Vidya at 9/28/2009 10:11 PM
Gravatar
Great Tips!!! Thanks for sharing.
Is there any way to find out which part of the asp.net application is slow, like Database, javascript or net code?

# re: Tips for ASP.NET Application Performance Enhancement

Left by Deepu at 10/29/2009 12:55 AM
Gravatar
Its really very nice post... very informative...
Thanks for the post.

# re: Tips for ASP.NET Application Performance Enhancement

Left by Aryan Pratap at 11/4/2009 12:20 AM
Gravatar
Really very Nice !!! I think you may include what is recommended under development and deployment which make it more fruitfull . Any way it help me a lot

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345