Improve Performance of .NET Application

This article provides some concepts for improving the performance of .NET applications. This article assumes that you have a basic understanding of .NET and you have hands-on experience with project development. Basically, these guidelines have been provided by Microsoft's developers, I have just assembled them from various places.

It will be very useful if we follow them in our day-to-day work.

Manage memory efficiently

It is very important to manage memory very efficiently to improve the performance of applications. We need to keep in mind a few tips during application development.

  • Always use a block if the class implements the IDisposable interface.
  • Don't create a new object always, try to use an existing object when possible. For example, for a function call, one object can be used for calling multiple functions in the same class.
  • Don't forget to dispose of an unmanaged object.
  • Try to not run GC.Collect(). It removes unused objects but slows down the application.
  • For large session objects, try to use a different session storage mechanism, like SQL Server or state server.
  • Always implement a final block to ensure that your resource will be released, even in the worst situation.

Multithreading in application

Minimize thread creation and use the safe-tuning thread pool for multithreaded work. Avoid creating threads on a per–request basis; also avoid using Thread. Abort or Thread. Suspend.

Ensure that you appropriately tune the thread pool for ASP.NET applications and for Web Services.

Try to use an Asynchronous call

It's a new feature of C# 5.0. It may benefit client-side applications where you need to maintain user interface responsiveness. Those are the situations where we can use an asynchronous call as in the following:

  • To call a web service
  • To read and write a huge file from an application
  • To perform heavy database operations

Handle exception properly

Exception objects are one of the most resource-consuming objects in applications. So try to use them with enough care. We should not use an exception in regular application logic (sometimes people throw an exception at the time of validation, ensure that it's not happening in your application). Don't throw a new exception when you want to throw it across layers. Throwing a new exception consumes the same resources as a new exception. Just use throw one if you really want to throw it.

Don't forget to implement a finally in each try-catch block.

Choose string and StringBuilder in the right situation

It is recommended to choose StringBuilder when there is a need to perform a huge number of string concatenations. For small concatenation purposes, String performs well.

Choose the proper data type in an application

Arrays are the fastest of all collection types, so unless you need special functionalities like a dynamic extension of the collection, sorting, and searching, you should use arrays. If you need a collection type then choose the most appropriate type based on your functionality requirements to avoid performance penalties.

  • Use ArrayList to store custom object types and particularly when the data changes frequently and you perform frequent insert and delete operations.
    Avoid using ArrayList for storing strings.
  • Use a StringCollection to store strings.
  • Use a Hashtable to store a large number of records and to store data that may or may not change frequently. Use Hashtable for frequently queried data such as product catalogs where a product ID is a key.
  • Use a HybridDictionary to store frequently queried data when you expect the number of records to usually be below with occasional increases in size.
  • Use a ListDictionary to store small amounts of data (fewer than 10 items).
  • Use a NameValueCollection to store strings of key-value pairs in presorted order. Use this type for data that changes frequently where you need to insert and delete items regularly and where you need to cache items for fast retrieval.
  • Use a Queue when you need to access data sequentially (First In, First Out) based on priority.
  • Use a Stack in scenarios where you need to process items in a Last In, First Out manner.
  • Use a SortedList for fast object retrieval using an index or key. However, avoid using a SortedList for large data changes because the cost of inserting a large amount of data is high. For large data changes, use an ArrayList and then sort it by calling the Sort method. Pay attention to ADO.NET code since a large part of any project is nothing but CRUD operations, so we need to pay more attention to ADO.NET code.
  • Choose the proper Data Access Layer. Think twice before using any third-party data access layer.
  • Implement a transaction properly because transaction blocks database resources from being used.
  • Use connection pooling and try to use a connection object efficiently.
  • Don't forget to release a database object after operations on it

Improve performance of Serialization

Reduce the amount of data that is serialized by using XmlIgnore or NonSerialize attributes. To improve DataSet serialization, you can use column name aliasing, you can avoid serializing both the original and the updated data values, and you can reduce the number of DataTable instances that you serialize.

Reduce the working set size

A smaller working set produces better system performances. Fewer large assemblies rather than many smaller assemblies help reduce working-set size.

Manage the BLOB object with special care

Avoid moving Binary Large OBject (BLOB) data repeatedly, and consider storing pointers in the database to BLOB files that are maintained on the file system. Use Chunking to reduce the load in the server, and use chunking where network bandwidth is very limited. Use CommandBehaviour.SequencialAccess enumerator to stream BLOB data.

Try to use DataReader

Since nothing is faster than a DataReader, do not use a Dataset object when it is possible to use a DataReader object. DataReader is best if we want access forward-only read-only data and if we do not want to cache the data. Use a DataSet when you need to add flexibility to when you need to cache data between requests.

Conclusion

Those tips are very important to improve the performance of our applications. I hope you have understood all the concepts and you are very much interested in implementing them in your next application.


Similar Articles