joydip_kanjilal
Contributor

Best practices to improve application performance in ASP.Net

opinion
Jun 29, 20154 mins
Software Development

Take advantage of the recommended practices to reduce page load time, manage state and resources efficiently and improve data access performance in your ASP.Net applications

There are factors aplenty that influence application performance of web applications. The essence to improving application performance is ensuring that you build applications that would consume the least amount of memory and require the least amount of processing to produce the desired output.

To achieve this, you need to adhere to the recommended guidelines and techniques that are instrumental for improving, optimizing and boosting the performance of your web application. In this post, I’ll discuss the most important recommendations that you should follow to improve the application performance and responsiveness of Web applications built using ASP.Net.

Reducing page load time

To reduce the page load time of your Web pages, you should minify the scripts and CSS files and avoid excessively large images, redundant tags, and nested tables. Avoid using server controls (unless there is a specific reason to use them) to minimize the size of your Web pages.

You should also avoid unnecessary roundtrips to the Web server to facilitate faster page loads. You can take advantage of the Page.IsPostback property to avoid unnecessary server processing on a roundtrip hence reducing the network traffic. Another technique you can follow is pre-compilation – you can pre-compile the Web pages in your application to reduce the working set size. You can also set AutoEventWireup attribute to “false” in the machine.config file so that the runtime doesn’t have to search for each of the event handlers in a Web page.

   <configuration>

     <system.web>

       <pages autoEventWireup="true|false" />

     </system.web>

   </configuration>

When you set this property to false, the page events would not be auto wired hence eliminating the possibility of the same event from being called twice when the page is in execution.

You should bundle the scripts and css used by you application as much as possible. Take advantage of asynchronous calls from the Web page to the server side methods whenever possible — this will help your Web page to be responsive.

State management

You should avoid using ViewState to facilitate faster page loads. Remember that every byte added to a web page by enabling its ViewState would incur two bytes of network traffic — one byte in each direction, i.e., from the server to the client and the other from the client to the server. Also, you should remove the runat=”server” form tag from your Web page if you don’t need to use ViewState. This would save you around 20 bytes of the page size.

Caching is another state management technique available for you — use it judiciously to store relatively stale data in memory. You can cache the Web pages or portion of your Web pages if need be. Data caching can be used to boost the application performance as reading data from the cache memory is relatively faster than reading the same data from a file or database.

You should optimize your code to ensure that you use resources (memory and processor, etc.) judiously — I’ll write a separate post on this.

Resource management

Proper resource management techniques if followed, can boost your application’s performance to a considerable extent. You should acquire resources (file handles, database connections, etc.) late and dispose them early. You should write your code in such a way that the objects are not promoted to higher generations — remember that the garbage collector works much more frequently in the lower generations than in the higher ones. You should use Dispose and Finalize appropriately to clean up the unmanaged resources you use in your application. It is a good practice to wrap the resource intensive code in your application within a using block. This would ensure that the resources are disposed of properly when they’re no longer needed. Note that the “using” statement on compilation degenerates into a “try – finally” combination and can be used only for those objects that implement the IDisposable interface.

You should also leverage the recommended data access strategies and ensure that your application doesn’t hold on to the database connections for a long time to facilitate better connection pooling. You should write you code in such a way that it uses minimal number of database connections. If your application holds on to the database connections, there is a chance that the database connection pool might run out of available connections hence degrading the performance if the demand for connections exceeds a certain limit. You can take advantage of stored procedures in most cases to reduce the processing overhead in your database server for frequently used queries — this will help improve the data access performance to a considerable extent.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author