General considerations to be made to improve Performance of .NET Applications
1. Resource Cleanup Failing to clean up resources is a common cause of performance and scalability bottlenecks. Review your code to make sure all resources are closed and released as soon as possible. This is particularly important for shared and limited resources such as connections. Make sure your code calls Dispose (or Close) on disposable resources.
Make sure your code uses finally blocks or using statements to ensure resources are closed even in the event of an exception.
2. Exceptions While structured exception handling is encouraged because it leads to more robust code and code that is less complex to maintain than code that uses method return codes to handle error conditions, exceptions can be expensive. Make sure you do not use exception handling to control regular application flow. Use it only for exceptional conditions. Avoid exception handling inside loops — surround the loop with a try/catch block instead if that is required. Also identify code that swallows exceptions or inefficient code that catches, wraps, and rethrows exceptions for no valid reason.
3. String Management
Excessive string concatenation results in many unnecessary allocations, creating extra work for the garbage collector. Use StringBuilder for complex string manipulations and when you need to concatenate strings multiple times. If you know the number of appends and concatenate strings in a single statement or operation, prefer the + operator. In ASP.NET applications, consider emitting HTML output by using multiple Response.Write calls instead of using a String Builder.
4. Threading:
Server-side code should generally use the common language runtime (CLR) thread pool and should not create threads on a per-request basis. Review your code to ensure that appropriate use is made of the thread pool and that the appropriate synchronization primitives are used. Make sure your code does not lock whole classes or whole methods when locking a few lines of code might be appropriate. Also make sure your code does not terminate or pause threads by using Thread.Abort or Thread.Suspend.
5. Boxing Boxing causes a heap allocation and a memory copy operation. Review your code to identify areas where implicit boxing occurs. Pay particular attention to code inside loops where the boxing overhead quickly adds up. Avoid passing value types in method parameters that expect a reference type. Sometimes this is unavoidable. In Check List for Improving the Performance of .NET Applications this case, to reduce the boxing overhead, box your variable once and keep an object reference to the boxed copy as long as needed, and then unbox it when you need a value type again. Excessive boxing often occurs where you use collections that store System.Object types. Consider using an array or a custom-typed collection class instead. To identify locations that might have boxing overhead, you can search your assembly’s Microsoft intermediate language (MSIL) code for the box and unbox instructions, using the following command line.
Ildasm.exe yourcomponent.dll /text | findstr box Ildasm.exe yourcomponent.dll /text | findstr unbox To measure the overhead, use a profiler.
For more detailed checklist for managed code performace , visit my blog
|
| Author: Prasoon Kumar 01 Jul 2009 | Member Level: Silver Points : 2 |
Hi Explicit Resource Clean Up by calling close or dispose is
necessary when we are dealing with objects in unmanaged
memory like while writing it to log file etc....
otherwise garbage collector will take care of anything
in the managed memory area(managed heap memory).
Regards Prasoon
|