10 tips that can improve the performance of .Net web applications

by

CAST-ten-tips-that-can-improve-the-performance-of-NET-web-applicationsWhen every product has the same features, the only way to make a difference in the jungle that is today’s software ecosystem is by having the one that performs best. Of course, in this article, by product, we mean application and its code. For .Net applications, this is truer than ever. Here are ten tips that can greatly improve the performance of your .Net application

1. Avoid recursive functions and nested loops

Recursive functions (or nested loops) present advantages which are unfortunately greatly diminished by higher consumption of memory and therefore decrease of overall performance. Proper functions and callbacks increase performance, instead of having huge chunks of code in one single method.

2. Avoid string concatenation in loops

When placed in a loop, string concatenation results in the creation and garbage collection of large numbers of temporary objects. This consumes memory and can dramatically slow the application execution. In order to avoid this, it is recommended to create a StringBuilder before entering the loop, and append to it within the loop, thus reducing the overhead.

3. Do not forget to release resources

If you have opened files, for example, make sure that you close them at the end. The finally block is really the best place to do so, as it is the only block of code that will surely be executed, no matter what happens in your catch block.

Opening connections without closing them also generates performance problems as they leak resources. At first, it slows down the application, but it can rapidly turn into a critical and costly application crash.

4. Avoid instantiations inside loops

One of the fundamental, object-oriented performance management principles is to avoid excessive object creation. This doesn't mean that you should give up the benefits of object-oriented programming by not creating any objects, but you should be wary of object creation inside of tight loops when executing performance-critical code. Object creation is expensive enough that you should avoid unnecessarily creating temporary or intermediate objects in situations where performance is an issue.

5. If possible, avoid throwing exceptions

Exceptions are one of the key processes to handling errors that might occur in your applications.  Their downside, however, is that they come with a performance hit, and their management must be done properly. Otherwise, they might do more harm than good. They are probably one of the heaviest resource hogs and causes of slowdowns you will ever see in web applications. For example, catching a specific exception is better than catching the most generic one, as hiding a problem never works in the long term.

6. Use caching to improve performance

OutputCaching enables your page to be cached and invalidated under specific circumstances (ex. parameters or duration). The cache exists for the duration you specify. Until this duration has elapsed, the requests are served from the cache -- preserving the server from unnecessary requests and lowering its overloading. Therefore, in order to increase performance, avoid assigning a short expiration to cached items. Items that expire quickly cause unnecessary turnover in the cache, generating frequently more work for the garbage collector. If the page has static as well as dynamic sections, partial caching (fragment caching) should be used by breaking up your page into user controls and specifying caching for only those controls which are more-or-less static.

7. Disable tracing unless until it is required

Tracing is a very good feature during development. It enables you to track the application during this development and therefore it is very useful. However when the application is put in production, this tracing can decrease the performance of the application. Therefore, it is highly recommended to disable it. This can be done very easily as follow in the “web.config” file:

<trace enabled="false" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>

8. Disable session state, if not required

The session states are not required in cases when the pages are static or there is no information to be stored from the page. In these cases, disabling it improves overall performances of the application.

9. Avoid frequent calls to the database

Calls made to the database can be quite expensive in terms of response time as well as resources. They can be avoided by using batch processing. Therefore, trying to make calls to the database as minimal as possible and making them last for even less time is very important for the performances of an application. The use of DataAdapter wherever applicable is very helpful since it automatically opens and closes connections whenever required and doesn’t require the user to explicitly open the connection. Optimizing stored procedures is also very important.

10. Avoid direct or indirect I/O inside nested loops

All I/O operations made inside nested loops can badly impact the performances of your application.  In order to improve performance, it is recommended to avoid this practice and keep this type of operation outside the loop.

All these tips are helpful. However, sometimes it might be difficult to check everything when the application set is large. A better way to do it is have them checked with an automatic tool. Using CAST AIP is a good way to detect the issues before is too late and the impacts are permanent.

Filled in: Software Analysis
Narcisa Zysman Senior Product Manager at CAST
Load more reviews
Thank you for the review! Your review must be approved first
Rating
New code

You've already submitted a review for this item

|