tags:

views:

369

answers:

8

I wrote a Java program to download HTML page. But CPU usage is near to 100%, while network utilization is lower than 3%. It seems like that CPU became my bottleneck. So is there any suggestions about cutting my CPU usage.

+4  A: 

If you have a continuous while loop, give your program some sleep time between iterations. Downloading the web pages alone should not cause that much resource utilization though, you may want to look into a profiler to find out whats bottlenecking you. Perhaps posting the code here would let us help you a bit more.

John T
+2  A: 

If your CPU usage is near to 100% for a long period of time, then most probably you have an error in your code (infinite loop or something). Try to profile your application to see what is happening. Start from printing the current time from various points at your code. If you still can't find that out, a profiler will be needed.

kgiannakakis
If the loop is infinite, grabbing a couple stack traces will find it. If the loop is _not_ infinite, but merely long, the same method works.
Mike Dunlavey
+5  A: 

use a profiler (I like VisualVM), identify the bottleneck and fix it!

dfa
+1  A: 

One very simple thing you can do to identify the problem is just grab a few stack traces. ctrl-\/ctrl-break/jstack/jconsole/visualvm. If the program is catastrophically spending a lot of its time where the performance problem is (reasonably likely), then you should easily see the problem.

Tom Hawtin - tackline
++ beat me to it. That's my favorite method.
Mike Dunlavey
+1  A: 

Hi,

Maybe you are polling for data too fast...

I would try to change the code to event-driven notification.

Regards.

ATorras
+1  A: 
KarlP
A: 

Are you, by any chance, parsing the HTML using the built in Java XML DOM stuff? From past experience, it can result in some pretty hefty CPU usage (and it's the slowest implementation I've ever seen, honestly). If so, you might want to consider a 3rd party library for XML parsing (jdom, for instance).

RHSeeger