You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Joan Balaguero <jo...@grupoventus.com> on 2010/03/30 16:29:53 UTC
Releasing connections
Hello,
Ive implemented the following code to send a request, read the response and
returns back the connection to the pool:
(
)
Try
{
(
)
entity = this.objHttp.execute(objPost));
if (entity != null)
{
bis = new BufferedInputStream(entity.getContent());
ByteArrayOutputStream buffer = new
ByteArrayOutputStream(httpOutBufferSize);
byte[] tmp = new byte[httpOutBufferSize];
int numBytesRead = 0;
while ((numBytesRead = bis.read(tmp)) >= 0) buffer.write(tmp, 0,
numBytesRead);
bis.close();
bis = null;
}
}
Catch (Exception e) { throw new ProxyServletException(); }
Finally
{
If (bis != null)
{
Try { bis.close(); bis = null; } catch (Exception e1) {}
}
If (entity != null)
{
Try { entity.consumeContent(); } catch (Exception e1) {}
}
}
With this code:
1. The connection is returned back to the pool correctly (the pool
never becomes exhausted).
2. But I have more and more instances of java.net.* classes in my heap
histogram:
For example:
SockSocketImpl (number of instances)
12:30 19.517
13:30 70.731
14:00 107.061
SocketInputStream
12:30 907
13:30 68.650
14:00 103.568
It seems that the connection is returned back to the pool but the resources
associated with the http response are not freed up. At 12:20 I started
Tomcat, and the load from 12:20 since now is similar.
Is there any error in my code that Im not able to see?
Thanks in advance,
Joan.
Re: Releasing connections
Posted by Oleg Kalnichevski <ol...@apache.org>.
Joan Balaguero wrote:
> Hello,
>
>
>
> I’ve implemented the following code to send a request, read the response and
> returns back the connection to the pool:
>
>
>
> (…)
>
> Try
>
> {
>
> (…)
>
> entity = this.objHttp.execute(objPost));
>
>
>
> if (entity != null)
>
> {
>
> bis = new BufferedInputStream(entity.getContent());
>
> ByteArrayOutputStream buffer = new
> ByteArrayOutputStream(httpOutBufferSize);
>
> byte[] tmp = new byte[httpOutBufferSize];
>
> int numBytesRead = 0;
>
>
>
> while ((numBytesRead = bis.read(tmp)) >= 0) buffer.write(tmp, 0,
> numBytesRead);
>
> bis.close();
>
> bis = null;
>
> }
>
> }
>
> Catch (Exception e) { throw new ProxyServletException(); }
>
> Finally
>
> {
>
> If (bis != null)
>
> {
>
> Try { bis.close(); bis = null; } catch (Exception e1) {}
>
> }
>
>
>
> If (entity != null)
>
> {
>
> Try { entity.consumeContent(); } catch (Exception e1) {}
>
> }
>
> }
>
>
>
> With this code:
>
>
>
> 1. The connection is returned back to the pool correctly (the pool
> never becomes exhausted).
>
> 2. But I have more and more instances of java.net.* classes in my heap
> histogram:
>
>
Joan
There is no need for so much code. Reliable connection release can be
implemented much simpler:
http://svn.apache.org/repos/asf/httpcomponents/httpclient/tags/4.0.1/httpclient/src/examples/org/apache/http/examples/client/ClientConnectionRelease.java
>
> For example:
>
> SockSocketImpl (number of instances)
>
> 12:30 19.517
>
> 13:30 70.731
>
> 14:00 107.061
>
>
>
> SocketInputStream
>
> 12:30 907
>
> 13:30 68.650
>
> 14:00 103.568
>
>
>
>
>
> It seems that the connection is returned back to the pool but the resources
> associated with the http response are not freed up. At 12:20 I started
> Tomcat, and the load from 12:20 since now is similar.
>
>
>
> Is there any error in my code that I’m not able to see?
>
>
HttpClient is not known to leak SockSocketImpl instances. In any way,
please upgrade to the latest SVN snapshot of HttpCore and re-test your
application.
http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/
If the problem persists please try to gather more information about the
problem by turning on context logging for connection management /
request execution as described here:
http://hc.apache.org/httpcomponents-client-4.0.1/logging.html
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org