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 !!!

Print | posted on Thursday, June 2, 2005 11:48 AM

Comments on this post

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Awesome List. I am bookmarking this page.

-Rajeev
Left by Rajeev Narang on Jul 22, 2005 10:13 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Very nice, just what I was looking for.
Left by heather on Aug 09, 2005 1:10 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
very usefull for ma, thanks.
Left by Dinesh on Oct 13, 2005 6:56 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Excellent tips. Thanks for sharing.
Left by Kamath on Nov 21, 2005 3:21 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Amazing,delighted me,thanks
Left by Anand on Dec 30, 2005 2:21 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Great, I have applied some tricks and it works great. Thanks
Left by viral on Apr 13, 2006 8:13 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Thanks.....I was really worried about it.
Left by Jamal Afroz on Aug 20, 2006 1:05 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Thants, for wanderfull Tips
Left by Ala Salameh on Dec 07, 2006 2:53 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Thanks, Very useful
Left by Srikanth on Mar 06, 2007 5:19 AM

# thank you

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

# re: Tips for ASP.NET Application Performance Enhancement

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

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Grate job..
Left by Gurmeet on Jan 10, 2008 6:27 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Great Article!... Many Thanks. :)
Left by Rajaprathap on Feb 15, 2008 6:06 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
But my site is still very slow plz help it using ajax controls http://ignouhelpline.info
Left by Nazre on May 02, 2008 6:07 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Great Article . really it is good for developer .
Left by Rajnish on Aug 29, 2008 12:13 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
thanks, Very Usefull Article
Left by PC on Sep 17, 2008 2:37 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
All tips are very nice and useful specially for fresher and those who have less experience.
Left by Ajit on Oct 07, 2008 7:17 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Really help full
Left by Mukeri on Oct 18, 2008 5:14 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
thanks, it is really good.
Left by aswath on Dec 29, 2008 3:35 PM

# re: Tips for ASP.NET Application Performance Enhancement

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

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
I implement this tricks in my application and my application speed is increased.
Left by shrikant on Feb 21, 2009 2:03 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Nice Job very useful
Left by Venkey on Mar 16, 2009 5:06 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting 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
Left by Dobedani on Mar 27, 2009 10:35 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting 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
Left by Saif ur rehman saifi on Apr 14, 2009 9:56 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Great Tips! Really these tips are very useful for me. Thanks for sharing very useful information.
Left by .net development on Jun 25, 2009 2:37 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Nice Collection, thanks for sharing them :)
Left by Nisha on Jul 07, 2009 7:37 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Thanks, It is really good.............
Left by Narinder on Sep 21, 2009 1:41 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting 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 ....
Left by narinder on Sep 21, 2009 1:57 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting 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
Left by mario oyunları on Sep 21, 2009 10:36 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting 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?
Left by Vidya on Sep 28, 2009 10:11 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Its really very nice post... very informative...
Thanks for the post.
Left by Deepu on Oct 29, 2009 12:55 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting 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
Left by Aryan Pratap on Nov 04, 2009 12:20 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Nice article..Thanks for sharing..I guess you have missed one important point and that is "Publish Website" Feature this will increase performance of asp.net web site.
Left by DotNetSpark on Nov 10, 2009 12:29 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
It was a really good and very helpful article
Left by Sanjay Gupta on Nov 25, 2009 4:10 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Its very good article thats helped me to improve my application performance.
Left by Girijesh Dixit on Dec 06, 2009 5:59 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Very Useful Tips !!!! Thanks For Sharing !!!!
Left by Belal Ahmed on Dec 09, 2009 9:57 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Very Helpfull
Left by vivek on Jan 03, 2010 6:14 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
In addition to the above points, if the viewstate has to be disabled then we can follow the below instructions:

Limiting the size of ASP.NET ViewState to improve performance:
ViewState works by adding hidden input fields to the Response, which helps maintain the state of objects in the page. But on high-volume Web sites where every byte of network bandwidth counts, you may want to limit the ViewState size—especially when you consider that ViewState is not only sent out to the client but also gets sent back to the server with each request. In essence, every byte added to a page by ViewState causes two bytes of network traffic, one in each direction.

ViewState is enabled on four levels: machine, application, page, and control. The machine and application levels are controlled by configuration file settings, while the page and control ViewState settings are controlled by properties. Most sites that use ASP.NET will use ViewState in at least portions of the site, so turning it off at the machine or application level often is not feasible. Therefore, ViewState must be handled carefully at the page and control levels.
To maximize performance, each page should be judged on whether it needs ViewState information. If no ViewState is needed in the page or any of its controls, set the EnableViewState property of the page or control to false. Also, remove the runat="Server" attribute from the form tag. If this tag isn't removed, the page itself passes on about 20 bytes of information to the ViewState.

If ViewState is needed, make sure it is enabled only for controls that are really going to use it. Some controls, such as Label and TextBox, do not have a large ViewState footprint. But DataGrids and DataRepeaters store all of their data in ViewState by default, making the page's output potentially very large.

If you want to use one of these larger footprint controls, you can reduce its size by setting the EnableViewState property to false. This will force the DataGrid to go to the datasource for its data with each request, which of course impacts whatever datasource you're using. But it will greatly reduce the size of the HTML stream sent to the client.
Left by Niks on Jan 13, 2010 5:40 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
stupid article
Left by granny goose on Mar 09, 2010 3:46 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
No4 - Caching is a good technique for boosting performance but be aware that ASP.NET's own cache has drawbacks. It is single point of failure, it causes data integrity problems in web farms/gardens and more. so you should avoid these problems by using a third-party distributed cache like NCache Express .

It is free and requires no code change at all
Left by steve on Apr 01, 2010 5:32 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
very nice post
Left by gratta e vinci on Apr 05, 2010 2:32 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Thanks Buddy
Left by Sabhari on May 27, 2010 12:46 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Good job....
Really helpfull for me...
Left by Pawitter on Aug 09, 2010 4:34 PM

# Thanks

Requesting Gravatar...
Keep this site strong and maintain so that we can always post here everyday thanks for providing this page.
Left by SJR Luxuria on Jul 05, 2011 12:17 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
nice tips
Left by Vishnu on Jul 12, 2011 4:59 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Nice article thanks for sharing useful data. I found another useful article on “improve asp.net performance and scalability” and I'm sharing this with you.
Left by Beezus on Aug 09, 2011 4:23 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Wow this is a great blog contest! A lot would be really interested at this, really. Most writers are women and would really be comfortable in writing about beauty. So expect lots of entries.
Left by Barton on Aug 19, 2011 10:23 PM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
a nice article.
I am a student from China and study .NET for one year. Now, I want to study more,what should I do?
thank you
Left by xiaoyi on May 02, 2012 7:15 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
Recently i had developed a web application and the performance of the application is slow.From this post i got the information on how to enhance the performance of my application.I'm thankful to you for sharing such a great post.
Left by Apartments in gachibowli on Sep 07, 2012 10:01 AM

# re: Tips for ASP.NET Application Performance Enhancement

Requesting Gravatar...
very useful & great
Left by sai on Oct 05, 2012 5:15 PM

Your comment:

 (will show your gravatar)