hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Memory leak using httpclient
Date Tue, 14 Mar 2006 18:09:28 GMT
On Tue, 2006-03-14 at 12:53 -0500, James Ostheimer wrote:
> Oleg-
> 
> I noticed that javadocs reccomend using getResponseBodyAsStream, and I 
> changed my get code as follows:
> 

...

> 
>    char[] cbuf = new char[1024];
>    int len = 0;
>    while((len = ir.read(cbuf)) > 0) {
>     rawPage += new String(cbuf);

-----------------------------^

This is all horribly inefficient and wrong.

Try this (Disclaimer: the code may fail to compile. I just typed this
stuff directly in my mail client. Make changes you deem necessary)

char[] cbuf = new char[1024];
StringBuffer sbuf = new StringBuffer();
int len = 0;
while((len = ir.read(cbuf)) > 0) {
 sbuf.append(cbuf, 0, len);
 if (sbuf.length() > 100000) {
   method.abort();
   // Important. Do not forget to abort the
   // method prior to releasing the connection
   // Otherwise the connection manager will
   // attempt to consume the remaining content 
 }
}


> 
> So I now quit on any file over 100 kb.

No wonder.

>   My memory still steadily rises and 
> now Jprofiler claims:
>
>     a.. 72.7% - 102,068 kB - 2,463 alloc. java.lang.StringBuffer.toString()
> from getPageApache().  That means that somewhere in the above code 
> (including calls into HttpClient) I am eating up a lot of memory in 
> StringBuffers.
> 

No wonder.

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message