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 Michael Velbaum <mi...@gmail.com> on 2016/05/17 20:28:19 UTC

Correct resource deallocation with HttpClient 4.3.x

Hi,

I have a few questions regarding proper resource de-allocation, as this
topic is not covered in detail in the tutorial.

1. The CloseableHttpClient interface allows you to close an HttpClient.
What advantage does manually closing an HttpClient have over simply letting
the HttpClient object be garbage collected when I'm done with it?

2. Do I need to use a CloseableHttpResponse and manually close the response
if I already use EntityUtils.toByteArray(entity) to fully consume the
response?

Thanks!

Re: Correct resource deallocation with HttpClient 4.3.x

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Tue, 2016-05-17 at 23:28 +0300, Michael Velbaum wrote:
> Hi,
> 
> I have a few questions regarding proper resource de-allocation, as this
> topic is not covered in detail in the tutorial.
> 
> 1. The CloseableHttpClient interface allows you to close an HttpClient.
> What advantage does manually closing an HttpClient have over simply letting
> the HttpClient object be garbage collected when I'm done with it?
> 

It ensures immediate shutdown and deallocation of system resources
allocated by persistent (kept-alive) connection.

> 2. Do I need to use a CloseableHttpResponse and manually close the response
> if I already use EntityUtils.toByteArray(entity) to fully consume the
> response?
> 

Yes, you do. You should always close responses with try-finally or
try-with-resources to make sure resources allocated by the response get
deallocated even in case of an exception.

If the response content has been fully consumed by the time
CloseableHttpResponse#close is called the overhead of the #close method
will be literally a true/false check on a volatile boolean variable.

Oleg


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