Improving the ASP.Net Application Performance - I
- 0 Comments
Performance tuning is a must if you are looking at wanting to improve performance. It’s no mean task for web based applications that involves a gamut of components , like HTML client, HTTP network, Web server, middle-tier components, database components, resource-management components, TCP/IP networks, and database servers. Performance tuning depends on a vast number of things ,and sometimes, by changing a single parameter, performance can shoot up commendably.
Some important tips to improve the performance are :
- Turn off Tracing unless until required. Enabling tracing adds performance overhead and might expose private information, so it should be enabled only while an application is being actively analyzed or in developing stage.
- Turn off Session State, if not required. Since ASP.NET Manages session state by default, you pay the cost in memory even if you don’t use it. i.e. whether you store your data in in-process or on state server or in a Sql Database, session state requires memory and it’s also time consuming when you store or retrieve data from it. You may not require session state when your pages are static or when you do not need to store information captured in the page.
In such cases where you need not use session state, disable it on your web form using the directive, <@%Page EnableSessionState=”false”%> - Disable View State of a Page if possible. There are a host of disadvantages to using view state. However, it increases the total payload of the pageat both times, when it is being served and it is being requested. There is also an extra cost incurred while serializing or deserializing view state data which is posted back to the server.
View state expands the memory allocations on the server. Several server controls, the most well renowned being the Data Grid, tend to bend towards greater usage of view state, even in cases where it’s not really warranted by the application requirement.
Pages that do not have any server post back events, can have the view state turned off. The default behavior of the ViewState property is enabled, but if you don’t need it, you can turn it off at the control or page level. Within a control, simply set the EnableViewState property to false, or set it globally within the page using this setting: <%@ Page EnableViewState=”false” %>
- Set debug=false in web.config Setting it to “true” needs the pdb information to be plugged into the file and this results in a comparatively sizable file and therefore, processing will take time.
- Avoid Response.Redirect Redirects are also very chatty. They should only be used when you are transferring people to another physical web server. Otherwise, use Server.Transfer.
- Avoid throwing exceptions Exceptions are probably one of the heaviest resource hogs and causes of slowdowns you will ever see in web applications, as well as windows applications.
Using as many try/catch blocks as you wish to isn’t a bad idea. Using exceptions richly is where you slip on performance. For example, you should avoidusing exceptions for control flow.
- Use Finally Method to kill resources. Make sure to apply the finally block to kill resources like closing database connection, closing files and other resources such that they get executed independent of whether the code worked in Try or went to Catch.
- Use Page.ISPostBack Ensure that you you don’t execute code without a valid reason. Use Page.ISPostBack property to ensure that you only perform page initialization logic when a page is loaded for the first time and not in response to client postbacks.
- Avoid Unnecessary Indirection When you use byRef, you pass pointers and not the the actual object. Quite often, this looks prudent (side-effecting functions, for example), but it’s not always required. Passing pointers leads to more indirection, which is slower than accessing a value that is on the stack.
When you don’t need to go through the heap, it is best to avoid it there by avoiding indirection.
- Always check Page.IsValid when using Validator Controls Always make sure you check Page.IsValid before processing your forms when using Validator Controls.
- Avoid making frequent calls across processes
- Minimize assemblies Minimize the number of assemblies you use to keep your working set small. If you load an entire assembly just to use one method, you’re paying a tremendous cost for very little benefit. See if you can duplicate that method’s functionality using code that you already have loaded.
- Use the String builder to concatenate string instead of string
- Use Foreach loop instead of For loop for String Iteration
