Increase your ASP.NET performance : code view
while considering perfomance, We need to to understand the places where bottlenecks typically occur, the causes of the bottlenecks, and the steps to take prevent the bottlenecks from occurring in your application. This article helps you to improve your code performace. Learn how to increase your ASP.NET performance : code view
Increase your ASP.NET performance : code view
Before releasing any big project. Performance is the major mile stone. while considering perfomance, We need to understand the places where bottlenecks typically occur,the causes of the bottlenecks, and the steps to take to prevent the bottlenecks from occurring in your application. A combination of sound architecture and design, best practice coding techniques.
There are certain things you should take into account when you are developing your applications.These things include page and control issues, caching, resource management, session and view state issues, threading, exception and string management, COM interop, and more.
There are lot of factors affecting performance. In this article we cover code view only.
Disabled session state
Disable Session State if you're not going to use it. By default it's on. You can actually turn this off for specific pages, instead of for every page. You can also disable it across the application in the web.config by setting the sessionState mode set to Off.
This is read-only property that indicates if the client has reset the connection to the server. if a long period of time has elapsed between a client request was made and the server responded, it may be beneficial to make sure the client is still connected before continuing to process.
Before deploy our code on server, we need to change configuration file. Set compilation debug="false" and trace enabled="false"
as this affect performance. set this value to true only during development. 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.
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. Turn off Tracing unless until required.
Turn off viewstate
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.
If your form is not postback then trun off viewstate for that perticular form. Bydefault turn on viewsate and slow down your site
Deploy in RELEASE mode
code needs to deploy in RELEASE not in DEBUG mode. it has additional overhead of creating PDB file in bin folder. this holds debug information. DEBUG mode is used for while development
it has high timeout, while debugging such that your process hangs on until you get to the exact break point.
selecting Release Mode will greatly improve the performance of the application when you deploy.
Avoid frequent round trips to database
Calls made to Database quite expensive in terms of response time and resources. It can be avoided by using Batch Processing. Use of dataset is useful wherever applicable since, datadapter automatically opens and closes Connection whenever required and does not need to open connection explicitly.
Take care while using datareader, it works in connected Architecture where connection has open and close explicitly. A number of connections opened and not closed adequately can directly influence in performance slow down.
Validate all user input
user inupts are evil must be validated before sending to server. Wrong, bad inputs may be slow down process. Use client side validations avoid server side validations.
Server-Side will just consume valuable resources on your servers, and cause more chat back and forth.
Page.IsPostBack basic thing
Make sure you don't execute code needlessly. I don't know how many web developers forget about checking IsPostBack, It seems like such a basic thing.
avoid Needless processing. use this in Page_Load. Page.IsPostback property to avoid unnecessary server processing on a roundtrip, reducing network traffic.
This Stringbuilder class is faster than string. When you want to append or concatenate text to your string this is good option.
let's take an example, i have a string say str = "Hello".
Then i am appending to it as str = str + " World"; here i am actually creating two instances of string in memory. Both the original as well as the new string will be stored in the memory.
use string builder instead. using a StringBuilder as str.Append(" World"); which only stores the value in the original string and no additional reference is created.
StringBuilder is designed to allow you to pre-allocate some memory and then assign characters to it. StringBuilder will monitor the usage of the memory buffer and if it needs more it will automatically re-allocate
Use Finally if Try used
In your code if Try..Catch.. Block used, always use the Finally method to close Open connections, close DataReaders, Files and other resources with unmanaged objects.
Finally always get executed whether exception occurred or not. We can implement dispose method kill resources in finally block.
autoEventWireup to False
AutoEventWireup is a Boolean attribute that indicates whether events of a Web Forms page are autowired. The default value for AutoEventWireup is TRUE.
When you explicitly set AutoEventWireup to true, Visual Studio .NET, by default, generates code to bind events to their event-handler methods.
At the same time, the ASP.NET page framework automatically calls the event-handler methods based on their predefined names. This can lead to the same event-handler method being called two times when the page runs and the performance gets affect.
If the AutoEventWireup is set to false, the runtime does not have to look for each of the page event handlers. Set AutoEventWireup to false
For all pages we can make AutoEventWireup to false in web.config
'using' statment to dispose resources
'using' will help you to dispose the objects. The using statement defines a scope at the end of which an object will be disposed even
if an exception is thrown, please note that the object that you are trying to dispose should implement 'System.IDisposable'.
The following code bit demonstrates this.
There are lot of other things affecting performance. we will consider it in later articles.
Suggestion are most welcome
Really it is a good article.