Welcome @ Ajay's cyber home

 

 

 

 

 
  • Articles
 
 
 
 

Performance Tips and Tricks for ASP.NET

Throw Fewer Exceptions One should always try to code in a way that least exceptions are thrown. Throwing exceptions can be very expensive. Finding and designing away exception-heavy code can result in a decent performance boost. Remember that this has nothing to do with try/catch blocks: you only incure the cost when the actual exception is thrown. You can use as many try/catch blocks as you want. Using exceptions gratuitously is where you lose performance.
Disable session state when you are not using it
Not all applications or pages require per-user session state, and you should disable it for any that do not. To disable session state for a page, set the EnableSessionState attribute in the @Page directive to false. For example, <%@ Page EnableSessionState="false" %>. Note If a page requires access to session variables but will not create or modify them, set the EnableSessionState attribute in the @Page directive to ReadOnly. To disable session state for an application, set the mode attribute to off in the sessionstate configuration section in the application's Web.config file. For example, <sessionstate mode="off" />.
Avoid unnecessary round trips to the server
While it is tempting to use the time- and code-saving features of the Web Forms page framework as much as you can, there are circumstances in which using ASP.NET server controls and postback event handling are inappropriate. Typically, you need to initiate round trips to the server only when your application is retrieving or storing data. Most data manipulations can take place on the client between these round trips. For example, validating user input from HTML forms can often take place on the client before that data is submitted to the server. In general, if you do not need to relay information to the server to be stored in a database, you should not write code that causes a round trip. If you develop custom server controls, consider having them render client-side code for browsers that support ECMAScript. By using server controls in this way, you can dramatically reduce the number of times information is unnecessarily sent to the Web server.
Use Page.IsPostBack to avoid performing unnecessary processing on a round trip
If you write code that handles server control postback processing, you will sometimes want other code to execute the first time the page is requested, rather than the code that executes when a user posts an HTML form contained in the page. Use the Page.IsPostBack property to conditionally execute code depending on whether the page is generated in response to a server control event.
Save server control view state only when necessary
Automatic view-state management is a feature of server controls that enables them to repopulate their property values on a round trip (without your having to write any code). This feature does affect performance, however, since a server control's view state is passed to and from the server in a hidden form field. You should be aware of when view state helps you and when it hinders your page's performance. For example, if you are binding a server control to data on every round trip, the saved view state is replaced with new values that are obtained from the data-binding operation. In this case, disabling view state saves processing time.
Leave buffering on unless you have a specific reason to turn it off
There is a significant performance cost for disabling buffering of Web Forms pages.
Use CLR's garbage collector and automatic memory management appropriately
Be careful about allocating too much memory per request because the garbage collector will have to do more work more often. Also, do not have unnecessary pointers to objects, since they will keep the objects alive, and try to avoid having objects with Finalize methods, since they will entail more work at a later time. In particular, never free resources in a call to Finalize, since the resource could consume memory until the garbage collector runs it. This last problem often ruins performance in Web server environments, since it can be easy to exhaust the availability of a given resource while waiting for Finalize to run.
Consider performing pre-batch compilation for large applications
Batch compilation is performed whenever a first request is made to a directory. If no page in the directory has been parsed and compiled, this feature will parse and compile all pages in the directory in chunks to provide better disk and memory usage. If this takes too long, a single page will be parsed and compiled quickly so that the request can be processed. This feature gives ASP.NET a performance benefit, since it compiles many pages into a single assembly. Accessing a page from an assembly that is already loaded is faster than loading a new assembly per page. The downside of batch compilation is that if the server receives many requests for pages that have not been compiled, performance can be poor while the Web server parses and compiles them. To solve this problem, you can perform pre-batch compilation. To do so, before your application goes live, simply request a page from it — it does not matter which page. Then, when users first access your site, the pages and their assembly will already be compiled.
Use HttpServerUtility.Transfer method to redirect between pages in the same application.
Using this method in a page, with Server.Transfer syntax, avoids unnecessary client-side redirection. Use early binding in Visual Basic .NET or JScript code Historically, one of the reasons that developers enjoy working with Visual Basic, VBScript, and JScript is their so-called typeless nature. Variables need no explicit type declaration and can be created simply by using them. When assigning from one type to another, conversions are performed automatically. However, this convenience can cause your application's performance to suffer greatly.
Use SQL Server stored procedures for data access
Of all the data access methods provided by the .NET Framework, SQL Server –based data access is the recommended choice for building high-performance, scalable Web applications. When using the managed SQL Server provider, you can get an additional performance boost by using compiled stored procedures instead of ad hoc queries.
Use the SqlDataReader class for a fast forward-only data cursor
The SqlDataReader class provides a means to read a forward-only data stream retrieved from a SQL Server database. If situations arise while you are creating an ASP.NET application that allow you to use it, the SqlDataReader class offers higher performance than the DataSet class. This is the case because SqlDataReader uses SQL Server's native network data-transfer format to read data directly from a database connection.
Choose the data viewing mechanism appropriate for your page or application
Depending on how you choose to display data in a Web Forms page, there are often significant tradeoffs between convenience and performance. For example, the DataGrid Web server control can be a quick and easy way to display data, but it is frequently the most expensive in terms of performance. Rendering the data yourself by generating the appropriate HTML may work in some simple cases, but customization and browser targeting can quickly offset the extra work involved. A Repeater Web server control is a compromise between convenience and performance. It is efficient, customizable, and programmable.
Disable debug mode Always remember to disable debug mode before deploying a production application or conducting any performance measurements. If debug mode is enabled, the performance of your application can suffer a great deal.

 

.