We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Terrible performance problem

ba272
ba272 asked
on
Medium Priority
376 Views
Last Modified: 2012-06-27
Greetings,

I'm having a terrible performance problem where if my program is running, it's using 100% of the processor's available CPU.  I've written an object which tracks thhe time spent is most every function, summarizes the elapsed times by function, and writes them to disk.  But I can't fund what's taking so long.  The values I see are relatively small, 20-250 milliseconds per 15 second period.

Has anybody got a suggestion of how I could approach this problem from a different angle?  I would love ot be able to query Windows to find out what function or functions are using al the processor time.

Has anybody got any idea?  I'm in a serious pickle.


Thanks,
Bob
Comment
Watch Question

Commented:
You might want to look into disposing your unused objects. That should help a little bit.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
If you are using visual studio 2005 you can use the performance profiler

http://msdn.microsoft.com/vstudio/teamsystem/reference/technotes/profiling_windows_apps/perm_profiler.aspx

to start a new performance session go to:
Tools->Performance Tools-> new Performance session
then debug the progrma and after you close it will write a performance protokoll for you, so you can see what funktions are taking more resources and are runned offter,

you also could think about using timers if you wait for something to happen and task managers to run big calculations step by step

Commented:
maybe this process explorer could help you too:

http://www.sysinternals.com/Utilities/ProcessExplorer.html
Commented:
you can start standard windows task manager, switch to the performance tab, then start debugging your program step-by-step and see what's exactly causing this performance problem.

good luck,
yurich

Author

Commented:
strickdd,

Could you please tell me more about disposing my objects?  As a C++ programmer I can certainly see the importane of this, but never got into that in C# because I thought the garbage collector did that for me.

Yurish, that's a relly cool idea I'll have to try.


Thanks,
Bob

Commented:
the garbage collector takes care of it of course but it's a good practice to release high-resource consuming objects such as all graphics objects (graphics itself, pens, brushes, and etc), io objects, some other stuff - doesn't come to my mind now ;)

good luck,
yurich

Author

Commented:
I suspect that my problem involves, ata minimum, not closing a database connection.

When using the following objects, which ones need to be closed?  I know I must close the connection and will add the Close() function inside the finally block in the event an exception is thrown.  What else must I close?

Thanks,
Bob

if ( menuItemSqlDataAdapter == null )
{
   menuItemSqlDataAdapter = new SqlDataAdapter( sql, connectString );
   menuItemDataSet = new DataSet();
   menuItemSqlDataAdapter.Fill(menuItemDataSet, "Order_Lines");
}
else
{
   SqlConnection myConnection = new SqlConnection( connectString );
   myConnection.Open();
   SqlCommand sqlCommand = new SqlCommand( sql, myConnection );
   menuItemSqlDataAdapter.SelectCommand = sqlCommand;
   menuItemDataSet.Clear();
   menuItemSqlDataAdapter.Fill( menuItemDataSet, "Order_Lines" );
}

Commented:
in fact, you don't need to call Open or Close for your connection if you're using data adapter - it will handle gracefully openning and closing connection as it needs it.

in terms of database components, you can close only connection (which you don't have to close since you're using adapters) and data reader (which you don't use in your code), everything else (command, relation, parameter, adapter) are not closable.

regs,
yurich

Author

Commented:
Yurich,

How wouldyou change this:

SqlConnection myConnection = new SqlConnection( connectString );
myConnection.Open();
SqlCommand sqlCommand = new SqlCommand( sql, myConnection );
menuItemSqlDataAdapter.SelectCommand = sqlCommand;
menuItemDataSet.Clear();
menuItemSqlDataAdapter.Fill( menuItemDataSet, "Order_Lines" );

to get rid of the connection so that the SqlDataAdapter can manage the connection?  I tried this but got an exception: Connection property has not been initialized.

SqlCommand sqlCommand = new SqlCommand( sql, myConnection );
menuItemSqlDataAdapter.SelectCommand = sqlCommand;
menuItemDataSet.Clear();
menuItemSqlDataAdapter.Fill( menuItemDataSet, "Order_Lines" );

I'd like to let the SqlDataAdapter manage the connection for me.  Could you tell me how?


Thanks,
Bob

Commented:
SqlDataAdapter can manage only opening and closing connection - it cannot create it, so try this:

SqlConnection myConnection = new SqlConnection( connectString );
SqlCommand sqlCommand = new SqlCommand( sql, myConnection );
menuItemSqlDataAdapter.SelectCommand = sqlCommand;
menuItemDataSet.Clear();
menuItemSqlDataAdapter.Fill( menuItemDataSet, "Order_Lines" );

in fact, you don't have to Clear your dataset every time you fill it as it will get resetted on its own when refilled, so it would be this one:

SqlConnection myConnection = new SqlConnection( connectString );
SqlCommand sqlCommand = new SqlCommand( sql, myConnection );
menuItemSqlDataAdapter.SelectCommand = sqlCommand;
menuItemSqlDataAdapter.Fill( menuItemDataSet, "Order_Lines" );

good luck,
yurich

Commented:
from my understanding of your question, you problem is because CPU is being utilized 100%, but no real performance issue in terms of how long the task get done, right?

As you mentioned you log all processing time lapsed, and all are within damn small timing.

Utilizing CPU 100% isn't really a performance concern IMO.

Author

Commented:
well there are performance problems, like the cursor flickers and the GUI really suffers when you need to close a program and open a window.  The whole system does bog down.

If the Task Manager shows ~100% utilization as soon as my main window opens, wouldn't that mean there is nothing left for any other process?  I'd like and try to get the utilization down to a level more like it used ot be.  In fact, given that my program runs on a "heartbeat" of 15 seconds, and that most of the work takes a second o two at the most, traditionally I've seen the utilization climb quickly, and sometomes reach 100%, but just as quickly fall again.  But lately it's flatlines at 100%.

Any suggestions to get it down?  (I haven't yet followed the links above.)

Commented:
try debugging with monitoring your performance - the one i advised before.

a continuous resource consumption can be happen if you have a timer for example and very cycle your intialize some resources (graphics, external dll access), in which case it doesn't matter whether you release them or not (i mean if you don't release you'd probably crash your system) but even if you do, you intialize 'em again...

when an application runs it can easily push the cpu usage to 100% but only for a very short time... hmm - have i mentioned debugging with monitoring already?

good luck,
yurich

Author

Commented:
Thanks for the help all.  Turns out I found the problem by doing a destructive test on my source code.  I deleted various controls from the window in question, then commented out major parts of the code, all to no avail.  Finally when the processor was still maxing at 100%, I used the "break" option under debugging, and I saw the problem.  Many moons ago I had written a parser that determined how many exceptions I had logged to my exceptions text file, so I could display the count on my window.  Well, I had a few bugs which had caused a huge number of exceptions, and that file needed to be parsed every heartbeat.   Dumb!

So thanks,

Bob
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.