A server (Websphere AS) crashed with an 'Out of Memory' error. Asked to investigate it I somehow came to this piece of code..(I think it was either or both the heap and thread dumps).
HttpURLConnection conn = new URL("some url").openConnection();
.. post some data to conn.. then..
StringBuffer sb = new StringBuffer();
InputStream is = conn.getInputStream();
while (true) {
int i = is.read();
if (i == -1) break;
sb.append(i);
}
clean up etc..
I saw two problems with this code:
1) If the system we were reading from went into some kind of loop, and kept feeding us data, we would keep reading it into our heap and ultimately crash when we ran out of heap-memory.
2) Reading one char at a time is not particularly efficient. A bufferedInputStream would be more appropriate. I ran some benchmarks and found that to read a 2240 bytes, the time fell from about 140ms to about 40ms with a 1024 byte readbuffer. This is pretty much all raw CPU time. So, if you are doing a lot of this, it would make a difference.
So, to prevent the crash put in some limit (whatever is reasonable in your case) into your read loop (break and throw an exception if the read exceeds a certain size), and to improve performance -- use BufferedInputStream to do your reading. Use 1024 bytes to start with, but the size of your buffer would depend on your application of course.
Sunday, November 29, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment