Skip to content

Dealing with performance issues of TPL

Dealing_with_performance_issues_of_TPL.jpg

Overview : Task Parallel Library (TPL) facilitates us to do multiple tasks in parallel. But at the same time, due to the multiple tasks overhead, we may encounter many issues which affect the application’s performance. This blog provides the information like how to handle the tasks efficiently without degrading the performance of the application.


TPL:

The Task Parallel Library (TPL) is in light of the idea of an assignment, which speaks to a nonconcurrent operation. In a few ways, an assignment takes after a string or ThreadPool work thing, yet at a larger amount of reflection. The term undertaking parallelism alludes to one or more free tasks running simultaneously.

Application Use Case:

Our application needs to execute the python scripts which return xml payloads of different formats. We need to process the xml payloads separately and concurrently. So, we have implemented TPL by scheduling tasks based on the xml format type.

Performance issues:

As many tasks may run in parallel and for a longer time, the application’s performance may degrade. Some of the performance issues, we usually encounter are:

  • High CPU utilization.
  • Application becomes non responsive.
  • Incorrect output retrieval.
  • Ongoing operation terminates abruptly (Script execution in our scenario).
  • Memory leakage.

Possible Solutions:

  • Dispatcher.Priority() : We usually use dispatchers for performing actions on UI elements. If we haven’t used them, application will throw InvalidOperationException. So use of Dispatcher is mandatory but it will have a great effect on CPU usage and sometimes application becomes non responsive. But what we can do is, we can decrease the priority of the Dispatcher. Optimal priority type is DispatcherPriority.Background.
  • EventWaitHandle. WaitOne () : If we have any dependent tasks running in parallel, we need to handle the output receiving from those tasks properly else the received output may be incorrectly processed. Using EventWaitHandle. WaitOne () method, we can make other dependent tasks to wait until the current task gets processed, then we can resume the other dependent tasks by using EventWaitHandle.Set () method.
  • StringBuilder : Generally we use String objects to handle the data, but it occupies maximum of memory if we do any string concatenations as it creates a new String object for every string concatenation or assignment. The best practice is to use StringBuilder instead of String object. This will not create new object if we assign or concatenate values, instead it appends the new value to the original one.
  • StringBuilder.Append() : If any requirement falls to have data to be hold in some variable and do some manipulations with it, use associated Append () method of StringBuilder object for appending the data instead of using concatenation operator (+).
  • Custom Task Scheduler : Task Scheduler creates the number of concurrent tasks based on the requirement which may increases the CPU usage and may also leads to memory leakage issues if we have them more in number. Custom Task Scheduler allows us to define the number of concurrent tasks to be created and run when we schedule a main task so that we can have a flexibility to specify the task count according to our requirement. 
  • Lock variable : Some tasks may need to access resources common to each other. In such case if we do not handle them properly may leads to occur deadlocks, incorrect retrieval of output and abrupt termination of operations. Lock variable ensures resources common to multiple tasks be accessed in synchronous fashion. Below screen shots shows WPF application which executes a python script that prints 1 to 100000 statements on the screen.

Before solving performance issue:

Before solving performance issue

After solving performance issue:

After solving performance issue

Above screen grab shows python script completed its execution in less than 1 min with optimal use of resources.

Phenomecloud is an enthusiastic family of individuals, fervent to make lives simpler through effective use of technology. Our mission is to implement solutions that drives business results. Know more insights from our thoughts and experience.
 
Contact us today or call 1-855-978-6816 to talk with us about your business needs.
Sharepoint services

Leave a Comment

@ 2024 Phenom Enterprise Cloud. All Rights Reserved.