CPU usage is high, can I know what is hogging the CPU?

S

Sin Jeong-hun

In short, is there any way to know the very code that is currently
hogging the CPU?

I've written an application which continously GETting web pages using
HttpWebRequest's asynchronous methods (it only GETs responses
(BegingGetResponse,EndGetResponse), not reads the stream contents).
I've read articles about asynchronous request operations. I aborted
timed out requests based on the article. ( http://www.developerfusion.co.uk/show/4654/
) And, I kept the number of HttpWebRequets waiting for response below
30. But still, as the execution time passes, the CPU usage is
gradually increasing. If memory consumption gets gradually high, then
I might think it's due to some memory leak, but what about this
situation? At first the CPU usage was below 5% but about 6 minutes
later, it gets as high as 40%. I don't know why it increases when it
is doing the same work in a loop.

I've downloaded the CLR profiler from Microsft and tried it, but it
only showed memory usages and allocations, not the CPU usage. When my
application is hogging 40% of the CPU, is it possible for me to know
which operation is consuming it? Since my code is simple, I think it
is probably one of the underlying HttpWebRequest or other BCL classes.

Thanks for your help.
 
N

Nicholas Paldino [.NET/C# MVP]

Sin Jeong-hun,

Well, you kind of indicated what the problem might be. You said that
you are running the work in a loop. This is a CPU-bound operation, and is
going to consume the CPU depending on what you are doing in the loop and how
many iterations you are performing.

What exactly are you doing in the loop?

Also, how are you determining what your memory consumption is?
 
P

Peter Bromberg [C# MVP]

Instead of doing your work in a loop, use a Threadpool with QueueUserWorkItem
and a callback method. Make sure that you Close / Dispose of any Response
streams in a finally block in the callback.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top