Best practices for avoiding high CPU usage

Take advantage of the recommended best practices to avoid CPU outrages in your application

Factors that cause high cpu usage

Application performance has for long been an important aspect and depends on many factors. While building applications, you might need to monitor its performance (resource consumption or usage over a period of time) and use the performance data to identify the bottlenecks in the application. Resource consumption here implies CPU, Disk I/O or Memory usage.

There are performance monitoring tools aplenty. However, if you follow these best practices, you can mitigate performance bottlenecks in your application earlier in the development life cycle.

Exception handling This is the technique of handling runtime errors in your application code. An exception is an error that occurs at runtime and terminates the normal flow of execution of a program if not handled properly. You should refrain from throwing or re-throwing exceptions in your application unless it is absolutely needed. Trust me, if you design your application the correct way, you would hardly need to throw too many exceptions in your code. Although an entry or exit from the try block isn't costly, the cost is associated when your code throws exceptions. Note that when an exception is thrown, the runtime needs to walk back through the calling frames and this incurs more CPU cycles while at the same time blocking the currently executing thread.

Using HttpResponse.Redirect The HttpResponse.Redirect method throws a ThreadAbortException and it causes the current thread, i.e., the thread on which the exception is thrown to exit. Thereafter, a new thread is allocated from the thread pool for further processing. As a result of all this, the CPU utilization increases and this can be a performance bottleneck if you use this method frequently in your application. To overcome the pitfall of this method call, you can take advantage of the overloaded version of the same method and then pass a value of "false" in the second parameter. You can then make a call to the Context.ApplicationInstance.CompleteRequest() method to ensure that the control exits gracefully. This is needed as the overloaded version of the HttpResponse.Redirect method would not terminate the current flow.

The following code snippet illustrates how you can use the overloaded version of Httpresponse.Redirect method while at the same time ensuring that the ThreadAbortException is not thrown in your application.

Response.Redirect("MainMenu", false);


Reduce processing overheads You should try to avoid reflection calls in your application as much as you can. This is needed to reduce the processing overhead. In most cases, you can design your application in such a way that reflection wouldn't be needed. You should also avoid using recursive methods in your application. It should be noted that recursive methods consume a lot of memory and CPU cycles.

Multi-threading Multithreading is the ability to have multiple threads in memory at a given time and switch amongst them so as to provide a pseudo parallelism, as though all the threads are executing at the same time. You should use multi-threading with care. Although you can take advantage of multithreading to perform several tasks simultaneously and increase the application's throughput, it should be used judiciously. Incorrect usage of multithreading may result in high CPU usages or increased CPU cycles and can drastically reduce your application's performance.

In this article I've discussed some of the factors that can increase the CPU usage and make your application to be unresponsive. There are many more factors that we'll explore in future posts here.