none
High CPU utilization by a C#.Net Application RRS feed

  • Question

  • Hello experts,

    I am a novice C# developer, so pardon me if this is a very basic question.

    I have an application written in C#.Net version 4.0. This application calls a CPP library. The problem is whenever I call the C# application in a loop on Windows 7 (or via Autosys job), it hangs at a particular stage. When I check process explorer, I can see that the CPU utilization of this process is almost 50%. I also checked the status of this application in Resource Monitor and there it shows that one of the threads of the application is causes kind of blocking for the main thread and therefore, overall the application hangs. However, on Windows XP, same application runs perfectly fine. Unfortunately, I wouldn't be able to provide the names of the CPP application or sample code here due to copyright limitations, but I'll try to answer if there are any supplimentary information that would help to understand the issue.

    Has anyone noticed such behaviour? What could be the issue? Really appreciate any pointer in this regard.

    Thanks in advance!

    Thursday, May 29, 2014 11:54 AM

Answers

  • Using PE or the debugger identify the method that is hanging.  Note that there are many threads that will be waiting while your code runs and that is fine because waits don't take CPU time. What you're looking for is the thread that is using all the CPU time.  If you have a loop that is calling a native method then that is most likely the culprit.  Interop calls aren't cheap and calling them in a loop will make it far worse.  But the first step is to identify where the high usage is.  After that you can determine how to optimize the call(s) and/or move the work to a secondary thread so the app is responsive.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, May 29, 2014 2:24 PM
    Moderator

All replies

  • Using PE or the debugger identify the method that is hanging.  Note that there are many threads that will be waiting while your code runs and that is fine because waits don't take CPU time. What you're looking for is the thread that is using all the CPU time.  If you have a loop that is calling a native method then that is most likely the culprit.  Interop calls aren't cheap and calling them in a loop will make it far worse.  But the first step is to identify where the high usage is.  After that you can determine how to optimize the call(s) and/or move the work to a secondary thread so the app is responsive.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, May 29, 2014 2:24 PM
    Moderator
  • This is a difficult question to respond to since it's somewhat vague. Several things come to mind, not the least of which is what exactly is the CPP library providing? Is it talking to a device? A network? Performing computational math? String sorts? Database table joins? 

    The list of possibilities is enormous.

    I'd start by trying to run Process Explorer and track down where the CPU is being used most. 

    Without a lot more detail, you're asking the equivalent of "Why did my dog bark last night?"

    Thursday, May 29, 2014 10:41 PM