Monday, August 10, 2015

Tips to improve the C# & ASP .Net performance improvement

Following are few tips to improve the C# & ASP .Net performance improvemnt:
1.       Running the profiler on several Application files individually.

2.       Introducing a Helper Class to debug and provide resource utilization of each function\methods

3.       Caching at content management level & caching at MVC website level needs to be evaluated.

4.       If there are file Download mechanism then it needs to be verified. There are open source that do better job.

5.       Can the pool size be increased to serve more number of request? Whether the connection release is happening properly.

6.       Enablement of recycling of .Net Application pool.

7.       Analyzing Clustered Index and Non-Clustered Indexes on the DB

8.       Usage of static methods & static variable needs to be verified. Issue with the static is that one can add objects to static data structure without removing it & hence bloating the memory.

9.       Whether some mechanism of the Garbage collection has been implemented?

10.   Whether some shared resource is there for multiple threads. In java it is Synchronized resources. This makes the tread to wait & hence increase the CPU usage. Can the tread pool size be increased?

11.   It is very easy to concatenate the Strings in C# - you need just “+” (plus) operator to do it but there are few drawbacks in this. One should know that String object is immutable in C#, once created they cannot be changed anymore. Whenever you are concatenating a String, the old one is getting dropped and new concatenated one is getting created which slow up the performance significantly. So, one should look up for StringBuilder to enhance the performance. Use the System.Text.StringBuilder class to perform the concatenations instead of using the “+” concatenation operator or String.Concat, since this class has been optimized for string concatenation. The buffer size will get changed automatically (if required) and length will also get tracked. This needs to be done where the variable scope is within the method.
When to use “+” (plus) or String.Concat ()

Security Issue: When one don’t want to change the value of String. Actually, Immutable makes things easier. When one is passing a String to function you can be very sure that its value will not get changed, so it helps in bypassing the security.

Small Operation: When you are doing Concatenation for couple of strings then we can go for “+” (plus) or String.Concat ().

When to use StringBuilder

Inside loop: Whenever one is appending the String inside the loop, one should go for StringBuilder to enhance the performance. Even, if one is appending the Strings over and over again, then you should go for StringBuilder.

12.   Say NO to Generic Exceptions. Error handling is always better but should be used with care. It is a costly resource and must be avoided as possible. Catch specific exceptions with precise details always rather using a root top level Exception class to catch everything. Such as catching an IOException is better instead of Exception e.

13.   As in the Java there is heap size which can be increased  to accommodate more objects & improve the performance. What is equivalent of this in the C# & ASP .Net?

14.   There few ways to do IIS performance tuning:   HTTP Compression, Limiting Connections, Configuring Application Pool Queue-Length . This is the low hanging fruit.

15.   I have asked the customer to increase the CPU. If he refuses then please introduce one infra architect also to look for optimizing the OS page size, RAM Cache. He will also look into optimizing the HTTP request for the firewalls, optimize CPU Share Allotment For A process. Also, based on the I/O Statistics ,split the subdisks in the volume and to move those regions to a less busy disk.


No comments:

Post a Comment