For Programmers: Free Programming Magazines  


Home > Archive > Java Help > September 2004 > URL class heavy on COU usage









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author URL class heavy on COU usage
jmm-list-gn

2004-09-24, 4:00 am

Hello,
Java v1.4.2_05.
I am using the URL and HttpsURLConnection classes in a program. As soon
as the openConnection() method is called, the CPU usage goes to 100%
greatly slowing the functioning of other programs. When the connection
closes, the usage drops back to normal.
Is this expected? A possible problem in the JVM?
Is there an alternate, low CPU usage, way of connecting?

Here is the code that pegs the CPU meter (less the exception code):

URL url;
String url_str = protocol + "://" + hostname
+ "/" + prefix + "/" + service;

// Create new URL and connect
//
// Let Sun's JSSE to deal with SSL
java.security.Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());
System.getProperties().put("java.protocol.handler.pkgs",
"com.sun.net.ssl.internal.www.protocol");
url = new URL(url_str);
HttpsURLConnection connection =
(HttpsURLConnection) url.openConnection();

// Setup HTTP POST parameters
connection.setDoOutput (true);
connection.setDoInput (true);
connection.setUseCaches(true);

// Get POST data from input StringBuffer containing an XML document
//
String queryString = XmlIn.toString();

// POST data
//
System.out.println("...sending request..."); System.out.flush();
OutputStream out = connection.getOutputStream();
out.write(queryString.getBytes());
out.close();

// get data from URL and return the XML doc as a StringBuffer
//
String data = "";
data = readURLConnection(connection);

// Data is saved and the function exits here closing the connection.

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
FISH

2004-09-24, 4:03 pm

jmm-list-gn <jmm-list.AXSPAMGN@sohnen-moe.com> wrote in message news:<0qWdnXdwQ6w7JM7cRVn-sQ@giganews.com>...
> Hello,
> Java v1.4.2_05.
> I am using the URL and HttpsURLConnection classes in a program. As soon
> as the openConnection() method is called, the CPU usage goes to 100%
> greatly slowing the functioning of other programs. When the connection
> closes, the usage drops back to normal.
> Is this expected? A possible problem in the JVM?


Which JVM would that be? ;-)


> Is there an alternate, low CPU usage, way of connecting?


Almost certainly, the fault is in your code. You don't specify what
the readURLConnection method does. It's possible there's some bad logic
in there controlling a loop. That's the most likely possibility. SSL
is another candidate, although I'd rule out your own code first, before
you start to blame Sun's.

-FISH- ><>
jmm-list-gn

2004-09-24, 4:03 pm

FISH wrote:
>
> Which JVM would that be? ;-)
>

java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-b04)
Java HotSpot(TM) Client VM (build 1.4.2_05-b04, mixed mode)
>
>
> Almost certainly, the fault is in your code. You don't specify what
> the readURLConnection method does.
>

Oops. Here it is:

int letter;
reader = new BufferedReader(new
InputStreamReader(uc.getInputStream()));
while (-1 != (letter = reader.read()))
buffer.append((char) letter);
reader.close();

I'd have thought that read() would block until there was actual input.

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Boudewijn Dijkstra

2004-09-24, 8:57 pm

"jmm-list-gn" <jmm-list.AXSPAMGN@sohnen-moe.com> schreef in bericht
news:0qWdnXdwQ6w7JM7cRVn-sQ@giganews.com...
> Hello,
> Java v1.4.2_05.
> I am using the URL and HttpsURLConnection classes in a program. As soon
> as the openConnection() method is called, the CPU usage goes to 100%
> greatly slowing the functioning of other programs. When the connection
> closes, the usage drops back to normal.
> Is this expected? A possible problem in the JVM?


Any unbuffered system stream is likely to draw 100% CPU, period.


jmm-list-gn

2004-09-24, 8:57 pm

Boudewijn Dijkstra wrote:
>
> Any unbuffered system stream is likely to draw 100% CPU, period.
>

Hmm, yes. Most of the time is spent in the HttpsURLConnection methods.
readURLConnection() is a buffered stream.
So your implication is that unbuffered streams are used in the http and
URL classes. Is there a way to use buffered streams?

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
Boudewijn Dijkstra

2004-09-25, 9:00 am

"jmm-list-gn" <jmm-list.AXSPAMGN@sohnen-moe.com> schreef in bericht
news:IsidnaBhcZDKLcncRVn-oA@giganews.com...
> Boudewijn Dijkstra wrote:
> Hmm, yes. Most of the time is spent in the HttpsURLConnection methods.
> readURLConnection() is a buffered stream.
> So your implication is that unbuffered streams are used in the http and
> URL classes. Is there a way to use buffered streams?


You can buffer an arbitrary stream by wrapping it into a new
java.io.BufferedXxxputStream, like so:
myBufferedInput = new BufferedInputStream(myInput);
or
myBufferedOutput = new BufferedOutputStream(myOutput);

You can then use the new stream in the same way as the old one, or even use
the same reference:
myXxxput = new BufferedXxxputStream(myXxxput);


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com